jruby-hornetq 0.2.5.alpha → 0.3.0.alpha

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.
Files changed (77) hide show
  1. data/HISTORY.md +12 -0
  2. data/Rakefile +13 -1
  3. data/bin/hornetq_server +2 -2
  4. data/examples/README +6 -0
  5. data/examples/{client/advanced → advanced}/batch_client.rb +18 -34
  6. data/examples/advanced/bytes_producer.rb +25 -0
  7. data/examples/advanced/client.rb +56 -0
  8. data/examples/{client/advanced/multi_client.rb → advanced/client_session_pooling.rb} +29 -25
  9. data/examples/advanced/consume_on_message.rb +50 -0
  10. data/examples/advanced/consumer.rb +31 -0
  11. data/examples/{client/advanced → advanced}/hornetq.yml +8 -6
  12. data/examples/advanced/multi_client.rb +71 -0
  13. data/examples/advanced/producer.rb +37 -0
  14. data/examples/advanced/server.rb +44 -0
  15. data/examples/client-server/client.rb +40 -0
  16. data/examples/client-server/server.rb +37 -0
  17. data/examples/producer-consumer/consume_all.rb +25 -0
  18. data/examples/producer-consumer/consume_on_message.rb +46 -0
  19. data/examples/producer-consumer/consumer.rb +27 -0
  20. data/examples/producer-consumer/producer.rb +32 -0
  21. data/examples/{client/resque → resque}/hornetq_job.rb +2 -2
  22. data/examples/{client/resque → resque}/processor.rb +1 -1
  23. data/examples/{client/resque → resque}/readme.md +0 -0
  24. data/examples/{client/resque → resque}/resque_conf.rb +0 -0
  25. data/examples/{client/resque → resque}/resque_worker.rb +0 -0
  26. data/examples/{client/resque → resque}/sleep_job.rb +0 -0
  27. data/examples/{server → server-config}/backup_server.yml +0 -0
  28. data/examples/{server → server-config}/live_server.yml +0 -0
  29. data/examples/{server → server-config}/standalone_server.yml +0 -0
  30. data/examples/{client → simple}/invm.rb +6 -7
  31. data/examples/{advanced/transaction-failover → transaction-failover}/README +0 -0
  32. data/examples/{advanced/transaction-failover → transaction-failover}/consumer.rb +4 -1
  33. data/examples/{advanced/transaction-failover → transaction-failover}/hornetq.yml +6 -3
  34. data/examples/{advanced/transaction-failover → transaction-failover}/producer.rb +8 -3
  35. data/examples/{simple_worker → worker}/README +0 -0
  36. data/examples/worker/hornetq.yml +26 -0
  37. data/examples/worker/producer.rb +71 -0
  38. data/examples/{simple_worker → worker}/test_object.rb +0 -0
  39. data/examples/worker/worker.rb +72 -0
  40. data/lib/hornetq/client.rb +1 -1
  41. data/lib/hornetq/client/{factory.rb → connection.rb} +153 -86
  42. data/lib/hornetq/client/message_handler.rb +3 -3
  43. data/lib/hornetq/client/org_hornetq_api_core_client_client_session.rb +187 -26
  44. data/lib/hornetq/client/org_hornetq_core_client_impl_client_consumer_impl.rb +26 -9
  45. data/lib/hornetq/client/org_hornetq_core_client_impl_client_message_impl.rb +190 -35
  46. data/lib/hornetq/client/session_pool.rb +4 -4
  47. data/lib/hornetq/common/log_delegate.rb +17 -15
  48. data/lib/hornetq/common/logging.rb +20 -7
  49. data/lib/hornetq/common/org_hornetq_core_logging_logger.rb +11 -9
  50. data/lib/hornetq/server.rb +86 -3
  51. data/lib/hornetq/{org_hornetq_core_server_hornet_q_server.rb → server/org_hornetq_core_server_hornet_q_server.rb} +1 -1
  52. data/lib/hornetq/uri.rb +4 -0
  53. data/test/client_connection_test.rb +158 -0
  54. data/test/logging_test.rb +32 -0
  55. data/test/server_test.rb +208 -0
  56. metadata +47 -48
  57. data/examples/client/advanced/bytes_producer.rb +0 -21
  58. data/examples/client/advanced/client.rb +0 -47
  59. data/examples/client/advanced/consumer.rb +0 -35
  60. data/examples/client/advanced/multi_consumer.rb +0 -54
  61. data/examples/client/advanced/producer.rb +0 -35
  62. data/examples/client/advanced/server.rb +0 -39
  63. data/examples/client/client.rb +0 -31
  64. data/examples/client/consumer.rb +0 -22
  65. data/examples/client/data/bindings/hornetq-bindings-1.bindings +0 -0
  66. data/examples/client/data/bindings/hornetq-bindings-2.bindings +0 -0
  67. data/examples/client/data/journal/hornetq-data-1.hq +0 -0
  68. data/examples/client/data/journal/hornetq-data-2.hq +0 -0
  69. data/examples/client/producer.rb +0 -21
  70. data/examples/client/server.rb +0 -31
  71. data/examples/simple_worker/hornetq.yml +0 -34
  72. data/examples/simple_worker/producer.rb +0 -54
  73. data/examples/simple_worker/worker.rb +0 -49
  74. data/lib/data/bindings/hornetq-bindings-1.bindings +0 -0
  75. data/lib/data/bindings/hornetq-bindings-2.bindings +0 -0
  76. data/lib/hornetq/server/factory.rb +0 -87
  77. data/test/server_factory_test.rb +0 -183
