torquebox-messaging 3.2.0-java → 4.0.0.alpha1-java

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. checksums.yaml +4 -4
  2. data/lib/torquebox-messaging.rb +19 -20
  3. data/lib/torquebox/messaging.rb +80 -20
  4. data/lib/torquebox/messaging/context.rb +174 -0
  5. data/lib/torquebox/messaging/destination.rb +164 -215
  6. data/lib/torquebox/messaging/helpers.rb +34 -0
  7. data/lib/torquebox/messaging/hornetq.rb +70 -0
  8. data/lib/torquebox/messaging/hornetq/address_settings.rb +228 -0
  9. data/lib/torquebox/messaging/queue.rb +139 -301
  10. data/lib/torquebox/messaging/topic.rb +84 -46
  11. data/lib/wunderboss-jars/hornetq-commons-2.4.5.Final.jar +0 -0
  12. data/lib/wunderboss-jars/hornetq-core-client-2.4.5.Final.jar +0 -0
  13. data/lib/wunderboss-jars/hornetq-jms-client-2.4.5.Final.jar +0 -0
  14. data/lib/wunderboss-jars/hornetq-jms-server-2.4.5.Final.jar +0 -0
  15. data/lib/wunderboss-jars/hornetq-journal-2.4.5.Final.jar +0 -0
  16. data/lib/wunderboss-jars/hornetq-native-2.4.5.Final.jar +0 -0
  17. data/lib/wunderboss-jars/hornetq-server-2.4.5.Final.jar +0 -0
  18. data/lib/wunderboss-jars/javax.inject-1.jar +0 -0
  19. data/lib/wunderboss-jars/jboss-common-core-2.2.10.GA.jar +0 -0
  20. data/lib/wunderboss-jars/jboss-connector-api_1.5_spec-1.0.0.Final.jar +0 -0
  21. data/lib/wunderboss-jars/jboss-jms-api_2.0_spec-1.0.0.Final.jar +0 -0
  22. data/lib/wunderboss-jars/jboss-transaction-api_1.1_spec-1.0.0.Final.jar +0 -0
  23. data/lib/wunderboss-jars/jboss-transaction-spi-7.0.0.Final.jar +0 -0
  24. data/lib/wunderboss-jars/jnpserver-5.0.3.GA.jar +0 -0
  25. data/lib/wunderboss-jars/netty-all-4.0.13.Final.jar +0 -0
  26. data/lib/wunderboss-jars/wunderboss-messaging-1.x.incremental.174.jar +0 -0
  27. metadata +88 -98
  28. data/lib/gem_hook.rb +0 -51
  29. data/lib/hornetq-commons-2.3.1.Final.jar +0 -0
  30. data/lib/hornetq-core-client-2.3.1.Final.jar +0 -0
  31. data/lib/hornetq-jms-client-2.3.1.Final.jar +0 -0
  32. data/lib/hornetq-journal-2.3.1.Final.jar +0 -0
  33. data/lib/jboss-jms-api_1.1_spec-1.0.1.Final.jar +0 -0
  34. data/lib/jboss-logging-3.1.2.GA.jar +0 -0
  35. data/lib/jboss-logmanager-1.4.0.Final.jar +0 -0
  36. data/lib/jboss-transaction-api_1.1_spec-1.0.1.Final.jar +0 -0
  37. data/lib/netty-3.6.2.Final.jar +0 -0
  38. data/lib/org.torquebox.messaging-client.rb +0 -20
  39. data/lib/torquebox-messaging.jar +0 -0
  40. data/lib/torquebox/messaging/backgroundable.rb +0 -250
  41. data/lib/torquebox/messaging/backgroundable_processor.rb +0 -52
  42. data/lib/torquebox/messaging/connection.rb +0 -74
  43. data/lib/torquebox/messaging/connection_factory.rb +0 -115
  44. data/lib/torquebox/messaging/const_missing.rb +0 -28
  45. data/lib/torquebox/messaging/core.rb +0 -27
  46. data/lib/torquebox/messaging/datamapper_marshaling.rb +0 -43
  47. data/lib/torquebox/messaging/echo_processor.rb +0 -35
  48. data/lib/torquebox/messaging/edn_message.rb +0 -26
  49. data/lib/torquebox/messaging/ext/javax_jms_queue_browser.rb +0 -25
  50. data/lib/torquebox/messaging/future.rb +0 -131
  51. data/lib/torquebox/messaging/future_responder.rb +0 -109
  52. data/lib/torquebox/messaging/future_status.rb +0 -37
  53. data/lib/torquebox/messaging/json_message.rb +0 -26
  54. data/lib/torquebox/messaging/marshal_base64_message.rb +0 -26
  55. data/lib/torquebox/messaging/marshal_message.rb +0 -43
  56. data/lib/torquebox/messaging/message.rb +0 -137
  57. data/lib/torquebox/messaging/message_processor.rb +0 -209
  58. data/lib/torquebox/messaging/processor_middleware/chain.rb +0 -89
  59. data/lib/torquebox/messaging/processor_middleware/default_middleware.rb +0 -39
  60. data/lib/torquebox/messaging/processor_middleware/with_transaction.rb +0 -35
  61. data/lib/torquebox/messaging/session.rb +0 -173
  62. data/lib/torquebox/messaging/task.rb +0 -66
  63. data/lib/torquebox/messaging/text_message.rb +0 -36
  64. data/lib/torquebox/messaging/xa_connection.rb +0 -55
  65. data/lib/torquebox/messaging/xa_connection_factory.rb +0 -45
  66. data/lib/torquebox/messaging/xa_session.rb +0 -51
  67. data/licenses/cc0-1.0.txt +0 -121
  68. data/spec/backgroundable_spec.rb +0 -319
  69. data/spec/chain_spec.rb +0 -160
  70. data/spec/datamapper_marshaling_spec.rb +0 -25
  71. data/spec/default_middleware_spec.rb +0 -21
  72. data/spec/destination_spec.rb +0 -248
  73. data/spec/future_responder_spec.rb +0 -81
  74. data/spec/future_spec.rb +0 -163
  75. data/spec/message_processor_spec.rb +0 -211
  76. data/spec/message_spec.rb +0 -135
  77. data/spec/task_spec.rb +0 -111
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a88f0c3624c3fc1d526b1db306ab3412dda0d25
4
- data.tar.gz: e6481e02bfb7293207f3ee0edb92bddbeab7cca5
3
+ metadata.gz: 0e0d4bba6bdf81556ac4029954113a4ca1261182
4
+ data.tar.gz: b60488fcd243cb03eb35dd93206a0a81a2979200
5
5
  SHA512:
