Class: EasyBunnyRPC::Client
- Inherits:
-
Object
- Object
- EasyBunnyRPC::Client
- Defined in:
- lib/easy_bunny_rpc/client.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #pop ⇒ Object
- #publish(payload, correlation_id = default_correlation_id) ⇒ Object
- #set_timeout(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/easy_bunny_rpc/client.rb', line 7 def initialize ={} @options = @subscribed = false # The timed queue which will collect incoming messages @timed_queue = EasyBunnyRPC::TimedQueue.new # Need to set a default timeout. How about 5 seconds? set_timeout(5) end |
Instance Method Details
#close ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/easy_bunny_rpc/client.rb', line 33 def close if defined?(@channel) channel.close remove_instance_variable :@channel remove_instance_variable :@queue end if defined?(@connection) connection.close remove_instance_variable :@connection end if defined?(@exchange) remove_instance_variable :@exchange end @subscribed = false end |
#pop ⇒ Object
18 19 20 21 22 |
# File 'lib/easy_bunny_rpc/client.rb', line 18 def pop correlation_id, payload = @timed_queue.pop_with_timeout(@timeout) JSON.parse(payload).merge!({ 'correlation_id' => correlation_id }) end |
#publish(payload, correlation_id = default_correlation_id) ⇒ Object
28 29 30 31 |
# File 'lib/easy_bunny_rpc/client.rb', line 28 def publish(payload, correlation_id=default_correlation_id) start_subscription unless @subscribed exchange.publish([payload].to_json, routing_key: @options[:queue], correlation_id: correlation_id, reply_to: queue.name, expiration: (@timeout*1000).to_i) end |
#set_timeout(value) ⇒ Object
24 25 26 |
# File 'lib/easy_bunny_rpc/client.rb', line 24 def set_timeout(value) @timeout = value end |