@@ -4,6 +4,9 @@
4
4
  # Write messages to the queue
5
5
  #
6
6
 
7
+ # Allow examples to be run in-place without requiring a gem install
8
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
9
+
7
10
  require 'rubygems'
8
11
  require 'yaml'
9
12
  require 'hornetq'
@@ -14,7 +17,7 @@ config = YAML.load_file(File.dirname(__FILE__) + '/hornetq.yml')
14
17
  constants = config['constants']
15
18
 
16
19
  # Create a HornetQ session
17
- HornetQ::Client::Factory.session(config['client']) do |session|
20
+ HornetQ::Client::Connection.session(config['client']) do |session|
18
21
  consumer = session.create_consumer(constants[:queue])
19
22
  session.start
20
23
 
@@ -12,12 +12,15 @@ live_server:
12
12
  :security_enabled: false
13
13
 
14
14
  client:
15
- :connector:
15
+ :connection:
16
16
  :uri: hornetq://localhost:15445,localhost:15446
17
17
  :failover_on_initial_connection: true
18
18
  :failover_on_server_shutdown: true
19
- # 5 Connection attempts takes about 16 seconds before it switches to the backup server
20
- :reconnect_attempts: 5
19
+ # Retry at 0.5, 1.0, 2.0, 4.0, 5.0
20
+ :reconnect_attempts: 4
21
+ :retry_interval: 500
22
+ :retry_interval_multiplier: 2.0
23
+ :max_retry_interval: 5000
21
24
 
22
25
  :session:
23
26
  :username: guest
@@ -4,6 +4,9 @@
4
4
  # Write messages to the queue
5
5
  #
6
6
 
7
+ # Allow examples to be run in-place without requiring a gem install
8
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
9
+
7
10
  require 'rubygems'
8
11
  require 'yaml'
9
12
  require 'hornetq'
@@ -13,8 +16,8 @@ config = YAML.load_file(File.dirname(__FILE__) + '/hornetq.yml')
13
16
  constants = config['constants']
14
17
 
15
18
  # Create a HornetQ session
16
- HornetQ::Client::Factory.session(config['client']) do |session|
17
- session.create_queue(constants[:address], constants[:queue], constants[:durable])
19
+ HornetQ::Client::Connection.session(config['client']) do |session|
20
+ session.create_queue_ignore_exists(constants[:address], constants[:queue], constants[:durable])
18
21
  producer = session.create_producer(constants[:address])
19
22
  start_time = Time.now
20
23
 
@@ -23,7 +26,8 @@ HornetQ::Client::Factory.session(config['client']) do |session|
23
26
  message = session.create_message(HornetQ::Client::Message::TEXT_TYPE, true)