6
- metadata.gz: 04b06b767aa8e96226d13458912dbd5a6e589c77d30a537097ccbf05eb35cea634629495dd64310a22547044186b2b2c34c06439dd5ac65997394f1c7c8fe780
7
- data.tar.gz: b549fba3e2e7d78af367d22df660e07fe8988cf2e7de0cc54b2f9a24870d167c75798fec2c04b2542b05d1f7ced40ea3cc98dcd8d7e2f5a8017f3a0f15494acb
6
+ metadata.gz: 379e6fdca557bb4ee68d126334324537ac1abd1793b213b85d566605adf3d6af613668d76cb006efee9d0e44557d63a21a6d27a034d181eec6484ccc25c9379a
7
+ data.tar.gz: 7f9b80963a6097523f642ac06c0190e007fb8b3d052f1b7476d8a90222e001c835bfbdd486950d2043fdef0a2d26e31c31b2e25d92f2b9f83c3f26d83770b3f7
@@ -1,22 +1,21 @@
1
- module TorqueboxMessaging
2
- VERSION = '3.2.0'
3
- MAVEN_VERSION = '3.2.0'
4
- end
5
- begin
6
- require 'java'
7
- require File.dirname(__FILE__) + '/torquebox-messaging.jar'
8
- require File.dirname(__FILE__) + '/jboss-jms-api_1.1_spec-1.0.1.Final.jar'
9
- require File.dirname(__FILE__) + '/jboss-transaction-api_1.1_spec-1.0.1.Final.jar'
10
- require File.dirname(__FILE__) + '/hornetq-core-client-2.3.1.Final.jar'
11
- require File.dirname(__FILE__) + '/hornetq-commons-2.3.1.Final.jar'
12
- require File.dirname(__FILE__) + '/jboss-logging-3.1.2.GA.jar'
13
- require File.dirname(__FILE__) + '/netty-3.6.2.Final.jar'
14
- require File.dirname(__FILE__) + '/hornetq-journal-2.3.1.Final.jar'
15
- require File.dirname(__FILE__) + '/jboss-logmanager-1.4.0.Final.jar'
16
- require File.dirname(__FILE__) + '/hornetq-jms-client-2.3.1.Final.jar'
17
- rescue LoadError
18
- puts 'JAR-based gems require JRuby to load. Please visit www.jruby.org.'
19
- raise
1
+ # Copyright 2014 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'torquebox-core'
16
+
17
+ Dir.glob("#{File.dirname(__FILE__)}/wunderboss-jars/*.jar") do |jar|
18
+ TorqueBox::Jars.register_and_require(jar)
20
19
  end
