torquebox-messaging 1.0.0.CR1-java
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acl-spi-3.0.0.CR2.jar +0 -0
- data/lib/activation-1.1.jar +0 -0
- data/lib/authorization-spi-3.0.0.CR2.jar +0 -0
- data/lib/dtdparser-1.21.jar +0 -0
- data/lib/gem_hook.rb +22 -0
- data/lib/hornetq-core-2.1.2.Final.jar +0 -0
- data/lib/hornetq-jms-client-2.1.2.Final.jar +0 -0
- data/lib/hornetq-logging-2.1.2.Final.jar +0 -0
- data/lib/identity-spi-3.0.0.CR2.jar +0 -0
- data/lib/jaxb-api-2.1.9.jar +0 -0
- data/lib/jboss-common-core-2.2.17.GA.jar +0 -0
- data/lib/jboss-jms-api_1.1_spec-1.0.0.Final.jar +0 -0
- data/lib/jboss-logging-3.0.0.Beta4.jar +0 -0
- data/lib/jboss-reflect-2.2.0.GA.jar +0 -0
- data/lib/jbosssx-bare-3.0.0.CR2.jar +0 -0
- data/lib/jbossxacml-2.0.5.CR1.jar +0 -0
- data/lib/jbossxb-2.0.3.GA.jar +0 -0
- data/lib/jnp-client-5.0.5.Final.jar +0 -0
- data/lib/jruby-complete-1.6.1.jar +0 -0
- data/lib/netty-3.2.1.Final.jar +0 -0
- data/lib/org.torquebox.messaging-client.rb +20 -0
- data/lib/picketbox-acl-impl-3.0.0.CR2.jar +0 -0
- data/lib/picketbox-bare-3.0.0.CR2.jar +0 -0
- data/lib/picketbox-identity-impl-3.0.0.CR2.jar +0 -0
- data/lib/picketbox-spi-bare-3.0.0.CR2.jar +0 -0
- data/lib/torquebox/messaging/backgroundable.rb +134 -0
- data/lib/torquebox/messaging/client.rb +74 -0
- data/lib/torquebox/messaging/destination.rb +173 -0
- data/lib/torquebox/messaging/ext/javax_jms_queue_browser.rb +27 -0
- data/lib/torquebox/messaging/ext/javax_jms_session.rb +142 -0
- data/lib/torquebox/messaging/task.rb +46 -0
- data/lib/torquebox/messaging.rb +6 -0
- data/lib/torquebox-base-core.jar +0 -0
- data/lib/torquebox-base-metadata.jar +0 -0
- data/lib/torquebox-base-spi.jar +0 -0
- data/lib/torquebox-mc-support.jar +0 -0
- data/lib/torquebox-messaging-core.jar +0 -0
- data/lib/torquebox-messaging.jar +0 -0
- data/lib/torquebox-messaging.rb +43 -0
- data/lib/xercesImpl-2.9.1.jar +0 -0
- data/lib/xml-apis-1.3.04.jar +0 -0
- data/licenses/lgpl-2.1.txt +504 -0
- data/spec/backgroundable_spec.rb +141 -0
- data/spec/client_spec.rb +119 -0
- data/spec/destination_spec.rb +296 -0
- data/spec/dispatcher-queues.yml +4 -0
- data/spec/dispatcher_not_running.rb +54 -0
- data/spec/ext/java_jmx_session_spec.rb +71 -0
- data/spec/queues.yml +6 -0
- data/spec/task_spec.rb +66 -0
- metadata +163 -0
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/lib/gem_hook.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
require 'torquebox/messaging/client'
|
19
|
+
require 'torquebox/messaging/task'
|
20
|
+
require 'torquebox/messaging/destination'
|
21
|
+
require 'torquebox/messaging/message_processor'
|
22
|
+
require 'torquebox/messaging/backgroundable'
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
# Backwards-compatibility
|
19
|
+
$stderr.puts "Usage of 'org.torquebox.messaging-client' is deprecated. Please use 'torquebox-messaging'."
|
20
|
+
require 'torquebox-messaging'
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
require 'torquebox/messaging/destination'
|
19
|
+
|
20
|
+
module TorqueBox
|
21
|
+
module Messaging
|
22
|
+
module Backgroundable
|
23
|
+
def self.included(base)
|
24
|
+
base.extend(ClassMethods)
|
25
|
+
end
|
26
|
+
|
27
|
+
def background(options = { })
|
28
|
+
BackgroundProxy.new(self, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
|
33
|
+
def always_background(*methods)
|
34
|
+
options = methods.last.is_a?(Hash) ? methods.pop : {}
|
35
|
+
@__backgroundable_methods ||= {}
|
36
|
+
|
37
|
+
methods.each do |method|
|
38
|
+
method = method.to_s
|
39
|
+
if !@__backgroundable_methods[method]
|
40
|
+
@__backgroundable_methods[method] ||= { }
|
41
|
+
@__backgroundable_methods[method][:options] = options
|
42
|
+
if Util.instance_methods_include?(self, method) ||
|
43
|
+
Util.private_instance_methods_include?(self, method)
|
44
|
+
__enable_backgrounding(method)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def method_added(method)
|
51
|
+
method = method.to_s
|
52
|
+
if @__backgroundable_methods &&
|
53
|
+
@__backgroundable_methods[method] &&
|
54
|
+
!@__backgroundable_methods[method][:backgrounding]
|
55
|
+
__enable_backgrounding(method)
|
56
|
+
else
|
57
|
+
super
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def __enable_backgrounding(method)
|
63
|
+
privatize = Util.private_instance_methods_include?(self, method)
|
64
|
+
protect = Util.protected_instance_methods_include?(self, method) unless privatize
|
65
|
+
async_method = "__async_#{method}"
|
66
|
+
sync_method = "__sync_#{method}"
|
67
|
+
|
68
|
+
@__backgroundable_methods[method][:backgrounding] = true
|
69
|
+
options = @__backgroundable_methods[method][:options]
|
70
|
+
class_eval do
|
71
|
+
|
72
|
+
define_method async_method do |*args|
|
73
|
+
Util.publish_message(self, sync_method, args, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
alias_method sync_method, method
|
77
|
+
alias_method method, async_method
|
78
|
+
|
79
|
+
if privatize || protect
|
80
|
+
send((privatize ? :private : :protected), method, sync_method, async_method)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
ensure
|
84
|
+
@__backgroundable_methods[method][:backgrounding] = nil
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
class BackgroundProxy
|
90
|
+
def initialize(receiver, options)
|
91
|
+
@receiver = receiver
|
92
|
+
@options = options
|
93
|
+
end
|
94
|
+
|
95
|
+
def method_missing(method, *args, &block)
|
96
|
+
@receiver.method_missing(method, *args, &block) unless @receiver.respond_to?(method)
|
97
|
+
raise ArgumentError.new("Backgrounding a method with a block argument is not supported. If you need this feature, please file a feature request at http://issues.jboss.org/browse/TORQUE") if block_given?
|
98
|
+
Util.publish_message(@receiver, method, args, @options)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
module Util
|
103
|
+
QUEUE_NAME = "/queues/torquebox/#{ENV['TORQUEBOX_APP_NAME']}/backgroundable"
|
104
|
+
|
105
|
+
class << self
|
106
|
+
def publish_message(receiver, method, args, options = { })
|
107
|
+
Queue.new(QUEUE_NAME).publish({:receiver => receiver, :method => method, :args => args}, options)
|
108
|
+
rescue javax.naming.NameNotFoundException => ex
|
109
|
+
raise RuntimeError.new("The backgroundable queue is not available. Did you disable it by setting its concurrency to 0?")
|
110
|
+
end
|
111
|
+
|
112
|
+
def instance_methods_include?(klass, method)
|
113
|
+
methods_include?(klass.instance_methods, method)
|
114
|
+
end
|
115
|
+
|
116
|
+
def private_instance_methods_include?(klass, method)
|
117
|
+
methods_include?(klass.private_instance_methods, method)
|
118
|
+
end
|
119
|
+
|
120
|
+
def protected_instance_methods_include?(klass, method)
|
121
|
+
methods_include?(klass.protected_instance_methods, method)
|
122
|
+
end
|
123
|
+
|
124
|
+
def methods_include?(methods, method)
|
125
|
+
method = (RUBY_VERSION =~ /^1\.9\./ ? method.to_sym : method.to_s)
|
126
|
+
methods.include?(method)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
begin
|
19
|
+
require 'torquebox-messaging'
|
20
|
+
rescue LoadError=>e
|
21
|
+
# ignore!
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'torquebox/naming'
|
25
|
+
require 'torquebox/messaging/ext/javax_jms_session'
|
26
|
+
|
27
|
+
module TorqueBox
|
28
|
+
module Messaging
|
29
|
+
|
30
|
+
class Client
|
31
|
+
|
32
|
+
AUTO_ACK = javax.jms::Session::AUTO_ACKNOWLEDGE
|
33
|
+
CLIENT_ACK = javax.jms::Session::CLIENT_ACKNOWLEDGE
|
34
|
+
DUPS_OK_ACK = javax.jms::Session::DUPS_OK_ACKNOWLEDGE
|
35
|
+
|
36
|
+
|
37
|
+
def self.canonical_ack_mode(ack_mode)
|
38
|
+
case ( ack_mode )
|
39
|
+
when Fixnum
|
40
|
+
return ack_mode
|
41
|
+
when :auto
|
42
|
+
return AUTO_ACK
|
43
|
+
when :client
|
44
|
+
return CLIENT_ACK
|
45
|
+
when :dups_ok
|
46
|
+
return DUPS_OK_ACK
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.connect(options={}, &block)
|
51
|
+
transacted = options.fetch(:transacted, true)
|
52
|
+
ack_mode = options.fetch(:ack_mode, AUTO_ACK)
|
53
|
+
naming_host = options[:naming_host]
|
54
|
+
naming_port = options[:naming_port]
|
55
|
+
connection_factory = nil
|
56
|
+
TorqueBox::Naming.connect( naming_host, naming_port ) do |context|
|
57
|
+
connection_factory = context['/ConnectionFactory']
|
58
|
+
connection = connection_factory.createConnection
|
59
|
+
session = connection.createSession( transacted, canonical_ack_mode( ack_mode ) )
|
60
|
+
connection.start
|
61
|
+
session.naming_context = context
|
62
|
+
session.connection = connection
|
63
|
+
return session if ( block.nil? )
|
64
|
+
begin
|
65
|
+
block.call( session )
|
66
|
+
ensure
|
67
|
+
connection.close()
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
require 'torquebox/kernel'
|
19
|
+
require 'torquebox/messaging/client'
|
20
|
+
|
21
|
+
require 'torquebox/messaging/ext/javax_jms_queue_browser'
|
22
|
+
|
23
|
+
module TorqueBox
|
24
|
+
module Messaging
|
25
|
+
|
26
|
+
module Destination
|
27
|
+
include Enumerable
|
28
|
+
|
29
|
+
attr_reader :name
|
30
|
+
|
31
|
+
PRIORITY_MAP = {
|
32
|
+
:low => 1,
|
33
|
+
:normal => 4,
|
34
|
+
:high => 7,
|
35
|
+
:critical => 9
|
36
|
+
}
|
37
|
+
|
38
|
+
def initialize(name, connect_options=nil, enumerable_options=nil)
|
39
|
+
@name = name
|
40
|
+
@connect_options = connect_options || {}
|
41
|
+
@enumerable_options = enumerable_options || {}
|
42
|
+
end
|
43
|
+
|
44
|
+
def publish(message, options = {})
|
45
|
+
wait_for_destination(options[:startup_timeout]) {
|
46
|
+
Client.connect(@connect_options) do |session|
|
47
|
+
session.publish name, message, normalize_options(options)
|
48
|
+
session.commit if session.transacted?
|
49
|
+
end
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def receive(options={})
|
54
|
+
result = nil
|
55
|
+
wait_for_destination(options[:startup_timeout]) {
|
56
|
+
Client.connect(@connect_options) do |session|
|
57
|
+
result = session.receive( name, options )
|
58
|
+
session.commit if session.transacted?
|
59
|
+
end
|
60
|
+
}
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
64
|
+
def start
|
65
|
+
TorqueBox::Kernel.lookup("JMSServerManager") do |server|
|
66
|
+
destination.name = name
|
67
|
+
destination.server = server
|
68
|
+
destination.create
|
69
|
+
end
|
70
|
+
end
|
71
|
+
alias_method :create, :start
|
72
|
+
|
73
|
+
def destroy
|
74
|
+
TorqueBox::Kernel.lookup("JMSServerManager") do |server|
|
75
|
+
destination.name = name
|
76
|
+
destination.server = server
|
77
|
+
destination.destroy
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def each(&block)
|
82
|
+
wait_for_destination do
|
83
|
+
Client.connect(@connect_options) do |session|
|
84
|
+
destination = session.lookup_destination( name )
|
85
|
+
browser = session.create_browser( destination, @enumerable_options[:selector] )
|
86
|
+
begin
|
87
|
+
browser.each(&block)
|
88
|
+
ensure
|
89
|
+
browser.close
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_s
|
96
|
+
name
|
97
|
+
end
|
98
|
+
|
99
|
+
protected
|
100
|
+
|
101
|
+
def normalize_options(options)
|
102
|
+
if options.has_key?(:persistent)
|
103
|
+
options[:delivery_mode] =
|
104
|
+
options.delete(:persistent) ? javax.jms::DeliveryMode.PERSISTENT : javax.jms::DeliveryMode.NON_PERSISTENT
|
105
|
+
end
|
106
|
+
|
107
|
+
if options.has_key?(:priority)
|
108
|
+
if PRIORITY_MAP[options[:priority]]
|
109
|
+
options[:priority] = PRIORITY_MAP[options[:priority]]
|
110
|
+
elsif (0..9) === options[:priority].to_i
|
111
|
+
options[:priority] = options[:priority].to_i
|
112
|
+
else
|
113
|
+
raise ArgumentError.new(":priority must in the range 0..9, or one of #{PRIORITY_MAP.keys.collect {|k| ":#{k}"}.join(',')}")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
options
|
118
|
+
end
|
119
|
+
|
120
|
+
def wait_for_destination(timeout=nil, &block)
|
121
|
+
timeout ||= 30000 # 30s default
|
122
|
+
start = Time.now
|
123
|
+
begin
|
124
|
+
block.call
|
125
|
+
rescue javax.naming.NameNotFoundException => ex
|
126
|
+
elapsed = (Time.now - start) * 1000
|
127
|
+
if elapsed > timeout
|
128
|
+
raise ex
|
129
|
+
else
|
130
|
+
sleep(0.1)
|
131
|
+
retry
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class Queue
|
138
|
+
include Destination
|
139
|
+
def destination
|
140
|
+
@destination ||= Java::org.torquebox.messaging.core::ManagedQueue.new
|
141
|
+
end
|
142
|
+
|
143
|
+
def publish_and_receive(message, options={})
|
144
|
+
result = nil
|
145
|
+
wait_for_destination(options[:startup_timeout]) {
|
146
|
+
Client.connect(@connect_options) do |session|
|
147
|
+
result = session.publish_and_receive(name, message,
|
148
|
+
normalize_options(options))
|
149
|
+
session.commit if session.transacted?
|
150
|
+
end
|
151
|
+
}
|
152
|
+
result
|
153
|
+
end
|
154
|
+
|
155
|
+
def receive_and_publish(options={}, &block)
|
156
|
+
wait_for_destination(options[:startup_timeout]) {
|
157
|
+
Client.connect(@connect_options) do |session|
|
158
|
+
session.receive_and_publish(name, normalize_options(options), &block)
|
159
|
+
session.commit if session.transacted?
|
160
|
+
end
|
161
|
+
}
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class Topic
|
166
|
+
include Destination
|
167
|
+
def destination
|
168
|
+
@destination ||= Java::org.torquebox.messaging.core::ManagedTopic.new
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
module javax.jms::QueueBrowser
|
21
|
+
include Enumerable
|
22
|
+
|
23
|
+
def each(&block)
|
24
|
+
enumeration.each(&block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# Copyright 2008-2011 Red Hat, Inc, and individual contributors.
|
2
|
+
#
|
3
|
+
# This is free software; you can redistribute it and/or modify it
|
4
|
+
# under the terms of the GNU Lesser General Public License as
|
5
|
+
# published by the Free Software Foundation; either version 2.1 of
|
6
|
+
# the License, or (at your option) any later version.
|
7
|
+
#
|
8
|
+
# This software is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11
|
+
# Lesser General Public License for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with this software; if not, write to the Free
|
15
|
+
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
16
|
+
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
17
|
+
|
18
|
+
require 'torquebox/messaging/javax_jms_text_message'
|
19
|
+
|
20
|
+
module javax.jms::Session
|
21
|
+
|
22
|
+
attr_accessor :connection
|
23
|
+
attr_accessor :naming_context
|
24
|
+
|
25
|
+
def publish(destination, message, options = {})
|
26
|
+
destination = lookup_destination( destination ) unless destination.is_a?( Java::javax.jms.Destination )
|
27
|
+
producer = createProducer( destination )
|
28
|
+
jms_message = create_text_message
|
29
|
+
populate_message_headers(jms_message, options)
|
30
|
+
populate_message_properties(jms_message, options[:properties])
|
31
|
+
jms_message.encode message
|
32
|
+
producer.send( jms_message,
|
33
|
+
options.fetch(:delivery_mode, producer.delivery_mode),
|
34
|
+
options.fetch(:priority, producer.priority),
|
35
|
+
options.fetch(:ttl, producer.time_to_live) )
|
36
|
+
jms_message
|
37
|
+
ensure
|
38
|
+
producer.close unless producer.nil?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns decoded message, by default. Pass :decode=>false to
|
42
|
+
# return the original JMS TextMessage. Pass :timeout to give up
|
43
|
+
# after a number of milliseconds
|
44
|
+
def receive(destination_name, options={})
|
45
|
+
decode = options.fetch(:decode, true)
|
46
|
+
timeout = options.fetch(:timeout, 0)
|
47
|
+
selector = options.fetch(:selector, nil)
|
48
|
+
destination = lookup_destination( destination_name )
|
49
|
+
consumer = createConsumer( destination, selector )
|
50
|
+
jms_message = consumer.receive( timeout )
|
51
|
+
if jms_message
|
52
|
+
decode ? jms_message.decode : jms_message
|
53
|
+
end
|
54
|
+
ensure
|
55
|
+
consumer.close unless consumer.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
# Implement the request-response pattern. Sends a message to the
|
59
|
+
# request destination and waits for a reply on the response
|
60
|
+
# destination.
|
61
|
+
#
|
62
|
+
# Options:
|
63
|
+
#
|
64
|
+
# :timeout - specifies the time in miliseconds to wait for answer,
|
65
|
+
# default: 10000 (10s)
|
66
|
+
# :decode - pass false to return the original JMS TextMessage,
|
67
|
+
# default: true
|
68
|
+
#
|
69
|
+
def publish_and_receive(destination, message, options = {})
|
70
|
+
options[:timeout] ||= 10000 # 10s
|
71
|
+
decode = options.fetch(:decode, false)
|
72
|
+
options[:properties] ||= {}
|
73
|
+
options[:properties]["synchronous"] = "true"
|
74
|
+
wrapped_message = { :timeout => options[:timeout], :message => message }
|
75
|
+
jms_message = publish(destination, wrapped_message, options)
|
76
|
+
commit if transacted?
|
77
|
+
|
78
|
+
options[:selector] = "JMSCorrelationID='#{jms_message.jms_message_id}'"
|
79
|
+
response = receive(destination, options)
|
80
|
+
commit if transacted?
|
81
|
+
|
82
|
+
if response
|
83
|
+
decode ? response.decode : response
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Receiving end of the request-response pattern. The return value of
|
88
|
+
# the block passed to this method is the response sent back to the
|
89
|
+
# client. If no block is given then request is returned as the
|
90
|
+
# response.
|
91
|
+
def receive_and_publish(destination, options = {})
|
92
|
+
receive_options = options.merge(:decode => false,
|
93
|
+
:selector => "synchronous = 'true'")
|
94
|
+
request = receive(destination, receive_options)
|
95
|
+
unless request.nil?
|
96
|
+
decoded_request = request.decode
|
97
|
+
request_message = decoded_request[:message]
|
98
|
+
# Base the response ttl off the original request timeout
|
99
|
+
request_timeout = decoded_request[:timeout]
|
100
|
+
options[:ttl] ||= request_timeout
|
101
|
+
|
102
|
+
response = block_given? ? yield(request_message) : request_message
|
103
|
+
|
104
|
+
options[:correlation_id] = request.jms_message_id
|
105
|
+
publish(destination, response, options)
|
106
|
+
end
|
107
|
+
commit if transacted?
|
108
|
+
end
|
109
|
+
|
110
|
+
def lookup_destination(destination_name)
|
111
|
+
@naming_context[ destination_name ]
|
112
|
+
end
|
113
|
+
|
114
|
+
def populate_message_headers(jms_message, options)
|
115
|
+
return if options.nil?
|
116
|
+
options.each do |key, value|
|
117
|
+
case key.to_s
|
118
|
+
when 'correlation_id' then jms_message.setJMSCorrelationID(value)
|
119
|
+
when 'reply_to' then jms_message.setJMSReplyTo(value)
|
120
|
+
when 'type' then jms_message.setJMSType(value)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def populate_message_properties(jms_message, properties)
|
126
|
+
return if properties.nil?
|
127
|
+
properties.each do |key, value|
|
128
|
+
case value
|
129
|
+
when Integer
|
130
|
+
jms_message.set_long_property(key.to_s, value)
|
131
|
+
when Float
|
132
|
+
jms_message.set_double_property(key.to_s, value)
|
133
|
+
when TrueClass, FalseClass
|
134
|
+
jms_message.set_boolean_property(key.to_s, value)
|
135
|
+
else
|
136
|
+
jms_message.set_string_property(key.to_s, value.to_s)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|