jruby-hornetq 0.2.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,175 @@
1
+ require 'gene_pool'
2
+
3
+ module HornetQClient
4
+ # Since a Session can only be used by one thread at a time, we could create
5
+ # a Session for every thread. That could result in excessive unused Sessions.
6
+ # An alternative is to create a pool of sessions that can be shared by
7
+ # multiple threads.
8
+ #
9
+ # Each thread can requests a session and then return it once it is no longer
10
+ # needed by that thread. Rather than requesting a session directly using
11
+ # SessionPool::session, it is recommended to pass a block so that the Session
12
+ # is automatically returned to the pool. For example:
13
+ # session_pool.session do |session|
14
+ # .. do something with the session here
15
+ # end
16
+ #
17
+ # Parameters:
18
+ # see regular session parameters from: HornetQClient::Factory::create_session
19
+ #
20
+ # Additional parameters for controlling the session pool itself
21
+ # :pool_name Name of the pool as it shows up in the logger.
22
+ # Default: 'HornetQClient::SessionPool'
23
+ # :pool_size Maximum Pool Size. Default: 10
24
+ # The pool only grows as needed and will never exceed
25
+ # :pool_size
26
+ # :pool_warn_timeout Amount of time to wait before logging a warning when a
27
+ # session in the pool is not available. Measured in seconds
28
+ # Default: 5.0
29
+ # :pool_logger Supply a logger that responds to #debug, #info, #warn and #debug?
30
+ # For example: Rails.logger
31
+ # Default: None.
32
+ # Example:
33
+ # session_pool = factory.create_session_pool(config)
34
+ # session_pool.session do |session|
35
+ # ....
36
+ # end
37
+ class SessionPool
38
+ def initialize(factory, parms={})
39
+ # Save Session parms since it will be used every time a new session is
40
+ # created in the pool
41
+ session_parms = parms.dup
42
+ # TODO Use same logger as HornetQ?
43
+ # TODO How to shrink unused connections?
44
+ @pool = GenePool.new(
45
+ :name => parms[:pool_name] || self.class.name,
46
+ :pool_size => parms[:pool_size] || 10,
47
+ :warn_timeout => parms[:pool_warn_timeout] || 5,
48
+ :logger => parms[:pool_logger]) do
49
+ s = factory.create_session(session_parms)
50
+ # Start the session since it will be used immediately upon creation
51
+ s.start
52
+ puts "Creating Session"
53
+ s
54
+ end
55
+
56
+ # Obtain a session from the pool and pass it to the supplied block
57
+ # The session is automatically returned to the pool once the block completes
58
+ def session(&block)
59
+ @pool.with_connection {|s| block.call(s)}
60
+ end
61
+
62
+ # Obtain a session from the pool and create a ClientConsumer.
63
+ # Pass both into the supplied block.
64
+ # Once the block is complete the consumer is closed and the session is
65
+ # returned to the pool.
66
+ #
67
+ # See HornetQClient::ClientConsumer for more information on the consumer
68
+ # parameters
69
+ #
70
+ # Example
71
+ # session_pool.consumer('MyQueue') do |session, consumer|
72
+ # message = consumer.receive(timeout)
73
+ # puts message.body if message
74
+ # end
75
+ def consumer(queue_name, &block)
76
+ session do |s|
77
+ consumer = nil
78
+ begin
79
+ consumer = s.create_consumer(queue_name)
80
+ block.call(s, consumer)
81
+ ensure
82
+ consumer.close
83
+ end
84
+ end
85
+ end
86
+
87
+ # Obtain a session from the pool and create a ClientProducer.
88
+ # Pass both into the supplied block.
89
+ # Once the block is complete the consumer is closed and the session is
90
+ # returned to the pool.
91
+ #
92
+ # See HornetQClient::ClientProducer for more information on the producer
93
+ # parameters
94
+ #
95
+ # Example
96
+ # session_pool.producer('MyAddress') do |session, producer|
97
+ # message = session.create_message(HornetQClient::Message::TEXT_TYPE,false)
98
+ # message << "#{Time.now}: ### Hello, World ###"
99
+ # producer.send(message)
100
+ # end
101
+ def producer(address, &block)
102
+ session do |s|
103
+ producer = nil
104
+ begin
105
+ producer = s.create_producer(address)
106
+ block.call(s, producer)
107
+ ensure
108
+ producer.close
109
+ end
110
+ end
111
+ end
112
+
113
+ # Obtain a session from the pool and create a ClientRequestor.
114
+ # Pass both into the supplied block.
115
+ # Once the block is complete the requestor is closed and the session is
116
+ # returned to the pool.
117
+ #
118
+ # See HornetQClient::ClientRequestor for more information on the requestor
119
+ #
120
+ # Example
121
+ # session_pool.requestor(parms) do |session, requestor|
122
+ # ....
123
+ # end
124
+ def requestor(address, &block)
125
+ session do |s|
126
+ requestor = nil
127
+ begin
128
+ requestor = s.create_requestor(address)
129
+ block.call(s, requestor)
130
+ ensure
131
+ requestor.close
132
+ end
133
+ end
134
+ end
135
+
136
+ # Obtain a session from the pool and create a ClientServer.
137
+ # Pass both into the supplied block.
138
+ # Once the block is complete the requestor is closed and the session is
139
+ # returned to the pool.
140
+ #
141
+ # See HornetQClient::ClientServer for more information on the server
142
+ #
143
+ # Example
144
+ # session_pool.server(queue, timeout) do |session, server|
145
+ # ....
146
+ # end
147
+ def server(queue, timeout=0, &block)
148
+ session do |s|
149
+ server = nil
150
+ begin
151
+ server = s.create_server(queue, timeout=0)
152
+ block.call(s, server)
153
+ ensure
154
+ server.close
155
+ end
156
+ end
157
+ end
158
+
159
+ # Immediately Close all sessions in the pool and release from the pool
160
+ #
161
+ # TODO: Allow an option to wait for active sessions to be returned before
162
+ # closing
163
+ def close
164
+ @pool.each do |s|
165
+ #@pool.remove(s)
166
+ s.close
167
+ #@pool.remove(s)
168
+ end
169
+ end
170
+
171
+ end
172
+
173
+ end
174
+
175
+ end
data/lib/hornetq.rb ADDED
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/hornetq/lib/hornetq-core-client.jar'
2
+ require File.dirname(__FILE__) + '/hornetq/lib/netty.jar'
3
+ require 'hornetq/org_hornetq_api_core_client_client_session'
4
+ require 'hornetq/org_hornetq_core_client_impl_client_message_impl'
5
+ require 'hornetq/org_hornetq_utils_typed_properties'
6
+ require 'hornetq/hornet_q_client'
7
+ require 'hornetq/client_requestor'
8
+ require 'hornetq/client_server'
9
+ require 'hornetq/session_pool'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jruby-hornetq
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ - alpha
10
+ version: 0.2.0.alpha
11
+ platform: ruby
12
+ authors:
13
+ - Reid Morrison
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-19 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: gene_pool
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 1
31
+ - 1
32
+ version: 1.1.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: JRuby-HornetQ is a Java and Ruby library that exposes the HornetQ Java API in a ruby friendly way. For JRuby only.
36
+ email: rubywmq@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - HISTORY.md
45
+ - LICENSE.txt
46
+ - Rakefile
47
+ - README.md
48
+ - bin/hornetq_server
49
+ - examples/backup_server.yml
50
+ - examples/batch_client.rb
51
+ - examples/client.rb
52
+ - examples/consumer.rb
53
+ - examples/hornetq.yml
54
+ - examples/live_server.yml
55
+ - examples/multi_client.rb
56
+ - examples/multi_consumer.rb
57
+ - examples/producer.rb
58
+ - examples/run_broker.rb
59
+ - examples/server.rb
60
+ - examples/standalone_server.yml
61
+ - lib/hornetq.rb
62
+ - lib/hornetq/client_requestor.rb
63
+ - lib/hornetq/client_server.rb
64
+ - lib/hornetq/hornet_q_client.rb
65
+ - lib/hornetq/org_hornetq_api_core_client_client_session.rb
66
+ - lib/hornetq/org_hornetq_core_client_impl_client_message_impl.rb
67
+ - lib/hornetq/org_hornetq_core_client_impl_client_producer_impl.rb
68
+ - lib/hornetq/org_hornetq_utils_typed_properties.rb
69
+ - lib/hornetq/session_pool.rb
70
+ - lib/hornetq/lib/hornetq-core-client.jar
71
+ - lib/hornetq/lib/hornetq-core.jar
72
+ - lib/hornetq/lib/netty.jar
73
+ has_rdoc: true
74
+ homepage: http://www.reidmorrison.com/
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">"
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 1
95
+ - 3
96
+ - 1
97
+ version: 1.3.1
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: JRuby interface into HornetQ
105
+ test_files: []
106
+