21
20
 
22
- load File.dirname(__FILE__) + '/gem_hook.rb' if File.exists?( File.dirname(__FILE__) + '/gem_hook.rb')
21
+ require 'torquebox/messaging'
@@ -1,22 +1,82 @@
1
- # Copyright 2008-2013 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.
1
+ # Copyright 2014 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
17
14
 
18
- begin
19
- require 'torquebox-messaging'
20
- rescue LoadError=>e
21
- # ignore!
15
+ require 'torquebox/messaging/context'
16
+ require 'torquebox/messaging/destination'
17
+ require 'torquebox/messaging/queue'
18
+ require 'torquebox/messaging/topic'
19
+ require 'torquebox/messaging/hornetq'
20
+
21
+ module TorqueBox
22
+ module Messaging
23
+ def self.default_encoding
24
+ @default_encoding ||= :marshal
25
+ end
26
+
27
+ def self.default_encoding=(enc)
28
+ @default_encoding = enc
29
+ end
30
+
31
+ # Creates a new queue reference.
32
+ #
33
+ # This may be a reference to a remote or local (in-vm) queue.
34
+ # Obtaining a reference to an in-vm queue will cause the queue
35
+ # to be created within the broker if it does not already exist.
36
+ # For remote queues, the queue must already exist in the remote
37
+ # broker.
38
+ #
39
+ # If a context is provided, it must be remote, and will be
40
+ # remembered and used by any method that takes a `:context`
41
+ # option.
42
+ #
43
+ # @param name [String] The name of the queue.
44
+ # @param options [Hash] Options for queue creation.
45
+ # @option options :context [Context] A context to a
46
+ # *remote* broker to use; caller expected to close.
47
+ # @option options :durable [true, false] (true) Whether messages
48
+ # persist across restarts.
49
+ # @option options :selector [String] A JMS (SQL 92) expression
50
+ # to filter published messages.
51
+ # @option options :default_options [Hash] A set of default
52
+ # options to apply to any operations on this queue.
53
+ # @return [Queue] The queue reference.
54
+ def self.queue(name, options = {})
55
+ Queue.new(name, options)
56
+ end
57
+
58
+ # Creates a new topic reference.
59
+ #
60
+ # This may be a reference to a remote or local (in-vm) topic.
61
+ # Obtaining a reference to an in-vm topic will cause the topic
62
+ # to be created within the broker if it does not already exist.
63
+ # For remote topics, the topic must already exist in the remote
64
+ # broker.
65
+ #
66
+ # If a context is provided, it must be remote, and will be
67
+ # remembered and used by any method that takes a `:context`
68
+ # option.
69
+ #
70
+ # @param name [String] The name of the topic.
71
+ # @param options [Hash] Options for topic creation.
72
+ # @option options :context [Context] A context to a
73
+ # *remote* broker to use; caller expected to close.
74
+ # @option options :default_options [Hash] A set of default
75
+ # options to apply to any operations on this topic.
76
+ # @return [Topic] The topic reference.
77
+ def self.topic(name, options = {})
78
+ Topic.new(name, options)
79
+ end
80
+
81
+ end
22
82
  end
