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.
@@ -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
- before(:all) do
9
- @jms_client = create_jms_client
5
+ subject do
6
+ create_jms_client
10
7
  end
11
8
 
12
- it 'deveria permitir ser iniciado e parado' do
13
- @jms_client.start
9
+ context 'When created' do
10
+
11
+ it 'should be started and stoped' do
12
+ subject.start
14
13
 
15
- @jms_client.is_started?.should be true
16
- @jms_client.is_stoped?.should be false
14
+ subject.is_started?.should be true
15
+ subject.is_stoped?.should be false
17
16
 
18
- @jms_client.stop
17
+ subject.stop
19
18
 
20
- @jms_client.is_started?.should be false
21
- @jms_client.is_stoped?.should be true
22
- end
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
- it 'deveria ter uma connection factory especifica para topics' do
44
- @jms_client.topic_connection_factory_enabled?.should be true
45
- end
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
- it 'deveria ter uma Queue especifica' do
48
- @jms_client.queue_enabled?(:test_queue).should eql true
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
- it 'deveria ter um Topic especifico' do
52
- @jms_client.topic_enabled?(:test_topic).should eql true
53
- end
54
- end
43
+ context 'When started' do
55
44
 
56
- #
57
- # Cenario pos-configuracao do cliente JMS, quando as queues e os topicos ja devem estar
58
- # disponiveis, e entao e possivel obter sender/receiver para elas.
59
- #
60
- describe JSparrow::Connection::Client, ', depois de ter sido configurado,' do
45
+ before(:all) do
46
+ subject.start
47
+ end
61
48
 
62
- before(:all) do
63
- @jms_client = create_jms_client
64
- @jms_client.start
65
- end
66
-
67
- after(:all) do
68
- @jms_client.stop
69
- end
70
-
71
- it 'deveria possibilitar obter um Sender para uma Queue especifica' do
72
- @jms_client.queue_sender(:test_queue).should_not be nil
73
- end
74
-
75
- it 'deveria possibilitar obter um Receiver para uma Queue especifica' do
76
- @jms_client.queue_receiver(:test_queue).should_not be nil
77
- end
78
-
79
- it 'deveria possibilitar obter um Sender para um Topic especifico' do
80
- @jms_client.topic_sender(:test_topic).should_not be nil
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
@@ -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
- before(:all) do
9
- @config = configure_connection
5
+ subject do
6
+ configure_connection
10
7
  end
11
8
 
12
- it 'deveria ter jms_client_jar' do
13
- @config.jms_client_jar.should_not be nil
14
- end
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
- it 'deveria ter jndi_properties' do
17
- @config.jndi_properties.should_not be nil
18
- end
15
+ it 'should known a jndi_properties' do
16
+ subject.jndi_properties.should_not be nil
17
+ end
19
18
 
20
- it 'deveria ter enabled_connection_factories' do
21
- @config.enabled_connection_factories.should_not be nil
22
- end
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
- it 'deveria ter enabled_queues' do
25
- @config.enabled_queues.should_not be nil
26
- end
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
- it 'deveria ter enabled_topics' do
29
- @config.enabled_topics.should_not be nil
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
@@ -1,142 +1,121 @@
1
1
  require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
2
2
 
3
- #
4
- # Cenario pos-obtencao de um Sender e um Receiver, quando deve ser possivel enviar e
5
- # receber mensagens de tres tipos (Texto, Objeto e Mapa) de uma queue especifica,
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
- before(:all) do
11
- @jms_client = create_jms_client
12
- @jms_client.start
7
+ before(:all) do
8
+ @jms_client = create_jms_client
9
+ @jms_client.start
13
10
 
14
- @sender = @jms_client.queue_sender(:test_queue)
15
- @receiver = @jms_client.queue_receiver(:test_queue)
16
- end
11
+ @sender = @jms_client.queue_sender(:test_queue)
12
+ @receiver = @jms_client.queue_receiver(:test_queue)
13
+ end
17
14
 
18
- after(:all) do
19
- @jms_client.stop
20
- end
15
+ after(:all) do
16
+ @jms_client.stop
17
+ end
21
18
 
22
- it 'deveria possibilitar enviar uma mensagem de texto e recebe-la' do
23
- my_text = 'Mensagem de texto enviada da spec'
19
+ it 'should send a text_message and next receive it' do
20
+ my_text = 'Mensagem de texto enviada da spec'
24
21
 
25
- @sender.send_text_message(my_text) do |msg|
26
- msg.set_string_property('recipient', 'jsparrow-spec')
27
- end
22
+ @sender.send_text_message(my_text) do |msg|
23
+ msg.set_string_property('recipient', 'jsparrow-spec')
24
+ end
28
25
 
