march_hare 3.0.0-java → 3.1.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 24c5f476a2e6d0f317060d847bac5b3415a6af9e
4
- data.tar.gz: cbd1a71d51155ca8898b89983c3409b08a914378
2
+ SHA256:
3
+ metadata.gz: a1b5c3b68a7999e7a4395f7287ac7a31e05a2d79acd0da0d117339842c491946
4
+ data.tar.gz: 80484d5a1935a4348d4720293e49c05c89cad8778f0aad2fa28cf39b1bc081e3
5
5
  SHA512:
6
- metadata.gz: 26dcc481444be82948f0234e493ab7c943e4f292e5f455e53acf1a1405b622846bf734d8f6bae3f10e06951b96ba760667fcc1a3e614484328fad7b61d486e87
7
- data.tar.gz: 69586d383dacb082c63221b51fed78795d94d31377b77cf296cfd5a46b8ed348d39373916d75c617a70c450df09df87434f247396e842311abc61babc6c13089
6
+ metadata.gz: 160281080b5551e33ca042d868f5f57190f1a4b5997a0a7b3bc354d120826c5bdb999b07d820bb3c7598860c017bd03466449ae4f01d2d971500200a9997b15d
7
+ data.tar.gz: cad20d0d92a2ccd8e857e2755ca8e59b5b454926604ad72ea16174f43419af968f93b028d07225923912529163dbc61a177a44d3a49cc56688c8a6534c0e76d7
Binary file
@@ -10,10 +10,12 @@ module MarchHare
10
10
  java_import com.rabbitmq.client.BlockedListener
11
11
  java_import com.rabbitmq.client.NullTrustManager
12
12
  java_import com.rabbitmq.client.MissedHeartbeatException
13
+ java_import com.rabbitmq.client.Address
13
14
 
14
15
  java_import javax.net.ssl.SSLContext
15
16
  java_import javax.net.ssl.KeyManagerFactory
16
17
  java_import java.security.KeyStore
18
+ java_import javax.net.ssl.TrustManagerFactory
17
19
 
18
20
  # Connection to a RabbitMQ node.
19
21
  #
@@ -39,6 +41,8 @@ module MarchHare
39
41
  #
40
42
  # @option options [Numeric] :executor_shutdown_timeout (30.0) when recovering from a network failure how long should we wait for the current threadpool to finish handling its messages
41
43
  # @option options [String] :host ("127.0.0.1") Hostname or IP address to connect to
44
+ # @option options [Array<String>] :hosts (["127.0.0.1"]) Array of hostnames or ips to connect to. The connection returned is the first in the array that succeeds.
45
+ # @option options [Array<String>] :addresses (["127.0.0.1:5672", "localhost:5673"]) Array of addresses to connect to. The connection returned is the first in the array that succeeds.
42
46
  # @option options [Integer] :port (5672) Port RabbitMQ listens on
43
47
  # @option options [String] :username ("guest") Username
44
48
  # @option options [String] :password ("guest") Password
@@ -55,6 +59,10 @@ module MarchHare
55
59
 
56
60
  if options[:uri]
57
61
  cf.uri = options[:uri] if options[:uri]
62
+ elsif options[:hosts] || options[:addresses]
63
+ cf.virtual_host = vhost_from(options) if include_vhost?(options)
64
+ cf.username = username_from(options) if include_username?(options)
65
+ cf.password = password_from(options) if include_password?(options)
58
66
  else
59
67
  cf.host = hostname_from(options) if include_host?(options)
60
68
  cf.port = options[:port].to_i if options[:port]
@@ -75,12 +83,11 @@ module MarchHare
75
83
  case tls
76
84
  when true then
77
85
  cf.use_ssl_protocol
78
- when String then
86
+ when String then
79
87
  # TODO: logging
80
88
  $stdout.puts "Using TLS/SSL version #{tls}"
81
- if options[:trust_manager]
82
- cf.use_ssl_protocol(tls, options[:trust_manager])
83
- elsif (cert_path = tls_certificate_path_from(options)) && (password = tls_certificate_password_from(options))
89
+ # Note: `options[:trust_manager] = com.rabbitmq.client.NullTrustManager.new` can be set to disable TLS verification.
90
+ if (cert_path = tls_certificate_path_from(options)) && (password = tls_certificate_password_from(options))
84
91
  ctx = SSLContext.get_instance(tls)
