qwirk 0.0.1
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/History.md +7 -0
- data/LICENSE.txt +201 -0
- data/README.md +180 -0
- data/Rakefile +34 -0
- data/examples/README +1 -0
- data/examples/activemq.xml +84 -0
- data/examples/advanced_requestor/README.md +15 -0
- data/examples/advanced_requestor/base_request_worker.rb +18 -0
- data/examples/advanced_requestor/char_count_worker.rb +16 -0
- data/examples/advanced_requestor/config.ru +24 -0
- data/examples/advanced_requestor/exception_raiser_worker.rb +17 -0
- data/examples/advanced_requestor/length_worker.rb +14 -0
- data/examples/advanced_requestor/print_worker.rb +14 -0
- data/examples/advanced_requestor/publisher.rb +49 -0
- data/examples/advanced_requestor/qwirk.yml +16 -0
- data/examples/advanced_requestor/reverse_worker.rb +14 -0
- data/examples/advanced_requestor/triple_worker.rb +14 -0
- data/examples/batch/my_batch_worker.rb +30 -0
- data/examples/batch/my_line_worker.rb +8 -0
- data/examples/qwirk.yml +20 -0
- data/examples/requestor/README.md +13 -0
- data/examples/requestor/config.ru +13 -0
- data/examples/requestor/qwirk_persist.yml +5 -0
- data/examples/requestor/requestor.rb +68 -0
- data/examples/requestor/reverse_echo_worker.rb +15 -0
- data/examples/setup.rb +13 -0
- data/examples/shared/README.md +24 -0
- data/examples/shared/config.ru +13 -0
- data/examples/shared/publisher.rb +49 -0
- data/examples/shared/qwirk_persist.yml +5 -0
- data/examples/shared/shared_worker.rb +16 -0
- data/examples/simple/README +53 -0
- data/examples/simple/bar_worker.rb +10 -0
- data/examples/simple/baz_worker.rb +10 -0
- data/examples/simple/config.ru +14 -0
- data/examples/simple/publisher.rb +49 -0
- data/examples/simple/qwirk_persist.yml +4 -0
- 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 +47 -0
- data/examples/task/config.ru +14 -0
- data/examples/task/foo_worker.rb +10 -0
- data/examples/task/messages.out +1000 -0
- data/examples/task/publisher.rb +25 -0
- data/examples/task/qwirk_persist.yml +5 -0
- data/examples/task/task.rb +36 -0
- data/lib/qwirk.rb +63 -0
- data/lib/qwirk/adapter.rb +45 -0
- data/lib/qwirk/base_worker.rb +96 -0
- data/lib/qwirk/batch.rb +4 -0
- data/lib/qwirk/batch/acquire_file_strategy.rb +47 -0
- data/lib/qwirk/batch/active_record.rb +3 -0
- data/lib/qwirk/batch/active_record/batch_job.rb +111 -0
- data/lib/qwirk/batch/active_record/failed_record.rb +5 -0
- data/lib/qwirk/batch/active_record/outstanding_record.rb +6 -0
- data/lib/qwirk/batch/file_status_strategy.rb +86 -0
- data/lib/qwirk/batch/file_worker.rb +228 -0
- data/lib/qwirk/batch/job_status.rb +29 -0
- data/lib/qwirk/batch/parse_file_strategy.rb +48 -0
- data/lib/qwirk/engine.rb +9 -0
- data/lib/qwirk/loggable.rb +23 -0
- data/lib/qwirk/manager.rb +140 -0
- data/lib/qwirk/marshal_strategy.rb +74 -0
- data/lib/qwirk/marshal_strategy/bson.rb +37 -0
- data/lib/qwirk/marshal_strategy/json.rb +37 -0
- data/lib/qwirk/marshal_strategy/none.rb +26 -0
- data/lib/qwirk/marshal_strategy/ruby.rb +26 -0
- data/lib/qwirk/marshal_strategy/string.rb +25 -0
- data/lib/qwirk/marshal_strategy/yaml.rb +25 -0
- data/lib/qwirk/publish_handle.rb +170 -0
- data/lib/qwirk/publisher.rb +67 -0
- data/lib/qwirk/queue_adapter.rb +3 -0
- data/lib/qwirk/queue_adapter/active_mq.rb +13 -0
- data/lib/qwirk/queue_adapter/active_mq/publisher.rb +12 -0
- data/lib/qwirk/queue_adapter/active_mq/worker_config.rb +16 -0
- data/lib/qwirk/queue_adapter/in_mem.rb +7 -0
- data/lib/qwirk/queue_adapter/in_mem/factory.rb +45 -0
- data/lib/qwirk/queue_adapter/in_mem/publisher.rb +98 -0
- data/lib/qwirk/queue_adapter/in_mem/queue.rb +88 -0
- data/lib/qwirk/queue_adapter/in_mem/reply_queue.rb +56 -0
- data/lib/qwirk/queue_adapter/in_mem/topic.rb +48 -0
- data/lib/qwirk/queue_adapter/in_mem/worker.rb +63 -0
- data/lib/qwirk/queue_adapter/in_mem/worker_config.rb +59 -0
- data/lib/qwirk/queue_adapter/jms.rb +50 -0
- data/lib/qwirk/queue_adapter/jms/connection.rb +42 -0
- data/lib/qwirk/queue_adapter/jms/consumer.rb +37 -0
- data/lib/qwirk/queue_adapter/jms/publisher.rb +126 -0
- data/lib/qwirk/queue_adapter/jms/worker.rb +89 -0
- data/lib/qwirk/queue_adapter/jms/worker_config.rb +38 -0
- data/lib/qwirk/remote_exception.rb +42 -0
- data/lib/qwirk/request_worker.rb +62 -0
- data/lib/qwirk/task.rb +177 -0
- data/lib/qwirk/task.rb.sav +194 -0
- data/lib/qwirk/version.rb +3 -0
- data/lib/qwirk/worker.rb +222 -0
- data/lib/qwirk/worker_config.rb +187 -0
- data/lib/rails/generators/qwirk/qwirk_generator.rb +82 -0
- data/lib/rails/generators/qwirk/templates/initializer.rb +6 -0
- data/lib/rails/generators/qwirk/templates/migration.rb +9 -0
- data/lib/rails/generators/qwirk/templates/schema.rb +28 -0
- data/lib/rails/railties/tasks.rake +8 -0
- data/lib/tasks/qwirk_tasks.rake +4 -0
- data/test/base_test.rb +248 -0
- data/test/database.yml +14 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +45 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +22 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +26 -0
- data/test/dummy/config/environments/production.rb +49 -0
- data/test/dummy/config/environments/test.rb +35 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/log/development.log +0 -0
- data/test/dummy/log/production.log +0 -0
- data/test/dummy/log/server.log +0 -0
- data/test/dummy/log/test.log +0 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/javascripts/application.js +2 -0
- data/test/dummy/public/javascripts/controls.js +965 -0
- data/test/dummy/public/javascripts/dragdrop.js +974 -0
- data/test/dummy/public/javascripts/effects.js +1123 -0
- data/test/dummy/public/javascripts/prototype.js +6001 -0
- data/test/dummy/public/javascripts/rails.js +191 -0
- data/test/dummy/script/rails +6 -0
- data/test/integration/navigation_test.rb +7 -0
- data/test/jms.yml +9 -0
- data/test/jms_fail_test.rb +149 -0
- data/test/jms_requestor_block_test.rb +278 -0
- data/test/jms_requestor_test.rb +238 -0
- data/test/jms_test.rb +287 -0
- data/test/marshal_strategy_test.rb +62 -0
- data/test/support/integration_case.rb +5 -0
- data/test/test_helper.rb +7 -0
- data/test/test_helper.rbold +22 -0
- data/test/test_helper_active_record.rb +61 -0
- data/test/unit/qwirk/batch/acquire_file_strategy_test.rb +101 -0
- data/test/unit/qwirk/batch/active_record/batch_job_test.rb +35 -0
- data/test/unit/qwirk/batch/parse_file_strategy_test.rb +49 -0
- metadata +366 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Handle Messaging and Queuing using JMS
|
|
2
|
+
module Qwirk
|
|
3
|
+
module QueueAdapter
|
|
4
|
+
module InMem
|
|
5
|
+
|
|
6
|
+
# This is the InMem Adapter that corresponds to Qwirk::WorkerConfig (also known as parent)
|
|
7
|
+
class WorkerConfig
|
|
8
|
+
include Rumx::Bean
|
|
9
|
+
|
|
10
|
+
bean_reader :queue_name, :string, 'Name of the queue'
|
|
11
|
+
bean_reader :queue_size, :integer, 'Current count of messages in the queue'
|
|
12
|
+
bean_accessor :queue_max_size, :integer, 'Max messages allowed in the queue'
|
|
13
|
+
|
|
14
|
+
attr_reader :stopped, :parent
|
|
15
|
+
|
|
16
|
+
def initialize(queue_adapter, parent, queue_name, topic_name, options, response_options)
|
|
17
|
+
@parent = parent
|
|
18
|
+
@queue_name = queue_name
|
|
19
|
+
@topic_name = topic_name
|
|
20
|
+
queue_max_size = options[:queue_max_size] || 100
|
|
21
|
+
@queue = Factory.get_worker_queue(parent.name, queue_name, topic_name, queue_max_size)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def default_marshal_sym
|
|
25
|
+
:none
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create_worker
|
|
29
|
+
Worker.new(@parent.name, @parent.marshaler, @queue, @queue_name, @topic_name)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def stop
|
|
33
|
+
return if @stopped
|
|
34
|
+
Qwirk.logger.debug { "Closing #{self}" }
|
|
35
|
+
@queue.stop
|
|
36
|
+
@stopped = true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
## End of required override methods for worker_config adapter
|
|
40
|
+
|
|
41
|
+
def queue_name
|
|
42
|
+
@queue.name
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def queue_size
|
|
46
|
+
@queue.size
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def queue_max_size
|
|
50
|
+
@queue.max_size
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def queue_max_size=(max_size)
|
|
54
|
+
@queue.max_size = max_size
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
require 'qwirk/queue_adapter/jms/connection'
|
|
4
|
+
require 'qwirk/queue_adapter/jms/publisher'
|
|
5
|
+
require 'qwirk/queue_adapter/jms/worker_config'
|
|
6
|
+
require 'qwirk/queue_adapter/jms/worker'
|
|
7
|
+
|
|
8
|
+
module Qwirk
|
|
9
|
+
module QueueAdapter
|
|
10
|
+
module JMS
|
|
11
|
+
def self.init(config)
|
|
12
|
+
Connection.new(config)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.same_destination?(options1, options2)
|
|
16
|
+
if options1[:queue_name]
|
|
17
|
+
return options1[:queue_name] == options2[:queue_name]
|
|
18
|
+
elsif options1[:topic_name]
|
|
19
|
+
return options1[:topic_name] == options2[:topic_name]
|
|
20
|
+
elsif options1[:virtual_topic_name]
|
|
21
|
+
return options1[:virtual_topic_name] == options2[:virtual_topic_name]
|
|
22
|
+
elsif options1[:destination]
|
|
23
|
+
return options1[:destination] == options2[:destination]
|
|
24
|
+
else
|
|
25
|
+
return false
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.create_message(session, marshaled_object, marshal_type)
|
|
30
|
+
case marshal_type
|
|
31
|
+
when :text
|
|
32
|
+
session.create_text_message(marshaled_object)
|
|
33
|
+
when :bytes
|
|
34
|
+
msg = session.create_bytes_message()
|
|
35
|
+
msg.data = marshaled_object
|
|
36
|
+
msg
|
|
37
|
+
else raise "Invalid marshal type: #{marshal_type}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.parse_response(message)
|
|
42
|
+
if error_yaml = message['qwirk:exception']
|
|
43
|
+
return Qwirk::RemoteException.from_hash(YAML.load(error_yaml))
|
|
44
|
+
end
|
|
45
|
+
marshaler = Qwirk::MarshalStrategy.find(message['qwirk:marshal'] || :ruby)
|
|
46
|
+
return marshaler.unmarshal(message.data)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'jms'
|
|
2
|
+
|
|
3
|
+
# Handle Messaging and Queuing
|
|
4
|
+
module Qwirk
|
|
5
|
+
module QueueAdapter
|
|
6
|
+
module JMS
|
|
7
|
+
class Connection
|
|
8
|
+
# Initialize the messaging system and connection pool for this VM
|
|
9
|
+
def initialize(config)
|
|
10
|
+
@config = config
|
|
11
|
+
@connection = ::JMS::Connection.new(config)
|
|
12
|
+
@session_pool = @connection.create_session_pool(@config)
|
|
13
|
+
@connection.start
|
|
14
|
+
|
|
15
|
+
at_exit do
|
|
16
|
+
close
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Create a session targeted for a consumer (producers should use the session_pool)
|
|
21
|
+
def create_session
|
|
22
|
+
@connection.create_session(@config || {})
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def session_pool
|
|
26
|
+
@session_pool
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def close
|
|
30
|
+
return if @closed
|
|
31
|
+
Qwirk.logger.info "Closing JMS connection"
|
|
32
|
+
@session_pool.close if @session_pool
|
|
33
|
+
if @connection
|
|
34
|
+
@connection.stop
|
|
35
|
+
@connection.close
|
|
36
|
+
end
|
|
37
|
+
@closed = true
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Handle Messaging and Queuing using JMS
|
|
2
|
+
module Qwirk
|
|
3
|
+
module QueueAdapter
|
|
4
|
+
module JMS
|
|
5
|
+
class Consumer
|
|
6
|
+
attr_reader :stopped
|
|
7
|
+
|
|
8
|
+
def initialize(connection, options)
|
|
9
|
+
@options = options
|
|
10
|
+
@session = connection.create_session
|
|
11
|
+
@consumer = @session.consumer(@options)
|
|
12
|
+
@session.start
|
|
13
|
+
@stopped = false
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def receive
|
|
17
|
+
@message = @consumer.receive
|
|
18
|
+
return nil unless @message
|
|
19
|
+
return JMS.parse_response(@message)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def acknowledge_message
|
|
23
|
+
@message.acknowledge
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def stop
|
|
27
|
+
return if @stopped
|
|
28
|
+
Qwirk.logger.info "Stopping consumer for #{@options.inspect}"
|
|
29
|
+
# Don't clobber the session before a reply
|
|
30
|
+
@consumer.close if @consumer
|
|
31
|
+
@session.close if @session
|
|
32
|
+
@stopped = true
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# Handle Messaging and Queuing using JMS
|
|
2
|
+
module Qwirk
|
|
3
|
+
module QueueAdapter
|
|
4
|
+
module JMS
|
|
5
|
+
class Publisher
|
|
6
|
+
|
|
7
|
+
#attr_reader :persistent, :marshaler, :reply_queue
|
|
8
|
+
|
|
9
|
+
def initialize(queue_adapter, queue_name, topic_name, options, response_options)
|
|
10
|
+
@connection = queue_adapter.adapter_info
|
|
11
|
+
response_options ||= {}
|
|
12
|
+
@dest_options = {:queue_name => queue_name} if queue_name
|
|
13
|
+
@dest_options = {:topic_name => topic_name} if topic_name
|
|
14
|
+
@persistent_sym = options[:persistent] ? :persistent : :non_persistent
|
|
15
|
+
@time_to_live = options[:time_to_live]
|
|
16
|
+
@response_time_to_live_str = response_options[:time_to_live] && response_options[:time_to_live].to_s
|
|
17
|
+
@response_persistent_str = nil
|
|
18
|
+
@response_persistent_str = (!!response_options[:persistent]).to_s unless response_options[:persistent].nil?
|
|
19
|
+
|
|
20
|
+
@connection.session_pool.session do |session|
|
|
21
|
+
# TODO: Use sync attribute so these queues aren't constantly created.
|
|
22
|
+
@dest_queue = session.create_destination(@dest_options)
|
|
23
|
+
if response_options
|
|
24
|
+
reply_queue_name = response_options[:queue_name] || :temporary
|
|
25
|
+
@reply_queue = session.create_destination(:queue_name => reply_queue_name)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def default_marshal_sym
|
|
31
|
+
:ruby
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Publish the given object and return the message_id.
|
|
35
|
+
# TODO: Too hackish to include task_id in here, think of a better solution
|
|
36
|
+
def publish(marshaled_object, marshaler, task_id, props)
|
|
37
|
+
message = nil
|
|
38
|
+
@connection.session_pool.producer(:destination => @dest_queue) do |session, producer|
|
|
39
|
+
producer.time_to_live = @time_to_live if @time_to_live
|
|
40
|
+
producer.delivery_mode_sym = @persistent_sym
|
|
41
|
+
message = JMS.create_message(session, marshaled_object, marshaler.marshal_type)
|
|
42
|
+
message.jms_reply_to = @reply_queue if @reply_queue
|
|
43
|
+
message['qwirk:marshal'] = marshaler.to_sym.to_s
|
|
44
|
+
message['qwirk:response:time_to_live'] = @response_time_to_live_str if @response_time_to_live_str
|
|
45
|
+
message['qwirk:response:persistent'] = @response_persistent_str unless @response_persistent_str.nil?
|
|
46
|
+
message['QwirkTaskID'] = task_id if task_id
|
|
47
|
+
props.each do |key, value|
|
|
48
|
+
message.send("#{key}=", value)
|
|
49
|
+
end
|
|
50
|
+
producer.send(message)
|
|
51
|
+
end
|
|
52
|
+
# Return the adapter_info which for JMS is the message_id. This value will be sent to with_response below.
|
|
53
|
+
return message.jms_message_id
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# See Qwirk::PublishHandle#read_response for the requirements for this method.
|
|
57
|
+
def with_response(message_id, &block)
|
|
58
|
+
raise "Invalid call to with_response for #{self}, not setup for responding" unless @reply_queue
|
|
59
|
+
options = { :destination => @reply_queue, :selector => "JMSCorrelationID = '#{message_id}'" }
|
|
60
|
+
@connection.session_pool.consumer(options) do |session, consumer|
|
|
61
|
+
yield MyConsumer.new(consumer)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# See Qwirk::Publisher#create_task_consumer for the requirements for this method.
|
|
66
|
+
def create_producer_consumer_pair(task_id, marshaler)
|
|
67
|
+
producer = MyTaskProducer.new(self, @reply_queue, task_id, marshaler)
|
|
68
|
+
consumer = Consumer.new(@connection, :destination => reply_queue, :selector => "QwirkTaskID = '#{task_id}'")
|
|
69
|
+
return producer, consumer
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def create_fail_producer_consumer_pair(task_id, marshaler)
|
|
73
|
+
fail_queue = nil
|
|
74
|
+
@connection.session_pool.session do |session|
|
|
75
|
+
fail_queue = session.create_destination(:queue_name => :temporary)
|
|
76
|
+
end
|
|
77
|
+
producer = MyTaskProducer.new(self, fail_queue, task_id, marshaler)
|
|
78
|
+
consumer = Consumer.new(@connection, :destination => reply_queue, :selector => "QwirkTaskID = '#{task_id}'")
|
|
79
|
+
return producer, consumer
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def to_s
|
|
83
|
+
"Publisher: #{@dest_options.inspect}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
#######
|
|
87
|
+
private
|
|
88
|
+
#######
|
|
89
|
+
|
|
90
|
+
class MyConsumer
|
|
91
|
+
attr_reader :worker_name
|
|
92
|
+
|
|
93
|
+
def initialize(consumer)
|
|
94
|
+
@consumer = consumer
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def timeout_read(timeout)
|
|
98
|
+
msec = (timeout * 1000).to_i
|
|
99
|
+
if msec > 100
|
|
100
|
+
message = @consumer.receive(msec)
|
|
101
|
+
else
|
|
102
|
+
#message = @consumer.receive_no_wait
|
|
103
|
+
message = @consumer.receive(100)
|
|
104
|
+
end
|
|
105
|
+
return nil unless message
|
|
106
|
+
message.acknowledge
|
|
107
|
+
return [message.jms_correlation_id, JMS.parse_response(message), message['qwirk:worker']]
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
class MyTaskProducer
|
|
112
|
+
def initialize(publisher, reply_queue, task_id, marshaler)
|
|
113
|
+
@publisher = publisher
|
|
114
|
+
@reply_queue = reply_queue
|
|
115
|
+
@task_id = task_id
|
|
116
|
+
@marshaler = marshaler
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def send(marshaled_object)
|
|
120
|
+
@publisher.publish(marshaled_object, @marshaler, @task_id, {})
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Handle Messaging and Queuing using JMS
|
|
2
|
+
module Qwirk
|
|
3
|
+
module QueueAdapter
|
|
4
|
+
module JMS
|
|
5
|
+
class Worker
|
|
6
|
+
def initialize(worker_config)
|
|
7
|
+
@worker_config = worker_config
|
|
8
|
+
@name = worker_config.parent.name
|
|
9
|
+
@marshaler = worker_config.parent.marshaler
|
|
10
|
+
@session = worker_config.connection.create_session
|
|
11
|
+
@consumer = @session.consumer(worker_config.destination)
|
|
12
|
+
@session.start
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def receive_message
|
|
16
|
+
@consumer.receive
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def acknowledge_message(msg)
|
|
20
|
+
msg.acknowledge
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def send_response(original_message, marshaled_object)
|
|
24
|
+
do_send_response(@marshaler, original_message, marshaled_object)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def send_exception(original_message, e)
|
|
28
|
+
@string_marshaler ||= MarshalStrategy.find(:string)
|
|
29
|
+
do_send_response(@string_marshaler, original_message, "Exception: #{e.message}") do |reply_message|
|
|
30
|
+
reply_message['qwirk:exception'] = Qwirk::RemoteException.new(e).to_hash.to_yaml
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def message_to_object(msg)
|
|
35
|
+
marshaler = Qwirk::MarshalStrategy.find(msg['qwirk:marshal'] || :ruby)
|
|
36
|
+
return marshaler.unmarshal(msg.data)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def handle_failure(message, fail_queue_name)
|
|
40
|
+
@session.producer(:queue_name => fail_queue_name) do |producer|
|
|
41
|
+
# TODO: Can't add attribute to read-only message?
|
|
42
|
+
#message['qwirk:exception'] = Qwirk::RemoteException.new(e).to_hash.to_yaml
|
|
43
|
+
producer.send(message)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def stop
|
|
48
|
+
puts "in jms worker stop"
|
|
49
|
+
return if @stopped
|
|
50
|
+
Qwirk.logger.info "Stopping JMS worker #{@name}"
|
|
51
|
+
# Don't clobber the session before a reply
|
|
52
|
+
@consumer.close if @consumer
|
|
53
|
+
@session.close if @session
|
|
54
|
+
@stopped = true
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def do_send_response(marshaler, original_message, marshaled_object)
|
|
60
|
+
return false unless original_message.reply_to
|
|
61
|
+
begin
|
|
62
|
+
@session.producer(:destination => original_message.reply_to) do |producer|
|
|
63
|
+
# For time_to_live and jms_deliver_mode, first use the local response_options if they're' set, otherwise
|
|
64
|
+
# use the value from the original_message attributes if they're' set
|
|
65
|
+
time_to_live = @time_to_live || original_message['qwirk:response:time_to_live']
|
|
66
|
+
persistent = @persistent
|
|
67
|
+
persistent = (original_message['qwirk:response:persistent'] == 'true') if persistent.nil? && original_message['qwirk:response:persistent']
|
|
68
|
+
# If persistent isn't set anywhere, then default to true unless time_to_live has been set
|
|
69
|
+
persistent = !time_to_live if persistent.nil?
|
|
70
|
+
# The reply is persistent if we explicitly set it or if we don't expire
|
|
71
|
+
producer.delivery_mode_sym = persistent ? :persistent : :non_persistent
|
|
72
|
+
producer.time_to_live = time_to_live.to_i if time_to_live
|
|
73
|
+
reply_message = Qwirk::QueueAdapter::JMS.create_message(@session, marshaled_object, marshaler.marshal_type)
|
|
74
|
+
reply_message.jms_correlation_id = original_message.jms_message_id
|
|
75
|
+
reply_message['qwirk:marshal'] = marshaler.to_sym.to_s
|
|
76
|
+
reply_message['qwirk:worker'] = @name
|
|
77
|
+
reply_message['QwirkTaskID'] = original_message['QwirkTaskID'] if original_message['QwirkTaskID']
|
|
78
|
+
yield reply_message if block_given?
|
|
79
|
+
producer.send(reply_message)
|
|
80
|
+
end
|
|
81
|
+
rescue Exception => e
|
|
82
|
+
Qwirk.logger.error {"Error attempting to send response: #{e.message}\n\t#{e.backtrace.join("\n\t")}"}
|
|
83
|
+
end
|
|
84
|
+
return true
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Handle Messaging and Queuing using JMS
|
|
2
|
+
module Qwirk
|
|
3
|
+
module QueueAdapter
|
|
4
|
+
module JMS
|
|
5
|
+
class WorkerConfig
|
|
6
|
+
include Rumx::Bean
|
|
7
|
+
|
|
8
|
+
#bean_reader :queue_size, :integer, 'Current count of messages in the queue'
|
|
9
|
+
|
|
10
|
+
attr_reader :connection, :parent, :destination, :marshaler, :time_to_live, :persistent, :stopped
|
|
11
|
+
|
|
12
|
+
def initialize(queue_adapter, parent, queue_name, topic_name, options, response_options)
|
|
13
|
+
@connection = queue_adapter.adapter_info
|
|
14
|
+
@parent = parent
|
|
15
|
+
@destination = {:queue_name => queue_name} if queue_name
|
|
16
|
+
@destination = {:topic_name => topic_name} if topic_name
|
|
17
|
+
# Time in msec until the message gets discarded, should be more than the timeout on the requestor side
|
|
18
|
+
@time_to_live = response_options[:time_to_live]
|
|
19
|
+
@persistent = response_options[:persistent]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Default marshal type for the response
|
|
23
|
+
def default_marshal_sym
|
|
24
|
+
:ruby
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def create_worker
|
|
28
|
+
Worker.new(self)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def stop
|
|
32
|
+
puts "in jms worker config stop"
|
|
33
|
+
@stopped = true
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|