jruby-jms 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ ################################################################################
2
+ # Copyright 2008, 2009, 2010, 2011 J. Reid Morrison
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #Interface javax.jms.ObjectMessage
18
+ module javax.jms::ObjectMessage
19
+ def data
20
+ getObject
21
+ end
22
+
23
+ def data(val)
24
+ setObject(val)
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ ################################################################################
2
+ # Copyright 2008, 2009, 2010, 2011 J. Reid Morrison
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ module javax.jms::QueueBrowser
18
+ # For each message on the queue call the supplied Proc
19
+ def each(parms={}, &proc)
20
+ raise "javax.jms.QueueBrowser::each requires a code block to be executed for each message received" unless proc
21
+
22
+ e = self.getEnumeration
23
+ while e.hasMoreElements
24
+ proc.call(e.nextElement)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,285 @@
1
+ ################################################################################
2
+ # Copyright 2008, 2009, 2010, 2011 J. Reid Morrison
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ # For each thread that will be processing messages concurrently a separate
18
+ # session is required. All sessions can share a single connection to the same
19
+ # JMS Provider.
20
+ #
21
+ # Interface javax.jms.Session
22
+ module javax.jms::Session
23
+ # Create a new message instance based on the type of the data being supplied
24
+ # String (:to_str) => TextMessage
25
+ # Hash (:each_pair) => MapMessage
26
+ # In fact duck typing is used to determine the type. If the class responds
27
+ # to :to_str then it is considered a String. Similarly if it responds to
28
+ # :each_pair it is considered to be a Hash
29
+ def message(data)
30
+ jms_message = nil
31
+ if data.respond_to?(:to_str, false)
32
+ jms_message = self.createTextMessage
33
+ jms_message.text = data.to_str
34
+ elsif data.respond_to?(:each_pair, false)
35
+ jms_message = self.createMapMessage
36
+ jms_message.data = data
37
+ else
38
+ raise "Unknown data type #{data.class.to_s} in Message"
39
+ end
40
+ jms_message
41
+ end
42
+
43
+ # Does the session support transactions?
44
+ # I.e. Can/should commit and rollback be called
45
+ def transacted?
46
+ self.getAcknowledgeMode == javax.jms.Session::SESSION_TRANSACTED
47
+ end
48
+
49
+ # Create and open a queue to put or get from. Once the supplied Proc is complete
50
+ # the queue is automatically closed. If no Proc is supplied then the queue must
51
+ # be explicitly closed by the caller.
52
+ def destination(parms={}, &proc)
53
+ parms[:jms_session] = self
54
+ q = JMS::Destination.new(parms)
55
+ q.open(parms)
56
+ if proc
57
+ begin
58
+ proc.call(q)
59
+ ensure
60
+ q.close
61
+ q = nil
62
+ end
63
+ end
64
+ q
65
+ end
66
+
67
+ # Return the queue matching the queue name supplied
68
+ # Call the Proc if supplied
69
+ def queue(q_name, &proc)
70
+ q = create_queue(q_name)
71
+ if proc
72
+ begin
73
+ proc.call(q)
74
+ ensure
75
+ q = nil
76
+ end
77
+ end
78
+ q
79
+ end
80
+
81
+ # Return a producer for the queue name supplied
82
+ # A producer supports sending messages to a Queue or a Topic
83
+ #
84
+ # Call the Proc if supplied, then automatically close the producer
85
+ #
86
+ # Parameters:
87
+ # :q_name => String: Name of the Queue to return
88
+ # Symbol: :temporary => Create temporary queue
89
+ # Mandatory unless :topic_name is supplied
90
+ # Or,
91
+ # :topic_name => String: Name of the Topic to write to or subscribe to
92
+ # Symbol: :temporary => Create temporary topic
93
+ # Mandatory unless :q_name is supplied
94
+ # Or,
95
+ # :destination=> Explicit javax.jms::Destination to use
96
+ def producer(parms, &proc)
97
+ destination = create_destination(parms)
98
+ # Call original java method with this destination
99
+ #p = java_send :create_producer, [javax.jms::Destination], destination
100
+ p = create_producer(destination)
101
+ if proc
102
+ begin
103
+ proc.call(p)
104
+ ensure
105
+ p.close
106
+ p = nil
107
+ end
108
+ end
109
+ p
110
+ end
111
+
112
+ # Return a consumer for the destination
113
+ # A consumer can read messages from the queue or topic
114
+ #
115
+ # Call the Proc if supplied, then automatically close the consumer
116
+ #
117
+ # Parameters:
118
+ # :q_name => String: Name of the Queue to return
119
+ # Symbol: :temporary => Create temporary queue
120
+ # Mandatory unless :topic_name is supplied
121
+ # Or,
122
+ # :topic_name => String: Name of the Topic to write to or subscribe to
123
+ # Symbol: :temporary => Create temporary topic
124
+ # Mandatory unless :q_name is supplied
125
+ # Or,
126
+ # :destination=> Explicit javaxJms::Destination to use
127
+ #
128
+ # :selector => Filter which messages should be returned from the queue
129
+ # Default: All messages
130
+ # :no_local => Determine whether messages published by its own connection
131
+ # should be delivered to it
132
+ # Default: false
133
+ def consumer(parms, &proc)
134
+ destination = create_destination(parms)
135
+ c = nil
136
+ if parms[:no_local]
137
+ c = create_consumer(destination, parms[:selector] || '', parms[:no_local])
138
+ elsif parms[:selector]
139
+ c = create_consumer(destination, parms[:selector])
140
+ else
141
+ c = create_consumer(destination)
142
+ end
143
+
144
+ if proc
145
+ begin
146
+ proc.call(c)
147
+ ensure
148
+ c.close
149
+ c = nil
150
+ end
151
+ end
152
+ c
153
+ end
154
+
155
+ # Consume all messages for the destination
156
+ # A consumer can read messages from the queue or topic
157
+ #
158
+ # Parameters:
159
+ # :q_name => String: Name of the Queue to return
160
+ # Symbol: :temporary => Create temporary queue
161
+ # Mandatory unless :topic_name is supplied
162
+ # Or,
163
+ # :topic_name => String: Name of the Topic to write to or subscribe to
164
+ # Symbol: :temporary => Create temporary topic
165
+ # Mandatory unless :q_name is supplied
166
+ # Or,
167
+ # :destination=> Explicit javaxJms::Destination to use
168
+ #
169
+ # :selector => Filter which messages should be returned from the queue
170
+ # Default: All messages
171
+ # :no_local => Determine whether messages published by its own connection
172
+ # should be delivered to it
173
+ # Default: false
174
+ #
175
+ # :timeout Follows the rules for MQSeries:
176
+ # -1 : Wait forever
177
+ # 0 : Return immediately if no message is available
178
+ # x : Wait for x milli-seconds for a message to be received from the broker
179
+ # Note: Messages may still be on the queue, but the broker has not supplied any messages
180
+ # in the time interval specified
181
+ # Default: 0
182
+ #
183
+ def consume(parms, &proc)
184
+ c = self.consumer(parms)
185
+ begin
186
+ c.each(parms, &proc)
187
+ ensure
188
+ c.close
189
+ end
190
+ end
191
+
192
+ # Return a browser for the destination
193
+ # A browser can read messages non-destructively from the queue
194
+ # It cannot browse Topics!
195
+ #
196
+ # Call the Proc if supplied, then automatically close the consumer
197
+ #
198
+ # Parameters:
199
+ # :q_name => String: Name of the Queue to return
200
+ # Symbol: :temporary => Create temporary queue
201
+ # Mandatory unless :topic_name is supplied
202
+ # Or,
203
+ # :destination=> Explicit javaxJms::Destination to use
204
+ #
205
+ # :selector => Filter which messages should be returned from the queue
206
+ # Default: All messages
207
+ def browser(parms, &proc)
208
+ raise "Session::browser requires a code block to be executed" unless proc
209
+
210
+ destination = create_destination(parms)
211
+ b = nil
212
+ if parms[:selector]
213
+ b = create_browser(destination, parms[:selector])
214
+ else
215
+ b = create_browser(destination)
216
+ end
217
+
218
+ if proc
219
+ begin
220
+ proc.call(b)
221
+ ensure
222
+ b.close
223
+ b = nil
224
+ end
225
+ end
226
+ b
227
+ end
228
+
229
+ # Browse the specified queue, calling the Proc supplied for each message found
230
+ #
231
+ # Parameters:
232
+ # :q_name => String: Name of the Queue to return
233
+ # Symbol: :temporary => Create temporary queue
234
+ # Mandatory unless :topic_name is supplied
235
+ # Or,
236
+ # :destination=> Explicit javaxJms::Destination to use
237
+ #
238
+ # :selector => Filter which messages should be returned from the queue
239
+ # Default: All messages
240
+ def browse(parms={}, &proc)
241
+ self.browser(parms) {|b| b.each(parms, &proc)}
242
+ end
243
+
244
+ private
245
+ # Create the destination based on the parameter supplied
246
+ #
247
+ # Parameters:
248
+ # :q_name => String: Name of the Queue to return
249
+ # Symbol: :temporary => Create temporary queue
250
+ # Mandatory unless :topic_name is supplied
251
+ # Or,
252
+ # :topic_name => String: Name of the Topic to write to or subscribe to
253
+ # Symbol: :temporary => Create temporary topic
254
+ # Mandatory unless :q_name is supplied
255
+ # Or,
256
+ # :destination=> Explicit javaxJms::Destination to use
257
+ def create_destination(parms)
258
+ return parms[:destination] if parms[:destination] && parms[:destination].kind_of?(javaxJms::Destination)
259
+ q_name = parms[:q_name]
260
+ topic_name = parms[:topic_name]
261
+ raise "Missing mandatory parameter :q_name or :topic_name to Session::producer, Session::consumer, or Session::browser" unless q_name || topic_name
262
+
263
+ if q_name
264
+ q_name == :temporary ? create_temporary_queue : create_queue(q_name)
265
+ else
266
+ topic_name == :temporary ? create_temporary_topic : create_topic(topic_name)
267
+ end
268
+ end
269
+ end
270
+
271
+ # Workaround for IBM MQ JMS implementation that implements an undocumented consume method
272
+ if defined? com.ibm.mq.jms::MQSession
273
+ class com.ibm.mq.jms::MQSession
274
+ def consume(parms, &proc)
275
+ result = nil
276
+ c = self.consumer(parms)
277
+ begin
278
+ result = c.each(parms, &proc)
279
+ ensure
280
+ c.close
281
+ end
282
+ result
283
+ end
284
+ end
285
+ end
@@ -0,0 +1,31 @@
1
+ ################################################################################
2
+ # Copyright 2008, 2009, 2010, 2011 J. Reid Morrison
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ################################################################################
16
+
17
+ #Interface javax.jms.TextMessage
18
+ module javax.jms::TextMessage
19
+ def data
20
+ getText
21
+ end
22
+
23
+ def data=(val)
24
+ setText(val.to_s)
25
+ end
26
+
27
+ def to_s
28
+ data
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jruby-jms
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.9.0
6
+ platform: ruby
7
+ authors:
8
+ - Reid Morrison
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-01-24 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: JRuby-JMS is a Java and Ruby library that exposes the Java JMS API in a ruby friendly way. For JRuby only.
18
+ email: rubywmq@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - HISTORY.md
27
+ - LICENSE.txt
28
+ - Rakefile
29
+ - README.md
30
+ - examples/consumer.rb
31
+ - examples/jms.yml
32
+ - examples/log4j.properties
33
+ - examples/producer.rb
34
+ - examples/performance/consumer.rb
35
+ - examples/performance/producer.rb
36
+ - lib/jms.rb
37
+ - lib/jms/connection.rb
38
+ - lib/jms/javax_jms_map_message.rb
39
+ - lib/jms/javax_jms_message.rb
40
+ - lib/jms/javax_jms_message_consumer.rb
41
+ - lib/jms/javax_jms_object_message.rb
42
+ - lib/jms/javax_jms_queue_browser.rb
43
+ - lib/jms/javax_jms_session.rb
44
+ - lib/jms/javax_jms_text_message.rb
45
+ has_rdoc: true
46
+ homepage: https://github.com/reidmorrison/jruby-jms
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.4.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: JRuby interface into JMS
73
+ test_files: []
74
+