qwirk 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -14
- data/lib/qwirk.rb +26 -17
- data/lib/qwirk/adapter.rb +3 -45
- data/lib/qwirk/adapter/base.rb +2 -0
- data/lib/qwirk/adapter/base/expanding_worker_config.rb +133 -0
- data/lib/qwirk/adapter/base/worker_config.rb +104 -0
- data/lib/qwirk/adapter/in_memory.rb +13 -0
- data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/factory.rb +2 -2
- data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/publisher.rb +4 -4
- data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/queue.rb +3 -2
- data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/reply_queue.rb +3 -2
- data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/topic.rb +2 -2
- data/lib/qwirk/{queue_adapter/in_mem → adapter/in_memory}/worker.rb +6 -8
- data/lib/qwirk/adapter/in_memory/worker_config.rb +50 -0
- data/lib/qwirk/adapter/inline.rb +9 -0
- data/lib/qwirk/adapter/inline/publisher.rb +86 -0
- data/lib/qwirk/adapter/inline/worker.rb +55 -0
- data/lib/qwirk/adapter/inline/worker_config.rb +30 -0
- data/lib/qwirk/adapter_factory.rb +48 -0
- data/lib/qwirk/base_worker.rb +18 -28
- data/lib/qwirk/batch/file_worker.rb +4 -4
- data/lib/qwirk/manager.rb +11 -8
- data/lib/qwirk/marshal_strategy/none.rb +1 -1
- data/lib/qwirk/publish_handle.rb +22 -11
- data/lib/qwirk/publisher.rb +9 -9
- data/lib/qwirk/{request_worker.rb → reply_worker.rb} +3 -3
- data/lib/qwirk/worker.rb +27 -29
- data/test/jms_fail_test.rb +11 -11
- data/test/jms_requestor_block_test.rb +12 -12
- data/test/jms_requestor_test.rb +8 -8
- data/test/jms_test.rb +10 -10
- metadata +104 -185
- data/examples/README +0 -1
- data/examples/activemq.xml +0 -84
- data/examples/advanced_requestor/README.md +0 -15
- data/examples/advanced_requestor/base_request_worker.rb +0 -18
- data/examples/advanced_requestor/char_count_worker.rb +0 -16
- data/examples/advanced_requestor/config.ru +0 -24
- data/examples/advanced_requestor/exception_raiser_worker.rb +0 -17
- data/examples/advanced_requestor/length_worker.rb +0 -14
- data/examples/advanced_requestor/print_worker.rb +0 -14
- data/examples/advanced_requestor/publisher.rb +0 -49
- data/examples/advanced_requestor/qwirk.yml +0 -16
- data/examples/advanced_requestor/reverse_worker.rb +0 -14
- data/examples/advanced_requestor/triple_worker.rb +0 -14
- data/examples/batch/my_batch_worker.rb +0 -30
- data/examples/batch/my_line_worker.rb +0 -8
- data/examples/qwirk.yml +0 -20
- data/examples/requestor/README.md +0 -13
- data/examples/requestor/config.ru +0 -13
- data/examples/requestor/qwirk_persist.yml +0 -5
- data/examples/requestor/requestor.rb +0 -68
- data/examples/requestor/reverse_echo_worker.rb +0 -15
- data/examples/setup.rb +0 -13
- data/examples/shared/README.md +0 -24
- data/examples/shared/config.ru +0 -13
- data/examples/shared/publisher.rb +0 -49
- data/examples/shared/qwirk_persist.yml +0 -5
- data/examples/shared/shared_worker.rb +0 -16
- data/examples/simple/README +0 -53
- data/examples/simple/bar_worker.rb +0 -10
- data/examples/simple/baz_worker.rb +0 -10
- data/examples/simple/config.ru +0 -14
- data/examples/simple/publisher.rb +0 -49
- data/examples/simple/qwirk_persist.yml +0 -4
- data/examples/simple/tmp/kahadb/db-1.log +0 -0
- data/examples/simple/tmp/kahadb/db.data +0 -0
- data/examples/simple/tmp/kahadb/db.redo +0 -0
- data/examples/task/README +0 -47
- data/examples/task/config.ru +0 -14
- data/examples/task/foo_worker.rb +0 -10
- data/examples/task/messages.out +0 -1000
- data/examples/task/publisher.rb +0 -25
- data/examples/task/qwirk_persist.yml +0 -5
- data/examples/task/task.rb +0 -36
- data/lib/qwirk/queue_adapter.rb +0 -3
- data/lib/qwirk/queue_adapter/active_mq.rb +0 -13
- data/lib/qwirk/queue_adapter/active_mq/publisher.rb +0 -12
- data/lib/qwirk/queue_adapter/active_mq/worker_config.rb +0 -16
- data/lib/qwirk/queue_adapter/in_mem.rb +0 -7
- data/lib/qwirk/queue_adapter/in_mem/worker_config.rb +0 -59
- data/lib/qwirk/queue_adapter/jms.rb +0 -50
- data/lib/qwirk/queue_adapter/jms/connection.rb +0 -42
- data/lib/qwirk/queue_adapter/jms/consumer.rb +0 -37
- data/lib/qwirk/queue_adapter/jms/publisher.rb +0 -126
- data/lib/qwirk/queue_adapter/jms/worker.rb +0 -89
- data/lib/qwirk/queue_adapter/jms/worker_config.rb +0 -38
- data/lib/qwirk/version.rb +0 -3
- data/lib/qwirk/worker_config.rb +0 -187
data/examples/README
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Modify the qwirk.yml file to setup the proper credentials for you ActiveMQ server.
|
data/examples/activemq.xml
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
<!--
|
2
|
-
ActiveMQ Configuration where the Broker is in-vm
|
3
|
-
|
4
|
-
Settings objectives:
|
5
|
-
- Use available resources
|
6
|
-
- No indefinite limiting of producers to prevent "hanging" of producers
|
7
|
-
when consumers are slow
|
8
|
-
- Use store based message flow so that slow consumers do not slow down producers
|
9
|
-
-->
|
10
|
-
<beans
|
11
|
-
xmlns="http://www.springframework.org/schema/beans"
|
12
|
-
xmlns:amq="http://activemq.apache.org/schema/core"
|
13
|
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
14
|
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
15
|
-
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
|
16
|
-
|
17
|
-
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="qwirk" dataDirectory="tmp" destroyApplicationContextOnStop="true">
|
18
|
-
|
19
|
-
<!-- Set producerFlowControl limits -->
|
20
|
-
<destinationPolicy>
|
21
|
-
<policyMap>
|
22
|
-
<policyEntries>
|
23
|
-
<policyEntry topic=">" producerFlowControl="true" memoryLimit="1024mb" />
|
24
|
-
<policyEntry queue=">" producerFlowControl="true" memoryLimit="1024mb" />
|
25
|
-
</policyEntries>
|
26
|
-
</policyMap>
|
27
|
-
</destinationPolicy>
|
28
|
-
|
29
|
-
<!-- Use the JMX context already created by the JVM -->
|
30
|
-
<managementContext>
|
31
|
-
<managementContext createConnector="false"/>
|
32
|
-
</managementContext>
|
33
|
-
|
34
|
-
<!--
|
35
|
-
Configure message persistence for the broker. The default persistence
|
36
|
-
mechanism is the KahaDB store (identified by the kahaDB tag).
|
37
|
-
For more information, see:
|
38
|
-
|
39
|
-
http://activemq.apache.org/persistence.html
|
40
|
-
-->
|
41
|
-
<persistenceAdapter>
|
42
|
-
<kahaDB directory="tmp/kahadb"/>
|
43
|
-
</persistenceAdapter>
|
44
|
-
|
45
|
-
<plugins>
|
46
|
-
<!-- Configure authentication of remote connections -->
|
47
|
-
<simpleAuthenticationPlugin>
|
48
|
-
<users>
|
49
|
-
<authenticationUser username="qwirkadmin" password="qwirkadmin" groups="users,admins"/>
|
50
|
-
<authenticationUser username="qwirk" password="qwirk" groups="users"/>
|
51
|
-
</users>
|
52
|
-
</simpleAuthenticationPlugin>
|
53
|
-
</plugins>
|
54
|
-
|
55
|
-
<!-- Limits for the broker before it slows down or fails producers.
|
56
|
-
Cause producers to fail with javax.jms.ResourceAllocationException
|
57
|
-
if no space is available after 3 seconds -->
|
58
|
-
<systemUsage>
|
59
|
-
<systemUsage sendFailIfNoSpaceAfterTimeout="3000">
|
60
|
-
<!-- Memory limit for NON_PERSISTENT messages
|
61
|
-
Apparently no longer used since ActiveMQ 5 now uses store
|
62
|
-
based cursors for NON_PERSISTENT messages -->
|
63
|
-
<memoryUsage>
|
64
|
-
<memoryUsage limit="2 gb"/>
|
65
|
-
</memoryUsage>
|
66
|
-
<!-- Disk storage limit for PERSISTENT messages -->
|
67
|
-
<storeUsage>
|
68
|
-
<storeUsage limit="50 gb"/>
|
69
|
-
</storeUsage>
|
70
|
-
<!-- Disk storage limit for temporary (NON_PERSISTENT) messages -->
|
71
|
-
<tempUsage>
|
72
|
-
<tempUsage limit="50 gb"/>
|
73
|
-
</tempUsage>
|
74
|
-
</systemUsage>
|
75
|
-
</systemUsage>
|
76
|
-
|
77
|
-
<!-- Allow remote connections using openwire protocols -->
|
78
|
-
<transportConnectors>
|
79
|
-
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
|
80
|
-
</transportConnectors>
|
81
|
-
|
82
|
-
</broker>
|
83
|
-
|
84
|
-
</beans>
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# Step 0
|
2
|
-
# Follow the directions for configuring jms.yml located in examples/README
|
3
|
-
|
4
|
-
# Step 1
|
5
|
-
# Start an ActiveMQ Server
|
6
|
-
|
7
|
-
# Step 2
|
8
|
-
# Start up the manager
|
9
|
-
rackup -p 4567
|
10
|
-
|
11
|
-
# Step 3
|
12
|
-
jruby publish.rb foobar 4 2
|
13
|
-
|
14
|
-
# Step 4
|
15
|
-
# Play around with the options in qwirk_persist.yml and repeat steps 2&3.
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module BaseRequestWorker
|
2
|
-
include Qwirk::RequestWorker
|
3
|
-
|
4
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
Qwirk::Worker.included(base)
|
8
|
-
end
|
9
|
-
|
10
|
-
def perform(obj)
|
11
|
-
puts "#{self}: Received #{obj} at #{Time.now}"
|
12
|
-
if config.sleep_time > 0.0
|
13
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
14
|
-
sleep config.sleep_time
|
15
|
-
end
|
16
|
-
super
|
17
|
-
end
|
18
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
class CharCountWorker
|
2
|
-
include Qwirk::RequestWorker
|
3
|
-
|
4
|
-
topic 'test_string', :response => {:marshal => :bson, :time_to_live => 5000}
|
5
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
6
|
-
|
7
|
-
def request(obj)
|
8
|
-
if config.sleep_time > 0.0
|
9
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
10
|
-
sleep config.sleep_time
|
11
|
-
end
|
12
|
-
hash = Hash.new(0)
|
13
|
-
obj.each_char {|c| hash[c] += 1}
|
14
|
-
hash
|
15
|
-
end
|
16
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require '../setup'
|
2
|
-
require './publisher'
|
3
|
-
require './char_count_worker'
|
4
|
-
require './exception_raiser_worker'
|
5
|
-
require './length_worker'
|
6
|
-
require './print_worker'
|
7
|
-
require './reverse_worker'
|
8
|
-
require './triple_worker'
|
9
|
-
|
10
|
-
# If we're not starting up a standalone publisher, then start up a manager
|
11
|
-
if ENV['RACK_ENV'] != 'publisher'
|
12
|
-
manager = Qwirk[$adapter_key].create_manager(:name => 'Worker', :persist_file => 'qwirk_persist.yml')
|
13
|
-
manager['CharCount'].max_count = 1
|
14
|
-
manager['ExceptionRaiser'].max_count = 1
|
15
|
-
manager['Length'].max_count = 1
|
16
|
-
manager['Print'].max_count = 1
|
17
|
-
manager['Reverse'].max_count = 1
|
18
|
-
manager['Triple'].max_count = 1
|
19
|
-
at_exit { manager.stop }
|
20
|
-
end
|
21
|
-
if ENV['RACK_ENV'] != 'worker'
|
22
|
-
Rumx::Bean.root.bean_add_child(:Publisher, Publisher.new($adapter_key))
|
23
|
-
end
|
24
|
-
run Rumx::Server
|
@@ -1,17 +0,0 @@
|
|
1
|
-
class ExceptionRaiserWorker
|
2
|
-
include Qwirk::RequestWorker
|
3
|
-
|
4
|
-
topic 'test_string', :response => {:marshal => :string, :time_to_live => 5000}
|
5
|
-
|
6
|
-
config_accessor :raise_exception, :boolean, 'Raise an exception instead of handling the request', false
|
7
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
8
|
-
|
9
|
-
def request(obj)
|
10
|
-
if config.sleep_time > 0.0
|
11
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
12
|
-
sleep config.sleep_time
|
13
|
-
end
|
14
|
-
raise "Raising dummy exception on #{obj}" if config.raise_exception
|
15
|
-
"We decided not to raise on #{obj}"
|
16
|
-
end
|
17
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class LengthWorker
|
2
|
-
include Qwirk::RequestWorker
|
3
|
-
|
4
|
-
topic 'test_string', :response => {:marshal => :ruby, :time_to_live => 5000}
|
5
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
6
|
-
|
7
|
-
def request(obj)
|
8
|
-
if config.sleep_time > 0.0
|
9
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
10
|
-
sleep config.sleep_time
|
11
|
-
end
|
12
|
-
obj.length
|
13
|
-
end
|
14
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class PrintWorker
|
2
|
-
include Qwirk::Worker
|
3
|
-
|
4
|
-
topic 'test_string'
|
5
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
6
|
-
|
7
|
-
def perform(obj)
|
8
|
-
if config.sleep_time > 0.0
|
9
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
10
|
-
sleep config.sleep_time
|
11
|
-
end
|
12
|
-
puts "#{self}: Received #{obj} at #{Time.now}"
|
13
|
-
end
|
14
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'rumx'
|
2
|
-
require 'qwirk'
|
3
|
-
|
4
|
-
class Publisher
|
5
|
-
include Rumx::Bean
|
6
|
-
|
7
|
-
bean_attr_reader :messages, :hash, 'Message', :hash_type => :string, :allow_write => false
|
8
|
-
|
9
|
-
bean_operation :publish, :void, 'Publish and receive messages', [
|
10
|
-
[ :message, :string, 'Message to get sent', 'Hello' ],
|
11
|
-
[ :timeout, :float, 'Timeout on receiving response', 4.0 ],
|
12
|
-
[ :sleep_time, :float, "Time between publishing and receiving where we're supposedly working", 2.0 ]
|
13
|
-
]
|
14
|
-
|
15
|
-
def initialize(adapter_key)
|
16
|
-
@publisher = Qwirk[adapter_key].create_publisher(:topic_name => 'test_string', :response => true, :marshal => :string)
|
17
|
-
end
|
18
|
-
|
19
|
-
def publish(message, timeout, sleep_time)
|
20
|
-
@messages = {}
|
21
|
-
puts "Publishing at #{Time.now.to_f}"
|
22
|
-
handle = @publisher.publish(message)
|
23
|
-
sleep sleep_time
|
24
|
-
|
25
|
-
handle.read_response(timeout) do |response|
|
26
|
-
response.on_message 'CharCount' do |hash|
|
27
|
-
messages['CharCount'] = "returned #{hash.inspect} in #{response.msec_delta.to_i} ms"
|
28
|
-
end
|
29
|
-
response.on_message 'Length', 'Reverse', 'Triple' do |val|
|
30
|
-
messages[response.name] = "returned #{val} in #{response.msec_delta.to_i} ms"
|
31
|
-
end
|
32
|
-
response.on_message 'ExceptionRaiser' do |val|
|
33
|
-
messages[response.name] = "didn't raise an exception, returned \"#{val}\" in #{response.msec_delta.to_i} ms"
|
34
|
-
end
|
35
|
-
response.on_timeout 'Reverse' do
|
36
|
-
messages[response.name] = "Timed out with it's own timeout handler in #{response.msec_delta.to_i} ms"
|
37
|
-
end
|
38
|
-
response.on_timeout do
|
39
|
-
messages[response.name] = "timed out in #{response.msec_delta.to_i} ms"
|
40
|
-
end
|
41
|
-
response.on_remote_exception 'ExceptionRaiser' do |e|
|
42
|
-
messages[response.name] = "It figures that ExceptionRaiser would raise an exception: #{e.message} in #{response.msec_delta.to_i} ms"
|
43
|
-
end
|
44
|
-
response.on_remote_exception do |e|
|
45
|
-
messages[response.name] = "raised exception: #{e.message} in #{response.msec_delta.to_i} ms"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class ReverseWorker
|
2
|
-
include Qwirk::RequestWorker
|
3
|
-
|
4
|
-
topic 'test_string', :response => {:marshal => :string, :time_to_live => 5000}
|
5
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
6
|
-
|
7
|
-
def request(obj)
|
8
|
-
if config.sleep_time > 0.0
|
9
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
10
|
-
sleep config.sleep_time
|
11
|
-
end
|
12
|
-
obj.reverse
|
13
|
-
end
|
14
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class TripleWorker
|
2
|
-
include Qwirk::RequestWorker
|
3
|
-
|
4
|
-
topic 'test_string', :response => {:marshal => :string, :time_to_live => 5000}
|
5
|
-
config_accessor :sleep_time, :float, 'Number of seconds to sleep between messages', 0
|
6
|
-
|
7
|
-
def request(obj)
|
8
|
-
if config.sleep_time > 0.0
|
9
|
-
puts "#{self}: Sleeping for #{config.sleep_time} at #{Time.now}"
|
10
|
-
sleep config.sleep_time
|
11
|
-
end
|
12
|
-
obj * 3
|
13
|
-
end
|
14
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
class MyBatchWorker
|
2
|
-
include Qwirk::Batch::FileWorker
|
3
|
-
|
4
|
-
file :glob => '/home/batch_files/**', :max_outstanding_requests => 100, :fail_threshold => 0.8, :save_period => 30.seconds
|
5
|
-
marshal :bson
|
6
|
-
|
7
|
-
def start_file(file, is_restart)
|
8
|
-
@outfile = File.open(output_file_for(file), 'a')
|
9
|
-
end
|
10
|
-
|
11
|
-
def record_to_object(line)
|
12
|
-
line_to_hash(line)
|
13
|
-
end
|
14
|
-
|
15
|
-
def process_response(hash)
|
16
|
-
@outfile.puts(hash_to_line(hash))
|
17
|
-
end
|
18
|
-
|
19
|
-
def failed
|
20
|
-
@outfile.close
|
21
|
-
end
|
22
|
-
|
23
|
-
def stop
|
24
|
-
@outfile.close
|
25
|
-
end
|
26
|
-
|
27
|
-
def finish_file
|
28
|
-
@outfile.close
|
29
|
-
end
|
30
|
-
end
|
data/examples/qwirk.yml
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
in_mem:
|
2
|
-
:adapter: InMem
|
3
|
-
:log_times: true
|
4
|
-
|
5
|
-
active_mq:
|
6
|
-
:adapter: ActiveMQ
|
7
|
-
:log_times: true
|
8
|
-
:factory: org.apache.activemq.ActiveMQConnectionFactory
|
9
|
-
:broker_url: tcp://localhost:61616
|
10
|
-
#:username: myuser
|
11
|
-
#:password: mypassword
|
12
|
-
|
13
|
-
active_mq_invm:
|
14
|
-
:adapter: ActiveMQ
|
15
|
-
:log_times: true
|
16
|
-
:factory: org.apache.activemq.ActiveMQConnectionFactory
|
17
|
-
:broker_url: vm://qwirk?brokerConfig=xbean:../activemq.xml
|
18
|
-
:object_message_serialization_defered: true
|
19
|
-
:username: qwirk
|
20
|
-
:password: qwirk
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# Step 0
|
2
|
-
# Follow the directions for configuring jms.yml located in examples/README
|
3
|
-
|
4
|
-
# Step 1
|
5
|
-
# Start an ActiveMQ Server
|
6
|
-
|
7
|
-
# Step 2
|
8
|
-
# Start up the manager
|
9
|
-
rackup -p 4567
|
10
|
-
|
11
|
-
# Step 3
|
12
|
-
# Request 'my string' get reversed
|
13
|
-
jruby request.rb 'my string'
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require '../setup'
|
2
|
-
require './reverse_echo_worker'
|
3
|
-
require './requestor'
|
4
|
-
|
5
|
-
# If we're not starting up a standalone publisher, then start up a manager
|
6
|
-
if ENV['RACK_ENV'] != 'publisher'
|
7
|
-
manager = Qwirk[$adapter_key].create_manager(:name => 'Worker', :persist_file => 'qwirk_persist.yml')
|
8
|
-
at_exit { manager.stop }
|
9
|
-
end
|
10
|
-
if ENV['RACK_ENV'] != 'worker'
|
11
|
-
Rumx::Bean.root.bean_add_child(:Requestor, Requestor.new($adapter_key))
|
12
|
-
end
|
13
|
-
run Rumx::Server
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'rumx'
|
2
|
-
require 'qwirk'
|
3
|
-
|
4
|
-
class MessageInfo
|
5
|
-
include Rumx::Bean
|
6
|
-
|
7
|
-
bean_attr_reader :request, :string, 'The message that was sent'
|
8
|
-
bean_attr_reader :response, :string, 'The response that was received'
|
9
|
-
|
10
|
-
def initialize(request, response)
|
11
|
-
@request, @response = request, response
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class Requestor
|
16
|
-
include Rumx::Bean
|
17
|
-
|
18
|
-
bean_reader :messages, :list, 'Messages', :list_type => :bean
|
19
|
-
|
20
|
-
bean_operation :publish, :void, 'Publish and receive messages', [
|
21
|
-
[ :message, :string, 'Message to get sent', 'Hello' ],
|
22
|
-
[ :timeout, :float, 'Timeout on receiving response', 4.0 ],
|
23
|
-
[ :sleep_time, :float, "Time between publishing and receiving where we're supposedly working", 2.0 ],
|
24
|
-
[ :thread_count, :integer, 'Number of different threads that are publishing and receiving the message', 10 ]
|
25
|
-
]
|
26
|
-
|
27
|
-
|
28
|
-
def initialize(adapter_key)
|
29
|
-
@outstanding_hash_mutex = Mutex.new
|
30
|
-
@messages = []
|
31
|
-
@publisher = Qwirk[adapter_key].create_publisher(:queue_name => 'ReverseEcho', :response => {:time_to_live => 10000}, :marshal => :string)
|
32
|
-
end
|
33
|
-
|
34
|
-
def publish(message, timeout, sleep_time, thread_count)
|
35
|
-
@outstanding_hash_mutex.synchronize do
|
36
|
-
@messages = []
|
37
|
-
end
|
38
|
-
threads = []
|
39
|
-
(0...thread_count).each do |i|
|
40
|
-
threads << Thread.new(i) do |i|
|
41
|
-
puts "#{i}: Publishing at #{Time.now.to_f}"
|
42
|
-
request = "##{i}: #{message}"
|
43
|
-
response = nil
|
44
|
-
begin
|
45
|
-
handle = @publisher.publish(request)
|
46
|
-
# Here's where we'd go off and do other work
|
47
|
-
sleep sleep_time
|
48
|
-
puts "#{i}: Finished sleeping at #{Time.now.to_f}"
|
49
|
-
response = handle.read_response(timeout)
|
50
|
-
puts "#{i}: Received at #{Time.now.to_f}: #{response}"
|
51
|
-
rescue Exception => e
|
52
|
-
puts "#{i}: Exception: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
|
53
|
-
response = e.message
|
54
|
-
end
|
55
|
-
@outstanding_hash_mutex.synchronize do
|
56
|
-
@messages << MessageInfo.new(request, response)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
threads.each {|t| t.join}
|
61
|
-
end
|
62
|
-
|
63
|
-
def messages
|
64
|
-
@outstanding_hash_mutex.synchronize do
|
65
|
-
return @messages.dup
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|