jsparrow 1.1.1 → 1.1.2
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 +1 -1
- data/VERSION.yml +2 -2
- data/lib/client.rb +19 -77
- data/lib/connection.rb +133 -6
- data/lib/jsparrow.rb +1 -0
- data/lib/listener.rb +146 -0
- data/lib/messaging.rb +24 -120
- data/sample/{sample.rb → sample_queue.rb} +0 -0
- data/sample/sample_topic.rb +33 -0
- data/spec/client_spec.rb +54 -71
- data/spec/connection_spec.rb +37 -21
- data/spec/listener_spec.rb +40 -0
- data/spec/messaging_spec.rb +86 -107
- data/spec/spec_helper.rb +49 -9
- metadata +39 -35
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'jsparrow'
|
3
|
+
|
4
|
+
JSparrow::Connection.configure do |connection|
|
5
|
+
connection.use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
6
|
+
|
7
|
+
connection.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
|
+
|
12
|
+
connection.enable_connection_factories :topic_connection_factory => 'ConnectionFactory'
|
13
|
+
|
14
|
+
connection.enable_topics :test_topic => 'TestTopic'
|
15
|
+
end
|
16
|
+
|
17
|
+
jms_client = JSparrow::Connection.new_client
|
18
|
+
jms_client.start
|
19
|
+
|
20
|
+
jms_client.topic_sender(:test_topic).send_text_message('jsparrow rocks!') do |msg|
|
21
|
+
msg.set_string_property('recipient', 'jsparrow-example')
|
22
|
+
end
|
23
|
+
|
24
|
+
jms_client.topic_receiver(:test_topic).receive_message(
|
25
|
+
:timeout => 5000,
|
26
|
+
:selector => "recipient = 'jsparrow-example'"
|
27
|
+
) do |msg|
|
28
|
+
|
29
|
+
puts "is text message? #{msg.is_text_message?}" # is text message? true
|
30
|
+
puts "message: #{msg.text}" # message: jsparrow rocks!
|
31
|
+
end
|
32
|
+
|
33
|
+
jms_client.stop
|
data/spec/client_spec.rb
CHANGED
@@ -1,86 +1,69 @@
|
|
1
1
|
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
|
4
|
-
# Cenario que testa o start e stop do cliente JMS.
|
5
|
-
#
|
6
|
-
describe JSparrow::Connection::Client, ', quando criado,' do
|
3
|
+
describe JSparrow::Connection::Client do
|
7
4
|
|
8
|
-
|
9
|
-
|
5
|
+
subject do
|
6
|
+
create_jms_client
|
10
7
|
end
|
11
8
|
|
12
|
-
|
13
|
-
|
9
|
+
context 'When created' do
|
10
|
+
|
11
|
+
it 'should be started and stoped' do
|
12
|
+
subject.start
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
subject.is_started?.should be true
|
15
|
+
subject.is_stoped?.should be false
|
17
16
|
|
18
|
-
|
17
|
+
subject.stop
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
#
|
26
|
-
# Cenario de configuracao do cliente JMS, quando sao informadas as propriedades de ambiente
|
27
|
-
# para conexao com o servidor de aplicacoes e a inicializacao do contexto JNDI inicial,
|
28
|
-
# onde estao criadas as connection factories, queues e topics.
|
29
|
-
#
|
30
|
-
# Importante: nesse momento o cliente JMS ainda nao sera iniciado, ja que nao deve haver
|
31
|
-
# configuracao depois inicia-lo.
|
32
|
-
#
|
33
|
-
describe JSparrow::Connection::Client, ', quando esta sendo configurado, mas ainda nao iniciado,' do
|
34
|
-
|
35
|
-
before(:all) do
|
36
|
-
@jms_client = create_jms_client
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'deveria ter uma connection factory especifica para queues' do
|
40
|
-
@jms_client.queue_connection_factory_enabled?.should be true
|
41
|
-
end
|
19
|
+
subject.is_started?.should be false
|
20
|
+
subject.is_stoped?.should be true
|
21
|
+
end
|
42
22
|
|
43
|
-
|
44
|
-
|
45
|
-
|
23
|
+
it 'should not be started if already is' do
|
24
|
+
subject.start
|
25
|
+
|
26
|
+
lambda {
|
27
|
+
subject.start
|
28
|
+
}.should raise_error JSparrow::Connection::InvalidStateError
|
29
|
+
|
30
|
+
subject.stop
|
31
|
+
end
|
46
32
|
|
47
|
-
|
48
|
-
|
33
|
+
it 'should not be stoped if already is' do
|
34
|
+
subject.start
|
35
|
+
subject.stop
|
36
|
+
|
37
|
+
lambda {
|
38
|
+
subject.stop
|
39
|
+
}.should raise_error JSparrow::Connection::InvalidStateError
|
40
|
+
end
|
49
41
|
end
|
50
42
|
|
51
|
-
|
52
|
-
@jms_client.topic_enabled?(:test_topic).should eql true
|
53
|
-
end
|
54
|
-
end
|
43
|
+
context 'When started' do
|
55
44
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
#
|
60
|
-
describe JSparrow::Connection::Client, ', depois de ter sido configurado,' do
|
45
|
+
before(:all) do
|
46
|
+
subject.start
|
47
|
+
end
|
61
48
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'deveria possibilitar obter um Receiver para um Topic especifico' do
|
84
|
-
@jms_client.topic_receiver(:test_topic).should_not be nil
|
49
|
+
after(:all) do
|
50
|
+
subject.stop
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should allow get a Sender for a Queue' do
|
54
|
+
subject.queue_sender(:test_queue).should_not be nil
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should allow get a Receiver for a Queue' do
|
58
|
+
subject.queue_receiver(:test_queue).should_not be nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should allow get a Sender for a Topic' do
|
62
|
+
subject.topic_sender(:test_topic).should_not be nil
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should allow get a Receiver for a Topic' do
|
66
|
+
subject.topic_receiver(:test_topic).should_not be nil
|
67
|
+
end
|
85
68
|
end
|
86
|
-
end
|
69
|
+
end
|
data/spec/connection_spec.rb
CHANGED
@@ -1,31 +1,47 @@
|
|
1
1
|
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
|
4
|
-
# Cenario que testa a configuracao a conexao com o provedor de JMS.
|
5
|
-
#
|
6
|
-
describe JSparrow::Connection, ', quando configurado,' do
|
3
|
+
describe JSparrow::Connection do
|
7
4
|
|
8
|
-
|
9
|
-
|
5
|
+
subject do
|
6
|
+
configure_connection
|
10
7
|
end
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
context 'When configured' do
|
10
|
+
|
11
|
+
it 'should known a jms_client_jar' do
|
12
|
+
subject.jms_client_jar.should_not be nil
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
it 'should known a jndi_properties' do
|
16
|
+
subject.jndi_properties.should_not be nil
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
it 'should have the queue_connection_factory enabled' do
|
20
|
+
subject.enabled_connection_factories[:queue_connection_factory].should_not be nil
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
it 'should have the topic_connection_factory enabled' do
|
24
|
+
subject.enabled_connection_factories[:topic_connection_factory].should_not be nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should have the test_queue enabled' do
|
28
|
+
subject.enabled_queues[:test_queue].should_not be nil
|
29
|
+
end
|
27
30
|
|
28
|
-
|
29
|
-
|
31
|
+
it 'should have the test_topic enabled' do
|
32
|
+
subject.enabled_topics[:test_topic].should_not be nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should allow create a new Client' do
|
36
|
+
jms_client = create_jms_client
|
37
|
+
|
38
|
+
jms_client.class.should be JSparrow::Connection::Client
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should allow create a new Listener' do
|
42
|
+
jms_listener = create_jms_listener
|
43
|
+
|
44
|
+
jms_listener.class.superclass.should be JSparrow::Connection::Listener
|
45
|
+
end
|
30
46
|
end
|
31
|
-
end
|
47
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe JSparrow::Connection::Listener do
|
4
|
+
|
5
|
+
subject do
|
6
|
+
create_jms_listener
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'When inherited for listening a queue' do
|
10
|
+
|
11
|
+
it 'should listen to "test_queue" destination' do
|
12
|
+
subject.listen_to_destination.should eql :queue => :test_queue
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'When inherited and created,' do
|
17
|
+
|
18
|
+
it 'should be started and stoped' do
|
19
|
+
subject.start_listening
|
20
|
+
|
21
|
+
subject.is_listening?.should be true
|
22
|
+
|
23
|
+
subject.stop_listening
|
24
|
+
|
25
|
+
subject.is_listening?.should be false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should receive a message' do
|
29
|
+
send_message_to_listener 'TestQueueListener'
|
30
|
+
|
31
|
+
subject.start_listening
|
32
|
+
|
33
|
+
sleep 1 # espera um pouquinho pra mensagem ser entregue
|
34
|
+
|
35
|
+
subject.received_messages.size.should eql 1
|
36
|
+
|
37
|
+
subject.stop_listening
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/messaging_spec.rb
CHANGED
@@ -1,142 +1,121 @@
|
|
1
1
|
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# individualmente ou em lote.
|
7
|
-
#
|
8
|
-
describe JSparrow::Messaging, ', quando tem um Sender e um Receiver para uma Queue especifica,' do
|
3
|
+
describe JSparrow::Messaging do
|
4
|
+
|
5
|
+
context 'When have a Sender and a Receiver for a Queue' do
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
before(:all) do
|
8
|
+
@jms_client = create_jms_client
|
9
|
+
@jms_client.start
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
@sender = @jms_client.queue_sender(:test_queue)
|
12
|
+
@receiver = @jms_client.queue_receiver(:test_queue)
|
13
|
+
end
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
after(:all) do
|
16
|
+
@jms_client.stop
|
17
|
+
end
|
21
18
|
|
22
|
-
|
23
|
-
|
19
|
+
it 'should send a text_message and next receive it' do
|
20
|
+
my_text = 'Mensagem de texto enviada da spec'
|
24
21
|
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
@sender.send_text_message(my_text) do |msg|
|
23
|
+
msg.set_string_property('recipient', 'jsparrow-spec')
|
24
|
+
end
|
28
25
|
|
29
|
-
|
26
|
+
received_text = nil
|
30
27
|
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
@receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
|
29
|
+
received_text = msg.text
|
30
|
+
end
|
34
31
|
|
35
|
-
|
36
|
-
|
32
|
+
received_text.should eql my_text
|
33
|
+
end
|
37
34
|
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
it 'should send a object_message and next receive it' do
|
36
|
+
my_object = java.util.ArrayList.new
|
37
|
+
my_object << 'Obj1 enviado da spec'
|
41
38
|
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
@sender.send_object_message(my_object) do |msg|
|
40
|
+
msg.set_string_property('recipient', 'jsparrow-spec')
|
41
|
+
end
|
45
42
|
|
46
|
-
|
43
|
+
received_object = nil
|
47
44
|
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
@receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
|
46
|
+
received_object = msg.object
|
47
|
+
end
|
51
48
|
|
52
|
-
|
53
|
-
|
49
|
+
received_object.should eql my_object
|
50
|
+
end
|
54
51
|
|
55
|
-
|
56
|
-
|
52
|
+
it 'should send a map_message and next receive it' do
|
53
|
+
my_id_long = 1234567
|
57
54
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
@sender.send_map_message do |msg|
|
56
|
+
msg.set_string_property('recipient', 'jsparrow-spec')
|
57
|
+
msg.set_long('id', my_id_long)
|
58
|
+
end
|
62
59
|
|
63
|
-
|
60
|
+
received_id_long = nil
|
64
61
|
|
65
|
-
|
66
|
-
|
67
|
-
|
62
|
+
@receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
|
63
|
+
received_id_long = msg.get_long('id')
|
64
|
+
end
|
68
65
|
|
69
|
-
|
70
|
-
|
66
|
+
received_id_long.should eql my_id_long
|
67
|
+
end
|
71
68
|
|
72
|
-
|
73
|
-
|
69
|
+
it 'should send messages a lot and next receive it' do
|
70
|
+
my_text = 'Mensagem de texto enviada da spec'
|
74
71
|
|
75
|
-
|
76
|
-
|
72
|
+
my_object = java.util.ArrayList.new
|
73
|
+
my_object << 'Obj1 enviado da spec'
|
77
74
|
|
78
|
-
|
75
|
+
my_id_long = 1234567
|
79
76
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
77
|
+
@sender.send_messages do |session, producer|
|
78
|
+
#--- TextMessage
|
79
|
+
text_message = session.create_text_message(my_text)
|
80
|
+
text_message.set_string_property('recipient', 'jsparrow-spec')
|
81
|
+
producer.send(text_message)
|
85
82
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
83
|
+
#--- objectMessage
|
84
|
+
object_message = session.create_object_message(my_object)
|
85
|
+
object_message.set_string_property('recipient', 'jsparrow-spec')
|
86
|
+
producer.send(object_message)
|
90
87
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
88
|
+
#--- MapMessage
|
89
|
+
map_message = session.create_map_message
|
90
|
+
map_message.set_string_property('recipient', 'jsparrow-spec')
|
91
|
+
map_message.set_long('id', my_id_long)
|
92
|
+
producer.send(map_message)
|
96
93
|
|
97
|
-
|
98
|
-
|
99
|
-
|
94
|
+
# Commita as tres mensagens enviadas na sessao
|
95
|
+
session.commit
|
96
|
+
end
|
100
97
|
|
101
|
-
|
102
|
-
|
103
|
-
|
98
|
+
received_text = nil
|
99
|
+
received_object = nil
|
100
|
+
received_id_long = nil
|
104
101
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
102
|
+
@receiver.receive_message(:timeout => 1000, :selector => "recipient = 'jsparrow-spec'") do |msg|
|
103
|
+
if msg.is_text_message?
|
104
|
+
received_text = msg.text
|
105
|
+
end
|
109
106
|
|
110
|
-
|
111
|
-
|
112
|
-
|
107
|
+
if msg.is_object_message?
|
108
|
+
received_object = msg.object
|
109
|
+
end
|
113
110
|
|
114
|
-
|
115
|
-
|
111
|
+
if msg.is_map_message?
|
112
|
+
received_id_long = msg.get_long('id')
|
113
|
+
end
|
116
114
|
end
|
117
|
-
end
|
118
115
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
end
|
124
|
-
|
125
|
-
#
|
126
|
-
# Cenario pos-configuracao do cliente JMS, quando deve ser possivel escutar mensagens
|
127
|
-
# atraves de objetos listeners.
|
128
|
-
#
|
129
|
-
describe JSparrow::Messaging::Listener,
|
130
|
-
', quando um Listener se registra para escutar uma Queue especifica,' do
|
131
|
-
|
132
|
-
before(:all) do
|
133
|
-
@jms_client = create_jms_client
|
134
|
-
@jms_client.start
|
135
|
-
end
|
136
|
-
|
137
|
-
after(:all) do
|
138
|
-
@jms_client.stop
|
116
|
+
received_text.should eql my_text
|
117
|
+
received_object.should eql my_object
|
118
|
+
received_id_long.should eql my_id_long
|
119
|
+
end
|
139
120
|
end
|
140
|
-
|
141
|
-
it 'deveria possibilitar escutar mensagens atraves de um listener'
|
142
121
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,15 +8,6 @@ require File.dirname(File.expand_path(__FILE__)) + '/../lib/jsparrow.rb'
|
|
8
8
|
#
|
9
9
|
module JSparrowHelperMethods
|
10
10
|
|
11
|
-
#
|
12
|
-
# Apenas cria, mas nao faz o setup do cliente JMS.
|
13
|
-
#
|
14
|
-
def create_jms_client
|
15
|
-
configure_connection
|
16
|
-
|
17
|
-
JSparrow::Connection.new_client
|
18
|
-
end
|
19
|
-
|
20
11
|
def configure_connection
|
21
12
|
JSparrow::Connection.configure do |connection|
|
22
13
|
connection.use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
@@ -34,8 +25,57 @@ module JSparrowHelperMethods
|
|
34
25
|
connection.enable_topics :test_topic => 'TestTopic'
|
35
26
|
end
|
36
27
|
end
|
28
|
+
|
29
|
+
def create_jms_client
|
30
|
+
configure_connection
|
31
|
+
|
32
|
+
JSparrow::Connection.new_client
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_jms_listener
|
36
|
+
configure_connection
|
37
|
+
|
38
|
+
JSparrow::Connection.new_listener :as => JSparrowHelperClasses::TestQueueListener
|
39
|
+
end
|
40
|
+
|
41
|
+
def send_message_to_listener(listener_name)
|
42
|
+
@jms_client = create_jms_client
|
43
|
+
@jms_client.start
|
44
|
+
|
45
|
+
my_text = 'Mensagem de texto enviada da spec para o listener TestQueueListener'
|
46
|
+
|
47
|
+
@jms_client.queue_sender(:test_queue).send_text_message(my_text) do |msg|
|
48
|
+
msg.set_string_property('recipient', 'jsparrow-spec')
|
49
|
+
msg.set_string_property('to_listener', listener_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
@jms_client.stop
|
53
|
+
end
|
37
54
|
end
|
38
55
|
|
56
|
+
module JSparrowHelperClasses
|
57
|
+
|
58
|
+
#
|
59
|
+
# Listener da queue TestQueue
|
60
|
+
#
|
61
|
+
class TestQueueListener < JSparrow::Connection::Listener
|
62
|
+
listen_to :queue => :test_queue
|
63
|
+
|
64
|
+
receive_only_in_criteria :selector => "recipient = 'jsparrow-spec' and to_listener = 'TestQueueListener'"
|
65
|
+
|
66
|
+
attr_reader :received_messages
|
67
|
+
|
68
|
+
def initialize(connection)
|
69
|
+
super(connection)
|
70
|
+
|
71
|
+
@received_messages = []
|
72
|
+
end
|
73
|
+
|
74
|
+
def on_receive_message(received_message)
|
75
|
+
@received_messages << received_messages
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
39
79
|
#
|
40
80
|
# Enriquece a classe Spec::Example::ExampleGroup com o helper.
|
41
81
|
#
|