85
92
  pwd = password.to_java.to_char_array
86
93
  begin
@@ -91,7 +98,14 @@ module MarchHare
91
98
  kmf = KeyManagerFactory.get_instance("SunX509")
92
99
  kmf.init(ks, pwd)
93
100
 
94
- ctx.init(kmf.get_key_managers, [NullTrustManager.new].to_java('javax.net.ssl.TrustManager'), nil)
101
+ if options[:trust_manager]
102
+ ctx.init(kmf.get_key_managers, [options[:trust_manager]], nil)
103
+ else
104
+ # use the key store as the trust store
105
+ tmf = TrustManagerFactory.get_instance(TrustManagerFactory.getDefaultAlgorithm());
106
+ tmf.init(ks)
107
+ ctx.init(kmf.get_key_managers, tmf.getTrustManagers(), nil)
108
+ end
95
109
 
96
110
  cf.use_ssl_protocol(ctx)
97
111
  rescue Java::JavaLang::Throwable => e
@@ -103,6 +117,8 @@ module MarchHare
103
117
  ensure
104
118
  is.close if is
105
119
  end
120
+ elsif options[:trust_manager]
121
+ cf.use_ssl_protocol(tls, options[:trust_manager])
106
122
  else
107
123
  cf.use_ssl_protocol(tls)
108
124
  end
@@ -137,15 +153,8 @@ module MarchHare
137
153
  # we expect this option to be specified in seconds
138
154
  @executor_shutdown_timeout = opts.fetch(:executor_shutdown_timeout, 30.0)
139
155
 
140
- @hosts = self.class.hosts_from(opts)
141
- @default_host_selection_strategy = lambda { |hosts| hosts.sample }
142
- @host_selection_strategy = opts[:host_selection_strategy] || @default_host_selection_strategy
143
-
144
- @connection = if @uses_uri
145
- self.new_uri_connection_impl(@uri)
146
- else
147
- self.new_connection_impl(@hosts, @host_selection_strategy)
148
- end
156
+ @addresses = self.class.adresses_from(opts)
157
+ @connection = build_new_connection
149
158
  @channels = JavaConcurrent::ConcurrentHashMap.new
150
159
 
151
160
  # should automatic recovery from network failures be used?
@@ -273,13 +282,7 @@ module MarchHare
273
282
  java.lang.Thread.sleep(ms)
274
283
 
275
284
  new_connection = converting_rjc_exceptions_to_ruby do
276
- reconnecting_on_network_failures(ms) do
277
- if @uses_uri
278
- self.new_uri_connection_impl(@uri)
279
- else
280
- self.new_connection_impl(@hosts, @host_selection_strategy)
281
- end
282
- end
285
+ reconnecting_on_network_failures(ms) { build_new_connection }
283
286
  end
284
287
  self.recover_shutdown_hooks(new_connection)
285
288
 
@@ -398,8 +401,8 @@ module MarchHare
398
401
  end
399
402
 
400
403
  # @private
401
- def self.hosts_from(options)
402
- options[:hosts] || [hostname_from(options)]
404
+ def self.adresses_from(options)
405
+ options[:addresses] || options[:hosts] || [hostname_from(options)]
403
406
  end
404
407
 
405
408
  # @private
@@ -497,6 +500,8 @@ module MarchHare
497
500
  raise ConnectionRefused.new("Connection to #{@cf.host}:#{@cf.port} refused: host unknown")
498
501
  rescue java.net.SocketException => e
499
502
  raise ConnectionRefused.new("Connection to #{@cf.host}:#{@cf.port} failed")
503
+ rescue java.util.concurrent.TimeoutException => e
504
+ raise ConnectionRefused.new("Connection to #{@cf.host}:#{@cf.port} failed: timeout")
500
505
  rescue com.rabbitmq.client.AuthenticationFailureException => e
501
506
  raise AuthenticationFailureError.new(@cf.username, @cf.virtual_host, @cf.password.bytesize)
502
507
  rescue com.rabbitmq.client.PossibleAuthenticationFailureException => e
@@ -516,18 +521,21 @@ module MarchHare
516
521
  end
517
522
 
518
523
  # @private