24
27
  # Set the message body text
25
28
  message.body = "Message ##{i}"
26
- message.put_string_property(Java::org.hornetq.api.core.SimpleString.new(HornetQ::Client::Message::HDR_DUPLICATE_DETECTION_ID.to_s), Java::org.hornetq.api.core.SimpleString.new("uniqueid#{i}"))
29
+ # TODO: Figure out why this isn't working as expected
30
+ message[HornetQ::Client::Message::HDR_DUPLICATE_DETECTION_ID] = "uniqueid#{i}"
27
31
  # Send message to the queue
28
32
  begin
29
33
  producer.send(message)
@@ -41,3 +45,4 @@ HornetQ::Client::Factory.session(config['client']) do |session|
41
45
  duration = Time.now - start_time
42
46
  puts "Delivered #{count} messages in #{duration} seconds at #{count/duration} messages per second"
43
47
  end
48
+
File without changes
@@ -0,0 +1,26 @@
1
+ standalone_server:
2
+ :uri: hornetq://localhost
3
+ :data_directory: ./data
4
+ :persistence_enabled: true
5
+ :security_enabled: false
6
+
7
+ client:
8
+ :connection:
9
+ :uri: hornetq://localhost
10
+ :session:
11
+ :username: guest
12
+ :password: guest
13
+ :auto_close: true
14
+ :session_pool:
15
+ :pool_size: 3
16
+ :pool_warn_timeout: 0.25
17
+ :username: guest
18
+ :password: guest
19
+
20
+ :constants:
21
+ :address1: address1
22
+ :address2: address2
23
+ :queue1_1: queue1_1
24
+ :queue1_2: queue1_2
25
+ :queue2_1: queue2_1
26
+ :queue2_2: queue2_2
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env jruby
2
+ #
3
+ # HornetQ Producer:
4
+ # Write messages to the queue
5
+ #
6
+
7
+ # Allow examples to be run in-place without requiring a gem install
8
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
9
+
10
+ require 'rubygems'
11
+ require 'yaml'
12
+ require 'hornetq'
13
+ require 'logger'
14
+ require 'json'
15
+ require 'test_object'
16
+
17
+ config = YAML.load_file(File.dirname(__FILE__) + '/hornetq.yml')
18
+ client = config['client']
19
+
20
+ # Create a HornetQ session
21
+ logger = Logger.new($stdout)
22
+ connection = HornetQ::Client::Connection.new(client[:connection])
23
+ session_pool = connection.create_session_pool(client[:session_pool])
24
+
25
+ ['HUP', 'INT', 'TERM'].each do |signal_name|
26
+ Signal.trap(signal_name) do
27
+ puts "caught #{signal_name}"
28
+ $stopped = true
29
+ end
30
+ end
31
+
32
+ $stopped = false
33
+ threads = []
34
+ (1..5).each do |i|
35
+ threads << Thread.new(i) do |thread_count|
36
+ msg_count = 0
37
+ while !$stopped
38
+ msg_count += 1
39
+ obj = TestObject.new("Message ##{thread_count}-#{msg_count}")
40
+ session_pool.producer('address1') do |session, producer|
41
+ message = session.create_message(HornetQ::Client::Message::BYTES_TYPE, true)
42
+ message.body = Marshal.dump(obj)
43
+ message['format'] = 'ruby_marshal'
44
+ puts "Sending on address1 #{obj.inspect}"
45
+ producer.send(message)
46
+ end
47
+ # session_pool.send('address1', obj, :persistent => true, :format => :ruby_marshal)
48
+ sleep 1
49
+ end
50
+ end
51
+ end
52
+ (6..10).each do |i|
53
+ threads << Thread.new(i) do |thread_count|
54
+ msg_count = 0
55
+ while !$stopped
56
+ msg_count += 1
57
+ obj = {:thread => thread_count, :message => msg_count}
58
+ session_pool.producer('address2') do |session, producer|
59
+ message = session.create_message(HornetQ::Client::Message::TEXT_TYPE, false)
60
+ message.body = obj.to_json
61
+ message['format'] = 'json'
62
+ puts "Sending on address2 #{obj.inspect}"
63
+ producer.send(message)
64
+ end
65
+ # session_pool.send('address2', obj, :persistent => false, :format => :json)
66
+ sleep 2
67
+ end
68
+ end
69
+ end
70
+
71
+ threads.each { |thread| thread.join }
File without changes
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env jruby
2
+ #
3
+ # HornetQ Worker:
4
+ # Creates multiple threads for processing of messages.
5
+ #
6
+
7
+ # Allow examples to be run in-place without requiring a gem install
8
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
9
+
10
+ require 'rubygems'
11
+ require 'yaml'
12
+ require 'hornetq'
13
+ require 'test_object'
14
+ require 'json'
15
+
16
+ $config = YAML.load_file(File.dirname(__FILE__) + '/hornetq.yml')
17
+ $client_config = $config['client']
18
+ $session_config = $client_config[:session]
19
+ $connection = HornetQ::Client::Connection.new($client_config[:connection])
20
+
21
+ ['HUP', 'INT', 'TERM'].each do |signal_name|
22
+ Signal.trap(signal_name) do
23
+ puts "caught #{signal_name}"
24
+ $connection.close
25
+ end
26
+ end
27
+
28
+ $threads = []
29
+ def create_workers(address, queue, count, sleep_time, is_durable, &block)
30
+ (1..count).each do |i|
31
+ session = $connection.create_session($session_config)
32
+ puts "Creating queue address=#{address} queue=#{queue}" if i==1
33
+ session.create_queue_ignore_exists(address, queue, is_durable) if i == 1
34
+ $threads << Thread.new(i, session) do |thread_count, session|
35
+ prefix = "#{address}-#{queue}-#{thread_count}"
36
+ begin
37
+ consumer = session.create_consumer(queue)
38
+ session.start
39
+ puts "#{prefix} waiting for message"
40
+ while msg = consumer.receive
41
+ case msg['format']
42
+ when 'json'
43
+ object = JSON::Parser.new(msg.body).parse
44
+ when 'ruby_marshal'
45
+ object = Marshal.load(msg.body)
46
+ else
47
+ object = msg.body
48
+ end
49
+ puts "#{prefix} read #{object.inspect}"
50
+ sleep sleep_time
51
+ end
52
+ puts "#{prefix} end of thread"
53
+ rescue Java::org.hornetq.api.core.HornetQException => e
54
+ if e.cause.code != Java::org.hornetq.api.core.HornetQException::OBJECT_CLOSED
55
+ puts "#{prefix} HornetQException: #{e.message}\n#{e.backtrace.join("\n")}"
56
+ else
57
+ # Normal exit
58
+ end
59
+ rescue Exception => e
60
+ puts "#{prefix} Exception: #{e.message}\n#{e.backtrace.join("\n")}"
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ create_workers('address1', 'address1', 5, 1, false)
67
+ create_workers('address1', 'queue1_1', 5, 1, false)
68
+ create_workers('address1', 'queue1_2', 5, 2, true)
69
+ create_workers('address2', 'queue2_1', 5, 2, true)
70
+ create_workers('address2', 'queue2_2', 5, 1, false)
71
+
72
+ $threads.each { |thread| thread.join }
@@ -8,7 +8,7 @@ module HornetQ
8
8
  end
