jsparrow 1.1.3 → 1.1.4
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 +3 -3
- data/Rakefile +5 -0
- data/VERSION.yml +1 -1
- data/lib/connection.rb +2 -250
- data/lib/connection/configuration.rb +88 -0
- data/lib/connection/provider.rb +91 -0
- data/lib/interaction.rb +4 -0
- data/lib/{client.rb → interaction/client.rb} +17 -21
- data/lib/interaction/interactors.rb +70 -0
- data/lib/{listener.rb → interaction/listener.rb} +65 -77
- data/lib/{messaging.rb → interaction/messaging.rb} +20 -48
- data/lib/javaee.rb +164 -0
- data/lib/{jms → javaee}/javaee-1.5.jar +0 -0
- data/lib/{jms → javaee}/jms.jar +0 -0
- data/lib/{jms → javaee}/jsparrow-essential.jar +0 -0
- data/lib/jsparrow.rb +4 -8
- data/sample/sample_queue.rb +2 -2
- data/sample/sample_topic.rb +2 -2
- data/spec/client_spec.rb +4 -4
- data/spec/connection_spec.rb +13 -6
- data/spec/listener_spec.rb +43 -8
- data/spec/messaging_spec.rb +9 -9
- data/spec/spec_helper.rb +34 -18
- metadata +13 -9
- data/lib/jms.rb +0 -14
data/lib/javaee.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# Libs directory definition
|
2
|
+
JEE_LIB_DIR = File.expand_path(File.dirname(__FILE__)) + '/javaee'
|
3
|
+
|
4
|
+
require 'java'
|
5
|
+
require "#{JEE_LIB_DIR}/jsparrow-essential.jar"
|
6
|
+
require "#{JEE_LIB_DIR}/javaee-1.5.jar"
|
7
|
+
require "#{JEE_LIB_DIR}/jms.jar"
|
8
|
+
|
9
|
+
module JSparrow
|
10
|
+
module JMS
|
11
|
+
|
12
|
+
module Session
|
13
|
+
#
|
14
|
+
# Sobrescreve metodos de instancia.
|
15
|
+
#
|
16
|
+
module OverrideMethods
|
17
|
+
def create_text_message(text_message)
|
18
|
+
enriches_message super(text_message)
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_object_message(object_message)
|
22
|
+
enriches_message super(object_message)
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_map_message
|
26
|
+
enriches_message super
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_consumer(destination, criteria_for_receiving)
|
30
|
+
enriches_consumer super(destination, criteria_for_receiving)
|
31
|
+
end
|
32
|
+
|
33
|
+
# --- Private methods -- #
|
34
|
+
private
|
35
|
+
|
36
|
+
def enriches_message(message)
|
37
|
+
class << message
|
38
|
+
include Message::TypingMethods
|
39
|
+
include Message::CriteriaMethods
|
40
|
+
end
|
41
|
+
|
42
|
+
message
|
43
|
+
end
|
44
|
+
|
45
|
+
def enriches_consumer(consumer)
|
46
|
+
class << consumer
|
47
|
+
include Consumer::OverrideMethods
|
48
|
+
end
|
49
|
+
|
50
|
+
consumer
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module Consumer
|
56
|
+
#
|
57
|
+
# Sobrescreve metodos de instancia.
|
58
|
+
#
|
59
|
+
module OverrideMethods
|
60
|
+
def receive(timeout)
|
61
|
+
received_message = super(timeout)
|
62
|
+
|
63
|
+
if received_message.nil?
|
64
|
+
received_message
|
65
|
+
else
|
66
|
+
enriches_message received_message
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# --- Private methods -- #
|
71
|
+
private
|
72
|
+
|
73
|
+
def enriches_message(message)
|
74
|
+
class << message
|
75
|
+
include Message::TypingMethods
|
76
|
+
end
|
77
|
+
|
78
|
+
message
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module Message
|
84
|
+
#
|
85
|
+
# Identifica o tipo de uma mensagem.
|
86
|
+
#
|
87
|
+
module TypingMethods
|
88
|
+
def is_text_message?
|
89
|
+
respond_to? :get_text
|
90
|
+
end
|
91
|
+
|
92
|
+
def is_object_message?
|
93
|
+
(respond_to? :get_object and !(respond_to? :get_long))
|
94
|
+
end
|
95
|
+
|
96
|
+
def is_map_message?
|
97
|
+
respond_to? :get_long
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
#
|
102
|
+
# Adiciona criterios a mensagem.
|
103
|
+
#
|
104
|
+
module CriteriaMethods
|
105
|
+
def add_criteria_to_reception(name, value)
|
106
|
+
set_string_property(name, value)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
module JNDI
|
113
|
+
#
|
114
|
+
# Builder para construcao de contexto JNDI para conexao com o provedor
|
115
|
+
# de JMS.
|
116
|
+
#
|
117
|
+
class ContextBuilder
|
118
|
+
attr_accessor :jms_client_jar, :jndi_properties
|
119
|
+
|
120
|
+
def initialize(jms_client_jar, jndi_properties)
|
121
|
+
@jms_client_jar = jms_client_jar
|
122
|
+
@jndi_properties = jndi_properties
|
123
|
+
end
|
124
|
+
|
125
|
+
#
|
126
|
+
# Constroi um contexto JNDI inicial a partir das configuracoes atuais.
|
127
|
+
#
|
128
|
+
def build
|
129
|
+
# Carrega a biblioteca cliente do servidor de aplicacoes
|
130
|
+
require @jms_client_jar
|
131
|
+
|
132
|
+
InitialContext.new(to_jndi_environment_hashtable)
|
133
|
+
end
|
134
|
+
|
135
|
+
# --- Private methods --- #
|
136
|
+
private
|
137
|
+
|
138
|
+
#
|
139
|
+
# Cria um Hashtable Java contendo as configuracoes atuais.
|
140
|
+
#
|
141
|
+
def to_jndi_environment_hashtable
|
142
|
+
jndi_env = Hashtable.new
|
143
|
+
|
144
|
+
jndi_env.put(
|
145
|
+
InitialContext::INITIAL_CONTEXT_FACTORY,
|
146
|
+
@jndi_properties[:initial_context_factory])
|
147
|
+
|
148
|
+
jndi_env.put(
|
149
|
+
InitialContext::PROVIDER_URL,
|
150
|
+
@jndi_properties[:provider_url])
|
151
|
+
|
152
|
+
jndi_env.put(
|
153
|
+
InitialContext::SECURITY_PRINCIPAL,
|
154
|
+
@jndi_properties[:security_principal]) if @jndi_properties[:security_principal]
|
155
|
+
|
156
|
+
jndi_env.put(
|
157
|
+
InitialContext::SECURITY_CREDENTIALS,
|
158
|
+
@jndi_properties[:security_credentials]) if @jndi_properties[:security_credentials]
|
159
|
+
|
160
|
+
jndi_env
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
File without changes
|
data/lib/{jms → javaee}/jms.jar
RENAMED
File without changes
|
File without changes
|
data/lib/jsparrow.rb
CHANGED
@@ -1,11 +1,7 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(File.expand_path(__FILE__)))
|
2
2
|
|
3
|
-
|
4
|
-
require 'jms.rb'
|
3
|
+
require 'javaee'
|
5
4
|
|
6
|
-
|
7
|
-
require '
|
8
|
-
require '
|
9
|
-
require 'listener.rb'
|
10
|
-
require 'messaging.rb'
|
11
|
-
require 'error.rb'
|
5
|
+
require 'connection'
|
6
|
+
require 'interaction'
|
7
|
+
require 'error'
|
data/sample/sample_queue.rb
CHANGED
@@ -14,11 +14,11 @@ JSparrow::Connection.configure do
|
|
14
14
|
enable_queues :test_queue => 'TestQueue'
|
15
15
|
end
|
16
16
|
|
17
|
-
jms_client =
|
17
|
+
jms_client = new_jsparrow_client
|
18
18
|
jms_client.start
|
19
19
|
|
20
20
|
jms_client.queue_sender(:test_queue).send_text_message('jsparrow rocks!') do |msg|
|
21
|
-
msg.
|
21
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-example')
|
22
22
|
end
|
23
23
|
|
24
24
|
jms_client.queue_receiver(:test_queue).receive_message(
|
data/sample/sample_topic.rb
CHANGED
@@ -14,11 +14,11 @@ JSparrow::Connection.configure do
|
|
14
14
|
enable_topics :test_topic => 'TestTopic'
|
15
15
|
end
|
16
16
|
|
17
|
-
jms_client =
|
17
|
+
jms_client = new_jsparrow_client
|
18
18
|
jms_client.start
|
19
19
|
|
20
20
|
jms_client.topic_sender(:test_topic).send_text_message('jsparrow rocks!') do |msg|
|
21
|
-
msg.
|
21
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-example')
|
22
22
|
end
|
23
23
|
|
24
24
|
jms_client.topic_receiver(:test_topic).receive_message(
|
data/spec/client_spec.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
describe JSparrow::
|
3
|
+
describe JSparrow::Interactors::Client do
|
4
4
|
|
5
5
|
subject do
|
6
|
-
|
6
|
+
new_jms_client
|
7
7
|
end
|
8
8
|
|
9
|
-
context '
|
9
|
+
context 'when created' do
|
10
10
|
|
11
11
|
it 'should be started and stoped' do
|
12
12
|
subject.start
|
@@ -40,7 +40,7 @@ describe JSparrow::Connection::Client do
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
context '
|
43
|
+
context 'when started' do
|
44
44
|
|
45
45
|
before(:all) do
|
46
46
|
subject.start
|
data/spec/connection_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe JSparrow::Connection do
|
|
6
6
|
configure_connection
|
7
7
|
end
|
8
8
|
|
9
|
-
context '
|
9
|
+
context 'when configured' do
|
10
10
|
|
11
11
|
it 'should known a jms_client_jar' do
|
12
12
|
subject.jms_client_jar.should_not be nil
|
@@ -33,15 +33,22 @@ describe JSparrow::Connection do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'should allow create a new Client' do
|
36
|
-
jms_client =
|
36
|
+
jms_client = new_jms_client
|
37
37
|
|
38
|
-
jms_client.class.should be JSparrow::
|
38
|
+
jms_client.class.should be JSparrow::Interactors::Client
|
39
39
|
end
|
40
40
|
|
41
|
-
it 'should allow create a new Listener' do
|
42
|
-
jms_listener =
|
41
|
+
it 'should allow create a new named Listener' do
|
42
|
+
jms_listener = new_named_jms_listener
|
43
43
|
|
44
|
-
jms_listener.class.superclass.should be JSparrow::
|
44
|
+
jms_listener.class.superclass.should be JSparrow::Interactors::Listener
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should allow create a new anonymous Listener' do
|
48
|
+
jms_listener = new_anonymous_jms_listener
|
49
|
+
|
50
|
+
jms_listener.class.should be JSparrow::Interactors::Listener
|
51
|
+
jms_listener.respond_to?(:on_receive_message).should be true
|
45
52
|
end
|
46
53
|
end
|
47
54
|
end
|
data/spec/listener_spec.rb
CHANGED
@@ -1,19 +1,16 @@
|
|
1
1
|
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
describe JSparrow::
|
3
|
+
describe JSparrow::Interactors::Listener do
|
4
4
|
|
5
|
-
|
6
|
-
create_jms_listener
|
7
|
-
end
|
5
|
+
context 'when inherited and created' do
|
8
6
|
|
9
|
-
|
7
|
+
subject do
|
8
|
+
new_named_jms_listener
|
9
|
+
end
|
10
10
|
|
11
11
|
it 'should listen to "test_queue" destination' do
|
12
12
|
subject.listen_to_destination.should eql :queue => :test_queue
|
13
13
|
end
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'When inherited and created,' do
|
17
14
|
|
18
15
|
it 'should be started and stoped' do
|
19
16
|
subject.start_listening
|
@@ -26,6 +23,8 @@ describe JSparrow::Connection::Listener do
|
|
26
23
|
end
|
27
24
|
|
28
25
|
it 'should receive a message' do
|
26
|
+
subject.received_messages.size.should eql 0
|
27
|
+
|
29
28
|
send_message_to_listener 'TestQueueListener'
|
30
29
|
|
31
30
|
subject.start_listening
|
@@ -37,4 +36,40 @@ describe JSparrow::Connection::Listener do
|
|
37
36
|
subject.stop_listening
|
38
37
|
end
|
39
38
|
end
|
39
|
+
|
40
|
+
context 'when anonymously created' do
|
41
|
+
|
42
|
+
subject do
|
43
|
+
new_anonymous_jms_listener
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should listen to "test_queue" destination' do
|
47
|
+
subject.listen_to_destination.should eql :queue => :test_queue
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should be started and stoped' do
|
51
|
+
subject.start_listening
|
52
|
+
|
53
|
+
subject.is_listening?.should be true
|
54
|
+
|
55
|
+
subject.stop_listening
|
56
|
+
|
57
|
+
subject.is_listening?.should be false
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should receive a message' do
|
61
|
+
subject.received_messages.size.should eql 0
|
62
|
+
|
63
|
+
send_message_to_listener 'anonymous'
|
64
|
+
|
65
|
+
subject.start_listening
|
66
|
+
|
67
|
+
sleep 1 # espera um pouquinho pra mensagem ser entregue
|
68
|
+
|
69
|
+
# verify if message was received
|
70
|
+
subject.received_messages.size.should eql 1
|
71
|
+
|
72
|
+
subject.stop_listening
|
73
|
+
end
|
74
|
+
end
|
40
75
|
end
|
data/spec/messaging_spec.rb
CHANGED
@@ -2,10 +2,10 @@ require File.dirname(File.expand_path(__FILE__)) + '/spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe JSparrow::Messaging do
|
4
4
|
|
5
|
-
context '
|
5
|
+
context 'when have a Sender and a Receiver for a Queue' do
|
6
6
|
|
7
7
|
before(:all) do
|
8
|
-
@jms_client =
|
8
|
+
@jms_client = new_jms_client
|
9
9
|
@jms_client.start
|
10
10
|
|
11
11
|
@sender = @jms_client.queue_sender(:test_queue)
|
@@ -20,7 +20,7 @@ describe JSparrow::Messaging do
|
|
20
20
|
my_text = 'Mensagem de texto enviada da spec'
|
21
21
|
|
22
22
|
@sender.send_text_message(my_text) do |msg|
|
23
|
-
msg.
|
23
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
24
24
|
end
|
25
25
|
|
26
26
|
received_text = nil
|
@@ -37,7 +37,7 @@ describe JSparrow::Messaging do
|
|
37
37
|
my_object << 'Obj1 enviado da spec'
|
38
38
|
|
39
39
|
@sender.send_object_message(my_object) do |msg|
|
40
|
-
msg.
|
40
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
41
41
|
end
|
42
42
|
|
43
43
|
received_object = nil
|
@@ -53,7 +53,7 @@ describe JSparrow::Messaging do
|
|
53
53
|
my_id_long = 1234567
|
54
54
|
|
55
55
|
@sender.send_map_message do |msg|
|
56
|
-
msg.
|
56
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
57
57
|
msg.set_long('id', my_id_long)
|
58
58
|
end
|
59
59
|
|
@@ -70,7 +70,7 @@ describe JSparrow::Messaging do
|
|
70
70
|
my_text = 'Mensagem de texto enviada da spec'
|
71
71
|
|
72
72
|
@sender.send_text_message(my_text) do |msg|
|
73
|
-
msg.
|
73
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
74
74
|
end
|
75
75
|
|
76
76
|
received_text = nil
|
@@ -93,17 +93,17 @@ describe JSparrow::Messaging do
|
|
93
93
|
@sender.send_messages do |session, producer|
|
94
94
|
#--- TextMessage
|
95
95
|
text_message = session.create_text_message(my_text)
|
96
|
-
text_message.
|
96
|
+
text_message.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
97
97
|
producer.send(text_message)
|
98
98
|
|
99
99
|
#--- objectMessage
|
100
100
|
object_message = session.create_object_message(my_object)
|
101
|
-
object_message.
|
101
|
+
object_message.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
102
102
|
producer.send(object_message)
|
103
103
|
|
104
104
|
#--- MapMessage
|
105
105
|
map_message = session.create_map_message
|
106
|
-
map_message.
|
106
|
+
map_message.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
107
107
|
map_message.set_long('id', my_id_long)
|
108
108
|
producer.send(map_message)
|
109
109
|
|
data/spec/spec_helper.rb
CHANGED
@@ -4,18 +4,18 @@ require 'spec'
|
|
4
4
|
require File.dirname(File.expand_path(__FILE__)) + '/../lib/jsparrow.rb'
|
5
5
|
|
6
6
|
#
|
7
|
-
#
|
7
|
+
# Helper methods for specs.
|
8
8
|
#
|
9
9
|
module JSparrowHelperMethods
|
10
10
|
|
11
11
|
def configure_connection
|
12
12
|
JSparrow::Connection.configure do
|
13
13
|
use_jms_client_jar '/opt/openjms/lib/openjms-0.7.7-beta-1.jar'
|
14
|
-
|
14
|
+
|
15
15
|
use_jndi_properties :initial_context_factory => 'org.exolab.jms.jndi.InitialContextFactory',
|
16
|
-
:provider_url
|
17
|
-
# :security_principal
|
18
|
-
# :security_credentials
|
16
|
+
:provider_url => 'tcp://localhost:3035'
|
17
|
+
# :security_principal => 'user',
|
18
|
+
# :security_credentials => 'password'
|
19
19
|
|
20
20
|
enable_connection_factories :queue_connection_factory => 'ConnectionFactory',
|
21
21
|
:topic_connection_factory => 'ConnectionFactory'
|
@@ -26,27 +26,43 @@ module JSparrowHelperMethods
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
29
|
+
def new_jms_client
|
30
30
|
configure_connection
|
31
31
|
|
32
|
-
|
32
|
+
new_jsparrow_client
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
35
|
+
def new_named_jms_listener
|
36
36
|
configure_connection
|
37
37
|
|
38
|
-
|
38
|
+
new_jsparrow_listener :as => JSparrowHelperClasses::TestQueueListener
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_anonymous_jms_listener
|
42
|
+
listener = new_jsparrow_listener(
|
43
|
+
:listen_to => { :queue => :test_queue },
|
44
|
+
:receive_only_in_criteria => { :selector => "recipient = 'jsparrow-spec' and to_listener = 'anonymous'" }
|
45
|
+
) do |received_message|
|
46
|
+
|
47
|
+
received_messages << received_message
|
48
|
+
end
|
49
|
+
|
50
|
+
def listener.received_messages
|
51
|
+
@received_messages ||= []
|
52
|
+
end
|
53
|
+
|
54
|
+
listener
|
39
55
|
end
|
40
56
|
|
41
57
|
def send_message_to_listener(listener_name)
|
42
|
-
@jms_client =
|
58
|
+
@jms_client = new_jms_client
|
43
59
|
@jms_client.start
|
44
60
|
|
45
|
-
my_text =
|
61
|
+
my_text = "Mensagem de texto enviada da spec para o listener #{listener_name}"
|
46
62
|
|
47
63
|
@jms_client.queue_sender(:test_queue).send_text_message(my_text) do |msg|
|
48
|
-
msg.
|
49
|
-
msg.
|
64
|
+
msg.add_criteria_to_reception('recipient', 'jsparrow-spec')
|
65
|
+
msg.add_criteria_to_reception('to_listener', listener_name)
|
50
66
|
end
|
51
67
|
|
52
68
|
@jms_client.stop
|
@@ -54,11 +70,10 @@ module JSparrowHelperMethods
|
|
54
70
|
end
|
55
71
|
|
56
72
|
module JSparrowHelperClasses
|
57
|
-
|
58
73
|
#
|
59
|
-
# Listener
|
74
|
+
# Listener for test against "TestQueue" queue
|
60
75
|
#
|
61
|
-
class TestQueueListener < JSparrow::
|
76
|
+
class TestQueueListener < JSparrow::Interactors::Listener
|
62
77
|
listen_to :queue => :test_queue
|
63
78
|
|
64
79
|
receive_only_in_criteria :selector => "recipient = 'jsparrow-spec' and to_listener = 'TestQueueListener'"
|
@@ -72,12 +87,13 @@ module JSparrowHelperClasses
|
|
72
87
|
end
|
73
88
|
|
74
89
|
def on_receive_message(received_message)
|
75
|
-
@received_messages <<
|
90
|
+
@received_messages << received_message
|
76
91
|
end
|
77
92
|
end
|
78
93
|
end
|
94
|
+
|
79
95
|
#
|
80
|
-
#
|
96
|
+
# Some steroids for Spec::Example::ExampleGroup.
|
81
97
|
#
|
82
98
|
class Spec::Example::ExampleGroup
|
83
99
|
include JSparrowHelperMethods
|