519
- def new_connection_impl(hosts, host_selector)
520
- if hosts && !hosts.empty? && !@uses_uri
521
- @cf.host = host_selector.call(hosts)
522
- end
524
+ def build_new_connection
525
+ @uses_uri ? new_uri_connection_impl(@uri) : new_connection_impl(@addresses)
526
+ end
527
+
528
+ # @private
529
+ def new_connection_impl(addresses)
530
+ addrs = Address.parse_addresses(addresses.join(','))
523
531
 
524
532
  converting_rjc_exceptions_to_ruby do
525
533
  if @executor_factory
526
534
  shut_down_executor_pool_and_await_timeout
527
535
  @executor = @executor_factory.call
528
- @cf.new_connection(@executor)
536
+ @cf.new_connection(@executor, addrs)
529
537
  else
530
- @cf.new_connection
538
+ @cf.new_connection(addrs)
531
539
  end
532
540
  end
533
541
  end
@@ -6,27 +6,58 @@ module MarchHare
6
6
  class ThreadPools
7
7
  # Returns a new thread pool (JDK executor) of a fixed size.
8
8
  #
9
+ # @param [Integer] n Number of threads to use
10
+ #
11
+ # @option options [Boolean] :use_daemon_threads (false) Should new threads be marked as daemon?
12
+ #
9
13
  # @return A thread pool (JDK executor)
10
- def self.fixed_of_size(n)
14
+ def self.fixed_of_size(n, use_daemon_threads: false)
11
15
  raise ArgumentError.new("n must be a positive integer!") unless Integer === n
12
16
  raise ArgumentError.new("n must be a positive integer!") unless n > 0
13
17
 
14
- JavaConcurrent::Executors.new_fixed_thread_pool(n)
18
+ if use_daemon_threads
19
+ JavaConcurrent::Executors.new_fixed_thread_pool(n, &daemon_thread_factory)
20
+ else
21
+ JavaConcurrent::Executors.new_fixed_thread_pool(n)
22
+ end
15
23
  end
16
24
 
17
25
  # Returns a new thread pool (JDK executor) of a fixed size of 1.
18
26
  #
27
+ # @option options [Boolean] :use_daemon_threads (false) Should new threads be marked as daemon?
28
+ #
19
29
  # @return A thread pool (JDK executor)
20
- def self.single_threaded
21
- JavaConcurrent::Executors.new_single_thread_executor
30
+ def self.single_threaded(use_daemon_threads: false)
31
+ if use_daemon_threads
32
+ JavaConcurrent::Executors.new_single_thread_executor(&daemon_thread_factory)
33
+ else
34
+ JavaConcurrent::Executors.new_single_thread_executor
35
+ end
22
36
  end
23
37
 
24
38
  # Returns a new thread pool (JDK executor) that will create new
25
39
  # threads as needed.
26
40
  #
41
+ # @option options [Boolean] :use_daemon_threads (false) Should new threads be marked as daemon?
42
+ #
27
43
  # @return A thread pool (JDK executor)
28
- def self.dynamically_growing
29
- JavaConcurrent::Executors.new_cached_thread_pool
44
+ def self.dynamically_growing(use_daemon_threads: false)
45
+ if use_daemon_threads
46
+ JavaConcurrent::Executors.new_cached_thread_pool(&daemon_thread_factory)
47
+ else
48
+ JavaConcurrent::Executors.new_cached_thread_pool
49
+ end
50
+ end
51
+
52
+ # Returns a new thread factory that creates daemon threads.
53
+ #
54
+ # @option options [ThreadFactory] :base_factory Upstream thread factory used to create threads
55
+ #
56
+ # @return A thread factory
57
+ def self.daemon_thread_factory(base_factory: JavaConcurrent::Executors.default_thread_factory)
58
+ proc { |runnable|
59
+ base_factory.new_thread(runnable).tap { |t| t.daemon = true }
60
+ }
30
61
  end
31
62
  end
32
63
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module MarchHare
4
- VERSION = "3.0.0"
4
+ VERSION = "3.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: march_hare
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: java
6
6
  authors:
7
7
  - Theo Hultberg
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-20 00:00:00.000000000 Z
12
+ date: 2018-02-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: RabbitMQ client for JRuby built around the official RabbitMQ Java client
15
15
  email:
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project: march_hare
59
- rubygems_version: 2.6.8
59
+ rubygems_version: 2.6.13
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: RabbitMQ client for JRuby built around the official RabbitMQ Java client