9
9
  end
10
10
 
11
- require 'hornetq/client/factory'
11
+ require 'hornetq/client/connection'
12
12
  require 'hornetq/common/org_hornetq_core_logging_logger'
13
13
  require 'hornetq/client/org_hornetq_api_core_client_client_session'
14
14
  require 'hornetq/client/org_hornetq_core_client_impl_client_message_impl'
@@ -1,69 +1,72 @@
1
1
  module HornetQ
2
2
  module Client
3
3
 
4
- class Factory
5
- # Create a new Factory and Session
4
+ class Connection
5
+ # Create a new Connection and Session
6
6
  #
7
- # Creates a new factory and session, then passes the session to the supplied
8
- # block. Upon completion the session and factory are both closed
9
- # See Factory::initialize and Factory::create_session for the list
7
+ # Creates a new connection and session, then passes the session to the supplied
8
+ # block. Upon completion the session and connection are both closed
9
+ # See Connection::initialize and Connection::create_session for the list
10
10
  # of parameters
11
+ #
12
+ # Returns result of block
11
13
  def self.session(params={},&proc)
12
14
  raise "Missing mandatory code block" unless proc
13
- factory = nil
15
+ connection = nil
14
16
  session = nil
15
17
  begin
16
18
  if params.kind_of?(String)