@@ -0,0 +1,174 @@
1
+ # Copyright 2014 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'torquebox/messaging/helpers'
16
+
17
+ module TorqueBox
18
+ module Messaging
19
+ # Represents a context to the message broker.
20
+ #
21
+ # You should only need to create a Context directly if you are
22
+ # connecting to a remote broker, you need transactional semantics,
23
+ # or you need better perfomance when doing lots of
24
+ # {Destination#publish} or {Destination#receive} calls in rapid
25
+ # succession, since each call will
26
+ # create and close a context if one is not provided.
27
+ class Context
28
+ include TorqueBox::OptionUtils
29
+ include TorqueBox::Messaging::Helpers
30
+ extend TorqueBox::OptionUtils
31
+
32
+ attr_reader :internal_context
33
+
34
+ # Valid options for Context creation.
35
+ CONTEXT_OPTIONS = optset(WBMessaging::CreateContextOption)
36
+
37
+ # Creates a new context.
38
+ #
39
+ # You are responsible for closing any contexts you create.
40
+ #
41
+ # If given a block, the Context instance will be passed to
42
+ # the block and the Context will be closed once the block
43
+ # returns.
44
+ #
45
+ # @param options [Hash]
46
+ # @option options :mode [Symbol] (:auto_ack) One of: :auto_ack,
47
+ # :client_ack, :transacted
48
+ # @option options :client_id [String] Identifies the client id
49
+ # for use with a durable topic subscriber.
50
+ # @option options :host [String] The host of a remote broker.
51
+ # @option options :port [Number] (nil, 5445 if :host provided)
52
+ # The port of a remote broker.
53
+ # @option options :username [String] The username for the remote
54
+ # broker.
55
+ # @option options :password [String] The password for the remote
56
+ # broker.
57
+ # @option options :remote_type [Symbol] (:hornetq_standalone)
58
+ # When connecting to a HornetQ instance running inside
59
+ # WildFly, this needs to be set to :hornetq_wildfly.
60
+ # @option options :reconnect_attempts [Number] (0) Total number
61
+ # of reconnect attempts to make before giving up (-1 for unlimited).
62
+ # @option options :reconnect_retry_interval [Number] (2000) The
63
+ # period in milliseconds between subsequent recontext attempts.
64
+ # @option options :reconnect_max_retry_interval [Number] (2000)
65
+ # The max retry interval that will be used.
66
+ # @option options :reconnect_retry_interval_multiplier [Number]
67
+ # (1.0) A multiplier to apply to the time since the last retry
68
+ # to compute the time to the next retry.
69
+ # @return [Context]
70
+ def initialize(options = {}, &block)
71
+ validate_options(options, CONTEXT_OPTIONS)
72
+ options[:mode] = coerce_mode(options[:mode])
73
+ create_options = extract_options(options, WBMessaging::CreateContextOption)
74
+ @internal_context = default_broker.create_context(create_options)
75
+ if block
76
+ begin
77
+ block.call(self)
78
+ ensure
79
+ close
80
+ end
81
+ end
82
+ end
83
+
84
+ # Creates a Queue from this context.
85
+ #
86
+ # The Queue instance will use this context for
87
+ # any of its methods that take a `:context` option.
88
+ #
89
+ # @param (see Queue#initialize)
90
+ # @return (see Queue#initialize)
91
+ def queue(name, options = {})
92
+ TorqueBox::Messaging.queue(name, options.merge(:context => self))
93
+ end
94
+
95
+ # Creates a Topic from this context.
96
+ #
97
+ # The Topic instance will use this context for
98
+ # any of its methods that take a `:context` option.
99
+ #
100
+ # @param (see Topic#initialize)
101
+ # @return (see Topic#initialize)
102
+ def topic(name, options = {})
103
+ TorqueBox::Messaging.topic(name, options.merge(:context => self))
104
+ end
105
+
106
+ # Closes this context.
107
+ #
108
+ # Once closed, any operations on this context will raise
109
+ # errors.
110
+ # @return [void]
111
+ def close
112
+ @internal_context.close
113
+ end
114
+
115
+
116
+ # Rolls back the context.
117
+ #
118
+ # This only has affect for :transacted contexts.
119
+ #
120
+ # @return [void]
121
+ def rollback
122
+ @internal_context.rollback
123
+ end
124
+
125
+ # Commits the context.
126
+ #
127
+ # This only has affect for :transacted contexts.
128
+ #
129
+ # @return [void]
130
+ def commit
131
+ @internal_context.commit
132
+ end
133
+
134
+ # Acknowledge any un-acknowledged messages in this context.
135
+ #
136
+ # This only has affect for :client_ack contexts.
137
+ #
138
+ # @return [void]
139
+ def acknowledge
140
+ @internal_context.acknowledge
141
+ end
142
+
143
+ # @return [Symbol] The mode of this context.
144
+ def mode
145
+ case @internal_context.mode
146
+ when WBContext::Mode::AUTO_ACK
147
+ :auto_ack
148
+ when WBContext::Mode::CLIENT_ACK
149
+ :client_ack
150
+ when WBContext::Mode::TRANSACTED
151
+ :transacted
152
+ end
153
+ end
154
+
155
+ protected
156
+
157
+ def coerce_mode(mode)
158
+ case mode
159
+ when nil
160
+ nil
161
+ when :auto_ack
162
+ WBContext::Mode::AUTO_ACK
163
+ when :client_ack
164
+ WBContext::Mode::CLIENT_ACK
165
+ when :transacted
166
+ WBContext::Mode::TRANSACTED
167
+ else
168
+ fail ArgumentError.new("#{mode} is not a valid context mode.")
169
+ end
170
+ end
171
+
172
+ end
173
+ end
174
+ end
@@ -1,37 +1,34 @@
1
- # Copyright 2008-2013 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/injectors'
19
- require 'torquebox/msc'
20
- require 'torquebox/messaging/session'
21
- require 'torquebox/messaging/connection_factory'
22
- require 'torquebox/messaging/ext/javax_jms_queue_browser'
1
+ # Copyright 2014 Red Hat, Inc, and individual contributors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'torquebox/codecs'
16
+ require 'torquebox/messaging/helpers'
23
17
 