29
- received_text = nil
26
+ received_text = nil
30
27
 
31
- @receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
32
- received_text = msg.text
33
- end
28
+ @receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
29
+ received_text = msg.text
30
+ end
34
31
 
35
- received_text.should eql my_text
36
- end
32
+ received_text.should eql my_text
33
+ end
37
34
 
38
- it 'deveria possibilitar enviar um objeto e recebe-lo' do
39
- my_object = java.util.ArrayList.new
40
- my_object << 'Obj1 enviado da spec'
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
- @sender.send_object_message(my_object) do |msg|
43
- msg.set_string_property('recipient', 'jsparrow-spec')
44
- end
39
+ @sender.send_object_message(my_object) do |msg|
40
+ msg.set_string_property('recipient', 'jsparrow-spec')
41
+ end
45
42
 
46
- received_object = nil
43
+ received_object = nil
47
44
 
48
- @receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
49
- received_object = msg.object
50
- end
45
+ @receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
46
+ received_object = msg.object
47
+ end
51
48
 
52
- received_object.should eql my_object
53
- end
49
+ received_object.should eql my_object
50
+ end
54
51
 
55
- it 'deveria possibilitar enviar uma mensagem mapa e recebe-la' do
56
- my_id_long = 1234567
52
+ it 'should send a map_message and next receive it' do
53
+ my_id_long = 1234567
57
54
 
58
- @sender.send_map_message do |msg|
59
- msg.set_string_property('recipient', 'jsparrow-spec')
60
- msg.set_long('id', my_id_long)
61
- end
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
- received_id_long = nil
60
+ received_id_long = nil
64
61
 
65
- @receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
66
- received_id_long = msg.get_long('id')
67
- end
62
+ @receiver.receive_message(:selector => "recipient = 'jsparrow-spec'") do |msg|
63
+ received_id_long = msg.get_long('id')
64
+ end
68
65
 
69
- received_id_long.should eql my_id_long
70
- end
66
+ received_id_long.should eql my_id_long
67
+ end
71
68
 
72
- it 'deveria possibilitar enviar varias mensagens de qualquer tipo e recebe-las' do
73
- my_text = 'Mensagem de texto enviada da spec'
69
+ it 'should send messages a lot and next receive it' do
70
+ my_text = 'Mensagem de texto enviada da spec'
74
71
 
75
- my_object = java.util.ArrayList.new
76
- my_object << 'Obj1 enviado da spec'
72
+ my_object = java.util.ArrayList.new
73
+ my_object << 'Obj1 enviado da spec'
77
74
 
78
- my_id_long = 1234567
75
+ my_id_long = 1234567
79
76
 
80
- @sender.send_messages do |session, producer|
81
- #--- TextMessage
82
- text_message = session.create_text_message(my_text)
83
- text_message.set_string_property('recipient', 'jsparrow-spec')
84
- producer.send(text_message)
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
- #--- objectMessage
87
- object_message = session.create_object_message(my_object)
88
- object_message.set_string_property('recipient', 'jsparrow-spec')
89
- producer.send(object_message)
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
- #--- MapMessage
92
- map_message = session.create_map_message
93
- map_message.set_string_property('recipient', 'jsparrow-spec')
94
- map_message.set_long('id', my_id_long)
95
- producer.send(map_message)
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
- # Commita as tres mensagens enviadas na sessao
98
- session.commit
99
- end
94
+ # Commita as tres mensagens enviadas na sessao
95
+ session.commit
96
+ end
100
97
 
101
- received_text = nil
102
- received_object = nil
103
- received_id_long = nil
98
+ received_text = nil
99
+ received_object = nil
100
+ received_id_long = nil
104
101
 
105
- @receiver.receive_message(:timeout => 1000, :selector => "recipient = 'jsparrow-spec'") do |msg|
106
- if msg.is_text_message?
107
- received_text = msg.text
108
- end
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
- if msg.is_object_message?
111
- received_object = msg.object
112
- end
107
+ if msg.is_object_message?
108
+ received_object = msg.object
109
+ end
113
110
 
114
- if msg.is_map_message?
115
- received_id_long = msg.get_long('id')
111
+ if msg.is_map_message?
112
+ received_id_long = msg.get_long('id')
113
+ end
116
114
  end
117
- end
118
115
 
119
- received_text.should eql my_text
120
- received_object.should eql my_object
121
- received_id_long.should eql my_id_long
122
- end
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
  #