17
19
  # TODO: Support passing username and password from URI to Session
18
- factory = self.new(params)
19
- session = factory.session({}, &proc)
20
+ connection = self.new(params)
21
+ connection.session({}, &proc)
20
22
  else
21
- factory = self.new(params[:connector] || {})
22
- session = factory.session(params[:session] || {}, &proc)
23
+ connection = self.new(params[:connection] || {})
24
+ connection.session(params[:session] || {}, &proc)
23
25
  end
24
26
  ensure
25
- session.close if session
26
- factory.close if factory
27
+ connection.close if connection
27
28
  end
28
29
  end
29
30
 
30
- # Create a new Factory along with a Session, and then start the session
31
+ # Create a new Connection along with a Session, and then start the session
31
32
  #
32
- # Creates a new factory and session, then passes the session to the supplied
33
- # block. Upon completion the session and factory are both closed
34
- # See Factory::initialize and Factory::create_session for the list
33
+ # Creates a new connection and session, then passes the session to the supplied
34
+ # block. Upon completion the session and connection are both closed
35
+ # See Connection::initialize and Connection::create_session for the list
35
36
  # of parameters
36
- def self.start(params={},&proc)
37
+ #
38
+ # Returns result of block
39
+ def self.start_session(params={},&proc)
37
40
  session(params) do |session|
38
41
  session.start
39
42
  proc.call(session)
40
43
  end
41
44
  end
42
45
 
43
- # Call the supplied code block after creating a factory instance
46
+ # Call the supplied code block after creating a connection instance
44
47
  # See initialize for the parameter list
45
- # The factory is closed before returning
48
+ # The connection is closed before returning
46
49
  #
47
50
  # Returns the result of the code block
48
- def self.create_factory(params={}, &proc)
51
+ def self.connection(params={}, &proc)
49
52
  raise "Missing mandatory code block" unless proc
50
- factory = nil
53
+ connection = nil
51
54
  result = nil
52
55
  begin
53
- factory=self.new(params)
54
- result = proc.call(factory)
56
+ connection=self.new(params)
57
+ result = proc.call(connection)
55
58
  ensure
56
- factory.close
59
+ connection.close
57
60
  end
58
61
  result
59
62
  end
60
63
 
61
- # Create a new Factory from which sessions can be created
64
+ # Create a new Connection from which sessions can be created
62
65
  #
63
66
  # Parameters:
64
67
  # * a Hash consisting of one or more of the named parameters
65
68
  # * Summary of parameters and their default values
66
- # HornetQ::Client::Factory.new(
69
+ # HornetQ::Client::Connection.new(
67
70
  # :uri => 'hornetq://localhost',
68
71
  # :ack_batch_size => ,
69
72
  # :auto_group => ,
@@ -154,6 +157,7 @@ module HornetQ
154
157
  # * :use_global_pools
155
158
 
156
159
  def initialize(params={})
160
+ params =params.clone
157
161
  uri = nil
158
162
  # TODO: Support :uri as an array for cluster configurations
159
163
  if params.kind_of?(String)
@@ -166,43 +170,44 @@ module HornetQ
166
170
  params = uri.params.merge(params)
167
171
  end
168
172
 
169
- @factory = nil
173
+ @connection = nil
174
+ @sessions = []
175
+ @consumers = []
170
176
  # In-VM Transport has no fail-over or additional parameters
171
177
  if uri.host == 'invm'
172
178
  transport = Java::org.hornetq.api.core.TransportConfiguration.new(HornetQ::INVM_CONNECTOR_CLASS_NAME)