24
18
  module TorqueBox
25
19
  module Messaging
20
+ # Represents a messaging destination.
21
+ #
22
+ # You'll never instantiate a Destination directly, but use {Queue}
23
+ # or {Topic} instead.
26
24
  class Destination
27
- include Enumerable
25
+ include TorqueBox::OptionUtils
26
+ include TorqueBox::Messaging::Helpers
27
+ extend TorqueBox::OptionUtils
28
28
 
29
- attr_reader :connection_factory
30
- attr_reader :name
31
- attr_reader :java_destination
32
- attr_accessor :enumerable_options
33
- attr_accessor :connect_options
29
+ attr_accessor :internal_destination
34
30
 
31
+ # Maps priority mnemonics to priority values.
35
32
  PRIORITY_MAP = {
36
33
  :low => 1,
37
34
  :normal => 4,
@@ -39,228 +36,180 @@ module TorqueBox
39
36
  :critical => 9
40
37
  }
41
38
 
42
- def _dump(level)
43
- name
44
- end
39
+ # Valid options for {#publish}.
40
+ PUBLISH_OPTIONS = optset(WBDestination::PublishOption, :encoding)
45
41
 
46
- def self._load(str)
47
- self.new( str )
42
+ # Send a message to this destination.
43
+ #
44
+ # The message can be any data that can be encoded using the
45
+ # given :encoding.
46
+ #
47
+ # If no context is provided, a new context will be created, then
48
+ # closed.
49
+ #
50
+ # @param message [Object] The message to send.
51
+ # @param options [Hash] Options for message publication.
52
+ # @option options :encoding [Symbol] (:marshal) one of: :edn, :json,
53
+ # :marshal, :marshal_base64, :text
54
+ # @option options :priority [Symbol, Number] (:normal) 0-9, or
55
+ # one of: :low, :normal, :high, :critical
56
+ # @option options :ttl [Number] (0) time to live, in millis, 0
57
+ # == forever
58
+ # @option options :persistent [true, false] (true) whether
59
+ # undelivered messages survive restarts
60
+ # @option options :properties [Hash] a hash to which selectors
61
+ # may be applied
62
+ # @option options :context [Context] a context to use;
63
+ # caller expected to close
64
+ # @return [void]
65
+ def publish(message, options = {})
66
+ validate_options(options, PUBLISH_OPTIONS)
67
+ options = apply_default_options(options)
68
+ options = normalize_publish_options(options)
69
+ options = coerce_context(options)
70
+ encoding = options[:encoding] || Messaging.default_encoding
71
+ @internal_destination.publish(message,
72
+ Codecs[encoding],
73
+ extract_options(options, WBDestination::PublishOption))
48
74
  end
