jsparrow 1.1.2 → 1.1.3
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.
- data/README.rdoc +16 -8
- data/VERSION.yml +1 -1
- data/lib/connection.rb +91 -83
- data/lib/messaging.rb +36 -20
- data/sample/sample_queue.rb +8 -8
- data/sample/sample_topic.rb +8 -8
- data/spec/messaging_spec.rb +18 -2
- data/spec/spec_helper.rb +10 -10
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
JSparrow is a JMS client based on JRuby. Previously it has been called Sparrow, but changed his name because has another project with the same name.
|
4
4
|
|
5
|
+
WARNING: I'm sorry but any time I break backward compatibility. However, always is for make a better API.
|
6
|
+
|
5
7
|
=== Install
|
6
8
|
|
7
9
|
Make gem install:
|
@@ -21,17 +23,17 @@ WARNING: OpenJMS will be used as JMS provider, but any other could be used with
|
|
21
23
|
|
22
24
|
2) Setup indispensable informations to connect your JMS provider
|
23
25
|
|
24
|
-
JSparrow::Connection.configure do
|
25
|
-
|
26
|
+
JSparrow::Connection.configure do
|
27
|
+
use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
use_jndi_properties :initial_context_factory => 'org.exolab.jms.jndi.InitialContextFactory',
|
30
|
+
:provider_url => 'tcp://localhost:3035'
|
31
|
+
# :security_principal => 'user',
|
32
|
+
# :security_credentials => 'password'
|
31
33
|
|
32
|
-
|
34
|
+
enable_connection_factories :queue_connection_factory => 'ConnectionFactory'
|
33
35
|
|
34
|
-
|
36
|
+
enable_queues :my_queue => 'MyQueue'
|
35
37
|
end
|
36
38
|
|
37
39
|
3) Create the client and start it
|
@@ -72,6 +74,12 @@ Or, if you don't want write code, only do it:
|
|
72
74
|
|
73
75
|
jruby sample/sample_queue.rb
|
74
76
|
|
77
|
+
=== Next
|
78
|
+
|
79
|
+
For more informations, read the specs! ;)
|
80
|
+
|
81
|
+
http://github.com/leandrosilva/jsparrow/tree/master/spec/
|
82
|
+
|
75
83
|
== Copyright
|
76
84
|
|
77
85
|
Copyright (c) 2009 Leandro Silva (CodeZone) <leandrodoze@gmail.com>. See LICENSE for details.
|
data/VERSION.yml
CHANGED
data/lib/connection.rb
CHANGED
@@ -5,42 +5,99 @@ import 'javax.naming.InitialContext'
|
|
5
5
|
module JSparrow
|
6
6
|
module Connection
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
8
|
+
class << self
|
9
|
+
#
|
10
|
+
# Metodo usado para configurar a conexao.
|
11
|
+
#
|
12
|
+
def configure(&block)
|
13
|
+
@@configuration = Configuration.new
|
14
|
+
|
15
|
+
class_eval(&block)
|
16
|
+
|
17
|
+
@@configuration
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Metodo usado para obter a configuracao para conexao com o provedor de JMS.
|
22
|
+
#
|
23
|
+
def configuration
|
24
|
+
@@configuration
|
25
|
+
end
|
17
26
|
|
18
|
-
|
19
|
-
|
27
|
+
#
|
28
|
+
# Use:
|
29
|
+
#
|
30
|
+
# use_jms_client_jar "path/to/name_of_the_client_jar_file.jar"
|
31
|
+
#
|
32
|
+
def use_jms_client_jar(client_jar)
|
33
|
+
configuration.jms_client_jar = client_jar
|
34
|
+
end
|
20
35
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
36
|
+
#
|
37
|
+
# Use:
|
38
|
+
#
|
39
|
+
# use_jndi_properties :a_jndi_property_name_in_lower_case => "a_value_of_property",
|
40
|
+
# :other_jndi_property_name_in_lower_case => "other_value_of_property"
|
41
|
+
#
|
42
|
+
def use_jndi_properties(jndi_properties = {})
|
43
|
+
configuration.jndi_properties = jndi_properties
|
44
|
+
end
|
27
45
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
46
|
+
#
|
47
|
+
# Use:
|
48
|
+
#
|
49
|
+
# enable_connection_factories :queue_connection_factory => "jndi_name_of_queue_connection_factory",
|
50
|
+
# :topic_connection_factory => "jndi_name_of_topic_connection_factory"
|
51
|
+
#
|
52
|
+
def enable_connection_factories(jndi_names = {})
|
53
|
+
configuration.enabled_connection_factories = jndi_names
|
54
|
+
end
|
36
55
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
56
|
+
#
|
57
|
+
# Use:
|
58
|
+
#
|
59
|
+
# enable_queues :a_queue_name_in_lower_case => "jndi_name_of_a_queue",
|
60
|
+
# :other_queue_name_in_lower_case => "jndi_name_of_other_queue"
|
61
|
+
#
|
62
|
+
def enable_queues(jndi_names = {})
|
63
|
+
configuration.enabled_queues = jndi_names
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Use:
|
68
|
+
#
|
69
|
+
# enable_topics :a_topic_name_in_lower_case => "jndi_name_of_a_topic",
|
70
|
+
# :other_topic_name_in_lower_case => "jndi_name_of_other_topic"
|
71
|
+
#
|
72
|
+
def enable_topics(jndi_names = {})
|
73
|
+
configuration.enabled_topics = jndi_names
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# Metodo usado para criar um novo Client JMS.
|
78
|
+
#
|
79
|
+
def new_client
|
80
|
+
Client.new(new_connection)
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# Metodo usado para criar um novo Listener de mensagens JMS.
|
85
|
+
#
|
86
|
+
def new_listener(listener_spec)
|
87
|
+
listener_spec[:as].new(new_connection)
|
88
|
+
end
|
89
|
+
|
90
|
+
# --- Private methods --- #
|
91
|
+
private
|
92
|
+
|
93
|
+
#
|
94
|
+
# Metodo usado para criar uma nova Connection
|
95
|
+
#
|
96
|
+
def new_connection
|
97
|
+
jndi_context_builder = JNDI::ContextBuilder.new(configuration.jms_client_jar, configuration.jndi_properties)
|
98
|
+
|
99
|
+
connection = Base.new(configuration, jndi_context_builder)
|
100
|
+
end
|
44
101
|
end
|
45
102
|
|
46
103
|
#
|
@@ -107,57 +164,8 @@ module JSparrow
|
|
107
164
|
# ao provedor de mensageria via contexto JNDI.
|
108
165
|
#
|
109
166
|
class Configuration
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
#
|
114
|
-
# Use:
|
115
|
-
#
|
116
|
-
# use_jms_client_jar "path/to/name_of_the_client_jar_file.jar"
|
117
|
-
#
|
118
|
-
def use_jms_client_jar(client_jar)
|
119
|
-
@jms_client_jar = client_jar
|
120
|
-
end
|
121
|
-
|
122
|
-
#
|
123
|
-
# Use:
|
124
|
-
#
|
125
|
-
# use_jndi_properties :a_jndi_property_name_in_lower_case => "a_value_of_property",
|
126
|
-
# :other_jndi_property_name_in_lower_case => "other_value_of_property"
|
127
|
-
#
|
128
|
-
def use_jndi_properties(jndi_properties = {})
|
129
|
-
@jndi_properties = jndi_properties
|
130
|
-
end
|
131
|
-
|
132
|
-
#
|
133
|
-
# Use:
|
134
|
-
#
|
135
|
-
# enable_connection_factories :queue_connection_factory => "jndi_name_of_queue_connection_factory",
|
136
|
-
# :topic_connection_factory => "jndi_name_of_topic_connection_factory"
|
137
|
-
#
|
138
|
-
def enable_connection_factories(jndi_names = {})
|
139
|
-
@enabled_connection_factories = jndi_names
|
140
|
-
end
|
141
|
-
|
142
|
-
#
|
143
|
-
# Use:
|
144
|
-
#
|
145
|
-
# enable_queues :a_queue_name_in_lower_case => "jndi_name_of_a_queue",
|
146
|
-
# :other_queue_name_in_lower_case => "jndi_name_of_other_queue"
|
147
|
-
#
|
148
|
-
def enable_queues(jndi_names = {})
|
149
|
-
@enabled_queues = jndi_names
|
150
|
-
end
|
151
|
-
|
152
|
-
#
|
153
|
-
# Use:
|
154
|
-
#
|
155
|
-
# enable_topics :a_topic_name_in_lower_case => "jndi_name_of_a_topic",
|
156
|
-
# :other_topic_name_in_lower_case => "jndi_name_of_other_topic"
|
157
|
-
#
|
158
|
-
def enable_topics(jndi_names = {})
|
159
|
-
@enabled_topics = jndi_names
|
160
|
-
end
|
167
|
+
attr_accessor :jms_client_jar, :jndi_properties,
|
168
|
+
:enabled_connection_factories, :enabled_queues, :enabled_topics
|
161
169
|
end
|
162
170
|
|
163
171
|
#
|
data/lib/messaging.rb
CHANGED
@@ -100,32 +100,48 @@ module JSparrow
|
|
100
100
|
#
|
101
101
|
# Receptor de mensagens.
|
102
102
|
#
|
103
|
-
class Receiver < Base
|
103
|
+
class Receiver < Base
|
104
104
|
def receive_message(criteria_for_receiving = {:timeout => DEFAULT_RECEIVER_TIMEOUT, :selector => ''}, &message_handler)
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
105
|
+
receive(:one_message, criteria_for_receiving, &message_handler)
|
106
|
+
end
|
107
|
+
|
108
|
+
def receive_messages(criteria_for_receiving = {:timeout => DEFAULT_RECEIVER_TIMEOUT, :selector => ''}, &message_handler)
|
109
|
+
receive(:many_messages, criteria_for_receiving, &message_handler)
|
110
|
+
end
|
111
|
+
|
112
|
+
# --- Private methods --- #
|
113
|
+
private
|
114
|
+
|
115
|
+
def receive(how_much_messages, criteria_for_receiving, &message_handler)
|
116
|
+
# Cria uma conexao, uma sessao e um consumidor de qualquer tipo de mensagem
|
117
|
+
connection = @connection_factory.create_connection
|
118
|
+
session = connection.create_session(false, Session::AUTO_ACKNOWLEDGE)
|
119
|
+
consumer = session.create_consumer(@destination, criteria_for_receiving[:selector])
|
109
120
|
|
110
|
-
|
111
|
-
|
121
|
+
# Prepara a conexao para receber mensagens
|
122
|
+
connection.start
|
112
123
|
|
113
|
-
|
114
|
-
|
124
|
+
# Inicia o recebimento de mensagens
|
125
|
+
timeout = criteria_for_receiving[:timeout] || DEFAULT_RECEIVER_TIMEOUT
|
126
|
+
|
127
|
+
# Uma (if) mensagem ou muitas (while) mensagens?
|
128
|
+
conditional_keyword = (how_much_messages.eql? :one_message) ? 'if' : 'while'
|
115
129
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
130
|
+
eval %Q{
|
131
|
+
#{conditional_keyword} (received_message = consumer.receive(timeout))
|
132
|
+
# Inclui o modulo de identificacao de mensagem, util para o message_handler
|
133
|
+
class << received_message
|
134
|
+
include MessageType
|
135
|
+
end
|
136
|
+
|
137
|
+
# Delega o tratamento da mensagem para o bloco recebido
|
138
|
+
message_handler.call(received_message)
|
139
|
+
end
|
140
|
+
}
|
121
141
|
|
122
|
-
#
|
123
|
-
|
142
|
+
# Fecha a conexao
|
143
|
+
connection.close
|
124
144
|
end
|
125
|
-
|
126
|
-
# Fecha a conexao
|
127
|
-
connection.close
|
128
|
-
end
|
129
145
|
end
|
130
146
|
|
131
147
|
#
|
data/sample/sample_queue.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'jsparrow'
|
3
3
|
|
4
|
-
JSparrow::Connection.configure do
|
5
|
-
|
4
|
+
JSparrow::Connection.configure do
|
5
|
+
use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
use_jndi_properties :initial_context_factory => 'org.exolab.jms.jndi.InitialContextFactory',
|
8
|
+
:provider_url => 'tcp://localhost:3035'
|
9
|
+
# :security_principal => 'user',
|
10
|
+
# :security_credentials => 'password'
|
11
11
|
|
12
|
-
|
12
|
+
enable_connection_factories :queue_connection_factory => 'ConnectionFactory'
|
13
13
|
|
14
|
-
|
14
|
+
enable_queues :test_queue => 'TestQueue'
|
15
15
|
end
|
16
16
|
|
17
17
|
jms_client = JSparrow::Connection.new_client
|
data/sample/sample_topic.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'jsparrow'
|
3
3
|
|
4
|
-
JSparrow::Connection.configure do
|
5
|
-
|
4
|
+
JSparrow::Connection.configure do
|
5
|
+
use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
use_jndi_properties :initial_context_factory => 'org.exolab.jms.jndi.InitialContextFactory',
|
8
|
+
:provider_url => 'tcp://localhost:3035'
|
9
|
+
# :security_principal => 'user',
|
10
|
+
# :security_credentials => 'password'
|
11
11
|
|
12
|
-
|
12
|
+
enable_connection_factories :topic_connection_factory => 'ConnectionFactory'
|
13
13
|
|
14
|
-
|
14
|
+
enable_topics :test_topic => 'TestTopic'
|
15
15
|
end
|
16
16
|
|
17
17
|
jms_client = JSparrow::Connection.new_client
|
data/spec/messaging_spec.rb
CHANGED
@@ -66,7 +66,23 @@ describe JSparrow::Messaging do
|
|
66
66
|
received_id_long.should eql my_id_long
|
67
67
|
end
|
68
68
|
|
69
|
-
|
69
|
+
it 'should send a message and next receive it with criterias' do
|
70
|
+
my_text = 'Mensagem de texto enviada da spec'
|
71
|
+
|
72
|
+
@sender.send_text_message(my_text) do |msg|
|
73
|
+
msg.set_string_property('recipient', 'jsparrow-spec')
|
74
|
+
end
|
75
|
+
|
76
|
+
received_text = nil
|
77
|
+
|
78
|
+
@receiver.receive_message(:timeout => 1000, :selector => "recipient = 'jsparrow-spec'") do |msg|
|
79
|
+
received_text = msg.text
|
80
|
+
end
|
81
|
+
|
82
|
+
received_text.should eql my_text
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should send messages a lot and next receive it with criterias' do
|
70
86
|
my_text = 'Mensagem de texto enviada da spec'
|
71
87
|
|
72
88
|
my_object = java.util.ArrayList.new
|
@@ -99,7 +115,7 @@ describe JSparrow::Messaging do
|
|
99
115
|
received_object = nil
|
100
116
|
received_id_long = nil
|
101
117
|
|
102
|
-
@receiver.
|
118
|
+
@receiver.receive_messages(:timeout => 1000, :selector => "recipient = 'jsparrow-spec'") do |msg|
|
103
119
|
if msg.is_text_message?
|
104
120
|
received_text = msg.text
|
105
121
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,20 +9,20 @@ require File.dirname(File.expand_path(__FILE__)) + '/../lib/jsparrow.rb'
|
|
9
9
|
module JSparrowHelperMethods
|
10
10
|
|
11
11
|
def configure_connection
|
12
|
-
JSparrow::Connection.configure do
|
13
|
-
|
12
|
+
JSparrow::Connection.configure do
|
13
|
+
use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
use_jndi_properties :initial_context_factory => 'org.exolab.jms.jndi.InitialContextFactory',
|
16
|
+
:provider_url => 'tcp://localhost:3035'
|
17
|
+
# :security_principal => 'user',
|
18
|
+
# :security_credentials => 'password'
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
enable_connection_factories :queue_connection_factory => 'ConnectionFactory',
|
21
|
+
:topic_connection_factory => 'ConnectionFactory'
|
22
22
|
|
23
|
-
|
23
|
+
enable_queues :test_queue => 'TestQueue'
|
24
24
|
|
25
|
-
|
25
|
+
enable_topics :test_topic => 'TestTopic'
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|