173
- @factory = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport)
179
+ @connection = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport)
174
180
  elsif params[:protocol]
175
181
  # Auto-Discovery just has a host name and port
176
182
  if params[:protocol] == 'discovery'
177
- @factory = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(uri.host, uri.port)
183
+ @connection = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(uri.host, uri.port)
178
184
  elsif params[:protocol] != 'netty'
179
185
  raise "Unknown HornetQ protocol:#{params[:protocol]}"
180
186
  end
181
187
  end
182
188
 
183
- # Unless already created, then the factory will use the netty protocol
184
- unless @factory
189
+ # Unless already created, then the connection will use the netty protocol
190
+ unless @connection
185
191
  # Primary Transport
186
192
  transport = Java::org.hornetq.api.core.TransportConfiguration.new(HornetQ::NETTY_CONNECTOR_CLASS_NAME, {'host' => uri.host, 'port' => uri.port })
187
193
 
188
194
  # Check for backup server connection information
189
195
  if uri.backup_host
190
196
  backup_transport = Java::org.hornetq.api.core.TransportConfiguration.new(HornetQ::NETTY_CONNECTOR_CLASS_NAME, {'host' => uri.backup_host, 'port' => uri.backup_port })
191
- @factory = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport, backup_transport)
197
+ @connection = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport, backup_transport)
192
198
  else
193
- @factory = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport)
199
+ @connection = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport)
194
200
  end
195
201
  end
196
202
 
197
- # If any other options were supplied, apply them to the created Factory instance
203
+ # If any other options were supplied, apply them to the created Connection instance
198
204
  params.each_pair do |key, val|
199
- next if key == :uri
200
205
  method = key.to_s+'='
201
- if @factory.respond_to? method
202
- @factory.send method, val
203
- #puts "Debug: #{key} = #{@factory.send key}" if @factory.respond_to? key.to_sym
206
+ if @connection.respond_to? method
207
+ @connection.send method, val
208
+ #puts "Debug: #{key} = #{@connection.send key}" if @connection.respond_to? key.to_sym
204
209
  else
205
- puts "Warning: Option:#{key}, with value:#{val} is invalid and being ignored"
210
+ HornetQ.logger.warn "Warning: Option:#{key}, with value:#{val} is invalid and being ignored"
206
211
  end
207
212
  end
208
213
  end
@@ -214,11 +219,11 @@ module HornetQ
214
219
  #
215
220
  # Note:
216
221
  # * The returned session MUST be closed once complete
217
- # factory = HornetQ::Client::Factory.new(:uri => 'hornetq://localhost/')
218
- # session = factory.create_session
222
+ # connection = HornetQ::Client::Connection.new(:uri => 'hornetq://localhost/')
223
+ # session = connection.create_session
219
224
  # ...
220
225
  # session.close
221
- # factory.close
226
+ # connection.close
222
227
  #
223
228
  # Returns:
224
229
  # * A new HornetQ ClientSession
@@ -231,11 +236,11 @@ module HornetQ
231
236
  # Example:
232
237
  # require 'hornetq'
233
238
  #
234
- # factory = nil
239
+ # connection = nil
235
240
  # session = nil
236
241
  # begin
237
- # factory = HornetQ::Client::Factory.new(:uri => 'hornetq://localhost/')
238
- # session = factory.create_session
242
+ # connection = HornetQ::Client::Connection.new(:uri => 'hornetq://localhost/')
243
+ # session = connection.create_session
239
244
  #
240
245
  # # Create a new queue
241
246
  # session.create_queue('Example', 'Example', true)
@@ -251,13 +256,13 @@ module HornetQ
251
256
  # producer.send(message)
252
257
  # ensure
253
258
  # session.close if session
254
- # factory.close if factory
259
+ # connection.close if connection
255
260
  # end
256
261
  #
257
262
  # Parameters:
258
263
  # * a Hash consisting of one or more of the named parameters
259
264
  # * Summary of parameters and their default values
260
- # factory.create_session(
265
+ # connection.create_session(
261
266
  # :username => 'my_username', # Default is no authentication
