jruby-hornetq 0.2.5.alpha → 0.3.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +12 -0
- data/Rakefile +13 -1
- data/bin/hornetq_server +2 -2
- data/examples/README +6 -0
- data/examples/{client/advanced → advanced}/batch_client.rb +18 -34
- data/examples/advanced/bytes_producer.rb +25 -0
- data/examples/advanced/client.rb +56 -0
- data/examples/{client/advanced/multi_client.rb → advanced/client_session_pooling.rb} +29 -25
- data/examples/advanced/consume_on_message.rb +50 -0
- data/examples/advanced/consumer.rb +31 -0
- data/examples/{client/advanced → advanced}/hornetq.yml +8 -6
- data/examples/advanced/multi_client.rb +71 -0
- data/examples/advanced/producer.rb +37 -0
- data/examples/advanced/server.rb +44 -0
- data/examples/client-server/client.rb +40 -0
- data/examples/client-server/server.rb +37 -0
- data/examples/producer-consumer/consume_all.rb +25 -0
- data/examples/producer-consumer/consume_on_message.rb +46 -0
- data/examples/producer-consumer/consumer.rb +27 -0
- data/examples/producer-consumer/producer.rb +32 -0
- data/examples/{client/resque → resque}/hornetq_job.rb +2 -2
- data/examples/{client/resque → resque}/processor.rb +1 -1
- data/examples/{client/resque → resque}/readme.md +0 -0
- data/examples/{client/resque → resque}/resque_conf.rb +0 -0
- data/examples/{client/resque → resque}/resque_worker.rb +0 -0
- data/examples/{client/resque → resque}/sleep_job.rb +0 -0
- data/examples/{server → server-config}/backup_server.yml +0 -0
- data/examples/{server → server-config}/live_server.yml +0 -0
- data/examples/{server → server-config}/standalone_server.yml +0 -0
- data/examples/{client → simple}/invm.rb +6 -7
- data/examples/{advanced/transaction-failover → transaction-failover}/README +0 -0
- data/examples/{advanced/transaction-failover → transaction-failover}/consumer.rb +4 -1
- data/examples/{advanced/transaction-failover → transaction-failover}/hornetq.yml +6 -3
- data/examples/{advanced/transaction-failover → transaction-failover}/producer.rb +8 -3
- data/examples/{simple_worker → worker}/README +0 -0
- data/examples/worker/hornetq.yml +26 -0
- data/examples/worker/producer.rb +71 -0
- data/examples/{simple_worker → worker}/test_object.rb +0 -0
- data/examples/worker/worker.rb +72 -0
- data/lib/hornetq/client.rb +1 -1
- data/lib/hornetq/client/{factory.rb → connection.rb} +153 -86
- data/lib/hornetq/client/message_handler.rb +3 -3
- data/lib/hornetq/client/org_hornetq_api_core_client_client_session.rb +187 -26
- data/lib/hornetq/client/org_hornetq_core_client_impl_client_consumer_impl.rb +26 -9
- data/lib/hornetq/client/org_hornetq_core_client_impl_client_message_impl.rb +190 -35
- data/lib/hornetq/client/session_pool.rb +4 -4
- data/lib/hornetq/common/log_delegate.rb +17 -15
- data/lib/hornetq/common/logging.rb +20 -7
- data/lib/hornetq/common/org_hornetq_core_logging_logger.rb +11 -9
- data/lib/hornetq/server.rb +86 -3
- data/lib/hornetq/{org_hornetq_core_server_hornet_q_server.rb → server/org_hornetq_core_server_hornet_q_server.rb} +1 -1
- data/lib/hornetq/uri.rb +4 -0
- data/test/client_connection_test.rb +158 -0
- data/test/logging_test.rb +32 -0
- data/test/server_test.rb +208 -0
- metadata +47 -48
- data/examples/client/advanced/bytes_producer.rb +0 -21
- data/examples/client/advanced/client.rb +0 -47
- data/examples/client/advanced/consumer.rb +0 -35
- data/examples/client/advanced/multi_consumer.rb +0 -54
- data/examples/client/advanced/producer.rb +0 -35
- data/examples/client/advanced/server.rb +0 -39
- data/examples/client/client.rb +0 -31
- data/examples/client/consumer.rb +0 -22
- data/examples/client/data/bindings/hornetq-bindings-1.bindings +0 -0
- data/examples/client/data/bindings/hornetq-bindings-2.bindings +0 -0
- data/examples/client/data/journal/hornetq-data-1.hq +0 -0
- data/examples/client/data/journal/hornetq-data-2.hq +0 -0
- data/examples/client/producer.rb +0 -21
- data/examples/client/server.rb +0 -31
- data/examples/simple_worker/hornetq.yml +0 -34
- data/examples/simple_worker/producer.rb +0 -54
- data/examples/simple_worker/worker.rb +0 -49
- data/lib/data/bindings/hornetq-bindings-1.bindings +0 -0
- data/lib/data/bindings/hornetq-bindings-2.bindings +0 -0
- data/lib/hornetq/server/factory.rb +0 -87
- data/test/server_factory_test.rb +0 -183
File without changes
|
@@ -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::
|
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
|
-
:
|
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
|
-
#
|
20
|
-
:reconnect_attempts:
|
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::
|
17
|
-
session.
|
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
|
-
|
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 }
|
data/lib/hornetq/client.rb
CHANGED
@@ -8,7 +8,7 @@ module HornetQ
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
require 'hornetq/client/
|
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
|
5
|
-
# Create a new
|
4
|
+
class Connection
|
5
|
+
# Create a new Connection and Session
|
6
6
|
#
|
7
|
-
# Creates a new
|
8
|
-
# block. Upon completion the session and
|
9
|
-
# See
|
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
|
-
|
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
|
-
|
19
|
-
|
20
|
+
connection = self.new(params)
|
21
|
+
connection.session({}, &proc)
|
20
22
|
else
|
21
|
-
|
22
|
-
|
23
|
+
connection = self.new(params[:connection] || {})
|
24
|
+
connection.session(params[:session] || {}, &proc)
|
23
25
|
end
|
24
26
|
ensure
|
25
|
-
|
26
|
-
factory.close if factory
|
27
|
+
connection.close if connection
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
# Create a new
|
31
|
+
# Create a new Connection along with a Session, and then start the session
|
31
32
|
#
|
32
|
-
# Creates a new
|
33
|
-
# block. Upon completion the session and
|
34
|
-
# See
|
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
|
-
|
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
|
46
|
+
# Call the supplied code block after creating a connection instance
|
44
47
|
# See initialize for the parameter list
|
45
|
-
# The
|
48
|
+
# The connection is closed before returning
|
46
49
|
#
|
47
50
|
# Returns the result of the code block
|
48
|
-
def self.
|
51
|
+
def self.connection(params={}, &proc)
|
49
52
|
raise "Missing mandatory code block" unless proc
|
50
|
-
|
53
|
+
connection = nil
|
51
54
|
result = nil
|
52
55
|
begin
|
53
|
-
|
54
|
-
result = proc.call(
|
56
|
+
connection=self.new(params)
|
57
|
+
result = proc.call(connection)
|
55
58
|
ensure
|
56
|
-
|
59
|
+
connection.close
|
57
60
|
end
|
58
61
|
result
|
59
62
|
end
|
60
63
|
|
61
|
-
# Create a new
|
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::
|
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
|
-
@
|
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
|
-
@
|
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
|
-
@
|
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
|
184
|
-
unless @
|
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
|
-
@
|
197
|
+
@connection = Java::org.hornetq.api.core.client.HornetQClient.create_client_session_factory(transport, backup_transport)
|
192
198
|
else
|
193
|
-
@
|
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
|
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 @
|
202
|
-
@
|
203
|
-
#puts "Debug: #{key} = #{@
|
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
|
-
|
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
|
-
#
|
218
|
-
# session =
|
222
|
+
# connection = HornetQ::Client::Connection.new(:uri => 'hornetq://localhost/')
|
223
|
+
# session = connection.create_session
|
219
224
|
# ...
|
220
225
|
# session.close
|
221
|
-
#
|
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
|
-
#
|
239
|
+
# connection = nil
|
235
240
|
# session = nil
|
236
241
|
# begin
|
237
|
-
#
|
238
|
-
# 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
|
-
#
|
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
|
-
#
|
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::
|
305
|
-
@
|
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
|
-
#
|
330
|
-
#
|
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
|
-
#
|
344
|
+
# session.producer('Example') do |producer|
|
339
345
|
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#
|
346
|
+
# # Create a Text Message
|
347
|
+
# message = session.create_message(HornetQ::Client::Message::TEXT_TYPE,true)
|
348
|
+
# message.body = 'Hello World'
|
343
349
|
#
|
344
|
-
#
|
345
|
-
#
|
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
|
409
|
+
# Close Connection connections
|
369
410
|
def close
|
370
|
-
@
|
371
|
-
@
|
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
|
396
|
-
#
|
397
|
-
# :
|
398
|
-
#
|
399
|
-
# :
|
400
|
-
#
|
401
|
-
#
|
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
|
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(
|
427
|
-
consumer_count =
|
471
|
+
def on_message(params, &proc)
|
472
|
+
consumer_count = params[:session_count] || 1
|
428
473
|
consumer_count.times do
|
429
|
-
session = self.create_session(
|
430
|
-
consumer = session.create_consumer_from_params(
|
431
|
-
consumer.on_message(
|
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
|