49
75
 
50
- def initialize(destination, connection_factory_or_options = nil)
51
- raise ArgumentError, "destination cannot be nil" unless destination
52
- if connection_factory_or_options.nil? || connection_factory_or_options.is_a?( Hash )
53
- options = connection_factory_or_options
54
- connection_factory = TorqueBox.fetch( 'connection-factory' )
55
- unless options.nil?
56
- # Don't use our internal connection factory if the user
57
- # has specified a host or port to connect to
58
- connection_factory = nil if options[:host] or options[:port]
59
- end
60
- @connection_factory = ConnectionFactory.new( connection_factory )
61
- @connect_options = options || {}
62
- else
63
- @connection_factory = ConnectionFactory.new( connection_factory_or_options )
64
- @connect_options = {}
65
- end
76
+ # Valid options for {#receive}.
77
+ RECEIVE_OPTIONS = optset(WBDestination::ReceiveOption, :decode, :timeout_val)
66
78
 
67
-
68
- if destination.is_a?(javax.jms.Destination )
69
- if destination.is_a?(javax.jms.Queue)
70
- @name = destination.queue_name
71
- else
72
- @name = destination.topic_name
73
- end
74
-
75
- @java_destination = destination
76
- else
77
- @name = destination
78
- end
79
-
80
-
81
- @enumerable_options = {}
79
+ # Receive a message from this destination.
80
+ #
81
+ # Can optionally be given a block that will be called with
82
+ # the message.
83
+ #
84
+ # If a :selector is provided, then only messages having
85
+ # properties matching that expression may be received.
86
+ #
87
+ # If no context is provided, a new context will be created, then
88
+ # closed.
89
+ #
90
+ # @param options [Hash] Options for message receipt.
91
+ # @option options :timeout [Number] (10000) Time in millis,
92
+ # after which the :timeout_val is returned. 0
93
+ # means wait forever, -1 means don't wait at all
94
+ # @option options :timeout_val [Object] The value to
95
+ # return when a timeout occurs. Also returned when
96
+ # a :timeout of -1 is specified, and no message is available
97
+ # @option options :selector [String] A JMS (SQL 92) expression
98
+ # matching message properties
99
+ # @option options :decode [true, false] (true) If true, the
100
+ # decoded message body is returned. Otherwise, the
101
+ # base message object is returned.
102
+ # @option options :context [Context] a context to use;
103
+ # caller expected to close
104
+ # @return The message, or the return value of the block if a
105
+ # block is given.
106
+ def receive(options = {}, &block)
107
+ validate_options(options, RECEIVE_OPTIONS)
108
+ options = apply_default_options(options)
109
+ options = coerce_context(options)
110
+ result = @internal_destination.receive(Codecs.java_codecs,
111
+ extract_options(options, WBDestination::ReceiveOption))
112
+ msg = if result
113
+ options.fetch(:decode, true) ? result.body : result
114
+ else
115
+ options[:timeout_val]
116
+ end
117
+ block ? block.call(msg) : msg
82
118
  end
83
119
 
84
- # Stops the destination.
120
+ # Valid options for {#listen}
121
+ LISTEN_OPTIONS = optset(WBDestination::ListenOption, :encoding, :decode)
122
+
123
+ # Registers a block to receive each message sent to this destination.
85
124
  #