262
267
  # :password => 'password', # Default is no authentication
263
268
  # :xa => false,
@@ -300,9 +305,18 @@ module HornetQ
300
305
  # * :ack_batch_size
301
306
  # * the batch size of the acknowledgements
302
307
  #
308
+ # * :managed
309
+ # * true: The session will be managed by the connection. It will be
310
+ # closed when the connection is closed.
311
+ # Also the session will be started or stopped when
312
+ # Connection#start_managed_sessions or
313
+ # Connection#stop_managed_sessions is called
314
+ # * false: The caller is responsible for closing the session
315
+ # * Default: false
316
+ #
303
317
  def create_session(params={})
304
- raise "HornetQ::Client::Factory Already Closed" unless @factory
305
- @factory.create_session(
318
+ raise "HornetQ::Client::Connection Already Closed" unless @connection
319
+ session = @connection.create_session(
306
320
  params[:username],
307
321
  params[:password],
308
322
  params[:xa] || false,
@@ -310,6 +324,8 @@ module HornetQ
310
324
  params[:auto_commit_acks].nil? ? true : params[:auto_commit_acks],
311
325
  params[:pre_acknowledge] || false,
312
326
  params[:ack_batch_size] || 1)
327
+ (@sessions << session) if params.fetch(:managed, false)
328
+ session
313
329
  end
314
330
 
315
331
  # Create a session, call the supplied block and once it completes
@@ -318,34 +334,23 @@ module HornetQ
318
334
  #
319
335
  # Returns the result of the block
320
336
  #
321
- # Example
322
- # HornetQ::Client::Factory.create_session(:uri => 'hornetq://localhost/') do |session|
323
- # session.create_queue("Address", "Queue")
324
- # end
325
- #
326
337
  # Example:
327
338
  # require 'hornetq'
328
339
  #
329
- # factory = nil
330
- # begin
331
- # factory = HornetQ::Client::Factory.new(:uri => 'hornetq://localhost/')
332
- # factory.create_session do |session|
333
- #
334
- # # Create a new queue
335
- # session.create_queue('Example', 'Example', true)
340
+ # HornetQ::Client::Connection.connection('hornetq://localhost/') do |connection
341
+ # connection.session do |session|
336
342
  #
337
343
  # # Create a producer to send messages
338
- # producer = session.create_producer('Example')
344
+ # session.producer('Example') do |producer|
339
345
  #
340
- # # Create a Text Message
341
- # message = session.create_message(HornetQ::Client::Message::TEXT_TYPE,true)
342
- # message.body = 'Hello World'
346
+ # # Create a Text Message
347
+ # message = session.create_message(HornetQ::Client::Message::TEXT_TYPE,true)
348
+ # message.body = 'Hello World'
343
349
  #
344
- # # Send the message
345
- # producer.send(message)
350
+ # # Send the message
351
+ # producer.send(message)
352
+ # end
346
353
  # end
347
- # ensure
348
- # factory.close if factory
349
354
  # end
350
355
  def session(params={}, &proc)
351
356
  raise "HornetQ::Client::session mandatory block missing" unless proc
@@ -358,17 +363,54 @@ module HornetQ
358
363
  end
359
364
  end
360
365
 
366
+ # Create a session, start the session, call the supplied block
367
+ # and once the block completes close the session.
368
+ #
369
+ # See: #session_create for the Parameters
370
+ #
371
+ # Returns the result of the block
372
+ #
373
+ #
374
+ # Example:
375
+ # require 'hornetq'
376
+ #
377
+ # HornetQ::Client::Connection.connection('hornetq://localhost/') do |connection
378
+ # # Must start the session other we cannot consume messages using it
379
+ # connection.start_session do |session|
380
+ #
381
+ # # Create a consumer to receive messages
382
+ # session.consumer('TestQueue') do |consumer|
383
+ #
384
+ # consumer.each do |message|
385
+ # message.acknowledge
386
+ # end
387
+ #
388
+ # end
389
+ # end
390
+ # end
391
+ def start_session(params={}, &proc)
392
+ raise "HornetQ::Client::session mandatory block missing" unless proc
393
+ session = nil
394
+ begin
395
+ session = create_session(params)
396
+ session.start
397
+ proc.call(session)
398
+ ensure
399
+ session.close if session
400
+ end
401
+ end
402
+
361
403
  # Create a Session pool
362
- # TODO Raise an exception when gene_pool is not available
363
404
  def create_session_pool(params={})
364
405
  require 'hornetq/client/session_pool'
365
406
  SessionPool.new(self, params)
366
407
  end
367
408
 
368
- # Close Factory connections
409
+ # Close Connection connections
369
410
  def close
370
- @factory.close if @factory
371
- @factory = nil
411
+ @sessions.each { |session| session.close }
412
+ @connection.close if @connection
413
+ @connection = nil
372
414
  end
373
415
 
374
416
  # Receive messages in a separate thread when they arrive
@@ -392,13 +434,16 @@ module HornetQ
392
434
  # Default: 1
393
435
  #
394
436
  # Consumer Parameters:
395
- # :queue_name => Name of the Queue to read messages from
396
- #
397
- # :selector => Filter which messages should be returned from the queue
398
- # Default: All messages
399
- # :no_local => Determine whether messages published by its own connection
400
- # should be delivered to it
401
- # Default: false
437
+ # :queue_name => The name of the queue to consume messages from. Mandatory
438
+ # :filter => Only consume messages matching the filter: Default: nil
439
+ # :browse_only => Whether to just browse the queue or consume messages
440
+ # true | false. Default: false
441
+ # :window_size => The consumer window size.
442
+ # :max_rate => The maximum rate to consume messages.
443
+ # :auto_start => Immediately start processing messages.
444
+ # If set to false, call Connection#start_managed_sessions
445
+ # to manually start receive messages later
446
+ # Default: true
402
447
  #
403
448
  # :statistics Capture statistics on how many messages have been read
404
449
  # true : This method will capture statistics on the number of messages received
@@ -418,17 +463,18 @@ module HornetQ
418
463
  # Any Exception => The session is rolled back
419
464
  #
420
465
  # Notes:
421
- # * Remember to call ::start on the factory otherwise the on_message will not
466
+ # * Remember to call ::start on the connection otherwise the on_message will not
422
467
  # start consuming any messages
423
468
  # * Remember to call message.acknowledge before completing the block so that
424
469
  # the message will be removed from the queue
425
470
  # * If the block throws an exception, the
426
- def on_message(parms, &proc)
427
- consumer_count = parms[:session_count] || 1
471
+ def on_message(params, &proc)
472
+ consumer_count = params[:session_count] || 1
428
473
  consumer_count.times do
429
- session = self.create_session(parms)
430
- consumer = session.create_consumer_from_params(parms)
431
- consumer.on_message(parms, &proc)
474
+ session = self.create_session(params)
475
+ consumer = session.create_consumer_from_params(params)
476
+ consumer.on_message(params, &proc)
477
+ session.start if params.fetch(:auto_start, true)
432
478
  @consumers << consumer
433
479
  @sessions << session
434
480
  end
@@ -437,7 +483,28 @@ module HornetQ
437
483
  def on_message_statistics
438
484
  @consumers.collect{|consumer| consumer.on_message_statistics}
439
485
  end
486
+
487
+ # Start all sessions managed by this connection
488
+ #
489
+ # Sessions created via #create_session are not managed unless
490
+ # :auto_close => true was specified when the session was created
491
+ #
492
+ # Session are Only managed when created through the following methods:
493
+ # Connection#on_message
494
+ # Connection#create_session And :auto_close => true
495
+ #
496
+ # This call does not do anything to sessions in a session pool
497
+ def start_managed_sessions
498
+ @sessions.each {|session| session.start}
499
+ end
440
500
 
501
+ # Stop all sessions managed by this connection so that they no longer
502
+ # receive messages for processing
503
+ #
504
+ # See: #start_managed_sessions for details on which sessions are managed
505
+ def stop_managed_sessions
506
+ @sessions.each {|session| session.stop}
507
+ end
441
508
  end
442
509
  end
443
510
  end