86
- # @note This is an asynchronous method.
87
- # @return [java.util.concurrent.CountDownLatch] The latch to wait for the task completion
88
- # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
89
- def stop
90
- TorqueBox::Messaging::Destination.with_destinationizer do |destinationizer|
91
- destinationizer.remove_destination(name)
125
+ # If a :selector is provided, then only messages having
126
+ # properties matching that expression will be received.
127
+ #
128
+ # If given a context, the context must be remote, and the mode
129
+ # of that context is ignored, since it is used solely to
130
+ # generate sub-contexts for each listener thread. Closing the
131
+ # given context will also close the listener.
132
+ #
133
+ # If no context is provided, a new context will be created, then
134
+ # closed.
135
+ #
136
+ # @param options [Hash] Options for the listener.
137
+ # @option options :concurrency [Number] (1) The number of
138
+ # threads handling messages.
139
+ # @option options :selector [String] A JMS (SQL 92) expression
140
+ # matching message properties
141
+ # @option options :decode [true, false] If true, the decoded
142
+ # message body is passed to the block. Otherwise, the
143
+ # base message object is passed.
144
+ # @option options :context [Context] a *remote* context to
145
+ # use; caller expected to close.
146
+ # @return A listener object that can be stopped by
147
+ # calling .close on it.
148
+ def listen(options = {}, &block)
149
+ validate_options(options, LISTEN_OPTIONS)
150
+ options = apply_default_options(options)
151
+ options = coerce_context(options)
152
+ handler = MessageHandler.new do |message|
153
+ block.call(options.fetch(:decode, true) ? message.body : message)
92
154
  end
155
+ @internal_destination.listen(handler,
156
+ Codecs.java_codecs,
157
+ extract_options(options, WBDestination::ListenOption))
93
158
  end
94
159
 
95
- # Stops the destination.
160
+ # Stops this destination.
96
161
  #
97
- # @note This is a synchronous method.
98
- # @return [Boolean] true if the destination was successfully stopped, false otherwise
99
- # @see TorqueBox::Messaging::Destination.stop
100
- def stop_sync
101
- TorqueBox::Messaging::Destination.wait_for_latch(stop)
102
- end
103
-
104
- # Publishes a message to the destination
162
+ # Note that stopping a destination may remove it from the broker if
163
+ # called outside of the container.
105
164
  #
106
- # @param message The message to publish
107
- # @param options Optional parameters (a Hash)
108
165
  # @return [void]
109
- def publish(message, options = {})
110
- wait_for_destination(options[:startup_timeout]) do
111
- with_session(options) do |session|
112
- session.publish self, message, normalize_options(options)
113
- end
114
- end
166
+ def stop
167
+ @internal_destination.stop
115
168
  end
116
169
 
117
- def receive(options = {}, &block)
118
- wait_for_destination(options[:startup_timeout]) do
119
- func = lambda do
120
- with_session(options) do |session|
121
- session.receive self, options, &block
122
- end
123
- end
124
- if block
125
- TorqueBox.transaction &func
126
- else
127
- func.call
128
- end
129
- end
130
- end
170
+ protected
131
171
 
132
- def each(&block)
133
- wait_for_destination do
134
- with_session do |session|
135
- destination = session.java_destination( self )
136
- browser = session.create_browser( destination, enumerable_options[:selector] )
137
- begin
138
- browser.each(&block)
139
- ensure
140
- browser.close
141
- end
142
- end
143
- end
172
+ def initialize(internal_destination, options)
173
+ @internal_destination = internal_destination
174
+ @default_options = options[:default_options] || {}
175
+ @default_options[:context] ||= options[:context]
144
176
  end
145
177
 
146
- def with_session(opts = {})
147
- transactional = opts.fetch(:tx, true)
148
-
149
- # https://issues.jboss.org/browse/TORQUE-1033
150
- # If there is no encoding for the message, set the default one
151
- # for the destination. If the encoding for destination isn't set
152
- # it won't hurt
153
- opts[:encoding] = @connect_options[:encoding] if opts[:encoding].nil?
154
-
155
- connection_factory.with_new_connection(connect_options, transactional) do |connection|
156
- connection.with_session do |session|
157
- yield session
158
- end
159
- end
178
+ def apply_default_options(options)
179
+ @default_options.merge(options || {})
160
180
  end
161
181
 
162
- def wait_for_destination(timeout=nil, &block)
163
- timeout ||= 30_000 # 30s default
164
- start = Time.now
165
- begin
166
- block.call
167
- rescue javax.naming.NameNotFoundException, javax.jms.JMSException
168
- elapsed = (Time.now - start) * 1000
169
- if elapsed > timeout
170
- raise
171
- else
172
- sleep(0.1)
173
- retry
174
- end
175
- end
182
+ def coerce_context(options)
183
+ options = options.dup
184
+ options[:context] = options[:context].internal_context if options[:context]
185
+ options
176
186
  end
177
187
 
178
- def normalize_options(options)
179
- if options.has_key?(:persistent)
180
- options[:delivery_mode] =
181
- options.delete(:persistent) ? javax.jms::DeliveryMode.PERSISTENT : javax.jms::DeliveryMode.NON_PERSISTENT
182
- end
183
-
184
- if options.has_key?(:priority)
188
+ def normalize_publish_options(options)
189
+ options = options.dup
190
+ if options.key?(:priority)
185
191
  if PRIORITY_MAP[options[:priority]]
186
192
  options[:priority] = PRIORITY_MAP[options[:priority]]
187
193
  elsif (0..9) === options[:priority].to_i
188
194
  options[:priority] = options[:priority].to_i
189
195
  else
190
- raise ArgumentError.new(":priority must in the range 0..9, or one of #{PRIORITY_MAP.keys.collect {|k| ":#{k}"}.join(',')}")
196
+ priorities = PRIORITY_MAP.keys.map { |k| ":#{k }" }.join(',')
197
+ fail ArgumentError.new(":priority must in the range 0..9, or one of #{priorities}")
191
198
  end
192
199
  end
193
-
194
200
  options
195
201
  end
196
202
 
197
- def enumerable_options
198
- @enumerable_options ||= { }
199
- @enumerable_options
200
- end
201
-
202
- def to_s
203
- name
204
- end
205
-
206
- class << self
207
-
208
- # List all destinations of this application.
209
- #
210
- # @return Array of {TorqueBox::Messaging::Queue} or {TorqueBox::Messaging::Topic}
211
- # depending on the destination type.
212
- def list
213
- # Get the JMS Manager
214
- TorqueBox::ServiceRegistry.lookup("jboss.messaging.default.jms.manager") do |manager|
215
-
216
- # JMSServerControl will let us grab the deployed queue/topic list
217
- server_control = Java::org.hornetq.jms.management.impl.JMSServerControlImpl.new(manager)
218
-
219
- # Retrieve the destination list appropriate to the destination type
220
- if self == TorqueBox::Messaging::Topic
221
- names = server_control.topic_names
222
- elsif self == TorqueBox::Messaging::Queue
223
- names = server_control.queue_names
224
- else
225
- names = []
226
- end
203
+ # @api private
204
+ class MessageHandler
205
+ include Java::OrgProjectoddWunderbossMessaging::MessageHandler
227
206
 
228
- names.map { |name| self.new(name) }
229
- end
230
- end
231
-
232
- # Lookup a destination of this application by name. A destination could be
233
- # a queue or topic.
234
- #
235
- # @param [String] name of the destination
236
- #
237
- # @return [TorqueBox::Messaging::Queue] or [TorqueBox::Messaging::Topic]
238
- # The destination instance.
239
- def lookup(name)
240
- list.find { |destination| destination.name == name }
241
- end
242
-
243
- # @api private
244
- def with_destinationizer
245
- service_name = TorqueBox::MSC.deployment_unit.service_name.append('torquebox').append('messaging').append('destinationizer')
246
-
247
- TorqueBox::ServiceRegistry.lookup(service_name) do |destinationizer|
248
- yield destinationizer
249
- end
207
+ def initialize(&block)
208
+ @block = block
250
209
  end
251
210
 
252
- # @api private
253
- def wait_for_latch(latch)
254
- if latch
255
- begin
256
- # Wait for the services to come up for up to 30 seconds
257
- latch.await(45, java.util.concurrent.TimeUnit::SECONDS)
258
- rescue
259
- return false
260
- end
261
- end
262
-
263
- true
211
+ def on_message(message, context)
212
+ @block.call(message, context)
264
213
  end
265
214
  end
266
215