amqp 0.8.0.rc13 → 0.8.0.rc14
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -1
- data/.travis.yml +8 -2
- data/.yardopts +1 -0
- data/CHANGELOG +9 -0
- data/Gemfile +17 -11
- data/README.md +26 -16
- data/amqp.gemspec +2 -2
- data/bin/ci/before_build.sh +21 -0
- data/docs/08Migration.textile +199 -5
- data/docs/AMQP091ModelExplained.textile +322 -0
- data/docs/Bindings.textile +24 -4
- data/docs/Clustering.textile +1 -1
- data/docs/ConnectingToTheBroker.textile +98 -82
- data/docs/ConnectionEncryptionWithTLS.textile +65 -5
- data/docs/DocumentationGuidesIndex.textile +93 -13
- data/docs/Durability.textile +1 -1
- data/docs/ErrorHandling.textile +458 -94
- data/docs/Exchanges.textile +901 -87
- data/docs/GettingStarted.textile +278 -143
- data/docs/PatternsAndUseCases.textile +420 -0
- data/docs/Queues.textile +730 -178
- data/docs/RabbitMQVersions.textile +18 -3
- data/docs/RunningTests.textile +1 -1
- data/docs/TestingWithEventedSpec.textile +121 -0
- data/docs/Troubleshooting.textile +15 -1
- data/docs/VendorSpecificExtensions.textile +1 -1
- data/docs/diagrams/001_hello_world_example_routing.png +0 -0
- data/docs/diagrams/002_blabbr_example_routing.png +0 -0
- data/docs/diagrams/003_weathr_example_routing.png +0 -0
- data/docs/diagrams/004_fanout_exchange.png +0 -0
- data/docs/diagrams/005_direct_exchange.png +0 -0
- data/docs/diagrams/redhat/direct_exchange.png +0 -0
- data/docs/diagrams/redhat/fanout_exchange.png +0 -0
- data/docs/diagrams/redhat/topic_exchange.png +0 -0
- data/examples/error_handling/automatic_recovery_of_channel_and_queues.rb +50 -0
- data/examples/error_handling/automatically_recovering_hello_world_consumer.rb +51 -0
- data/examples/error_handling/automatically_recovering_hello_world_consumer_that_uses_a_server_named_queue.rb +51 -0
- data/examples/error_handling/basic_connection_failover.rb +22 -0
- data/examples/error_handling/channel_level_exception.rb +9 -2
- data/examples/error_handling/connection_level_exception.rb +8 -1
- data/examples/error_handling/connection_level_exception_with_objects.rb +49 -0
- data/examples/error_handling/connection_loss_handler.rb +1 -5
- data/examples/error_handling/hello_world_producer.rb +43 -0
- data/examples/error_handling/insufficient_permissions.rb +54 -0
- data/examples/error_handling/manual_connection_and_channel_recovery.rb +71 -0
- data/examples/error_handling/queue_exclusivity_violation.rb +41 -0
- data/examples/error_handling/queue_name_violation.rb +31 -0
- data/examples/exchanges/autodeletion_of_exchanges.rb +1 -4
- data/examples/guides/queues/01a_declaring_a_server_named_queue_using_queue_constructor.rb +7 -8
- data/examples/guides/queues/01b_declaring_a_queue_using_queue_constructor.rb +7 -8
- data/examples/guides/queues/02a_declaring_a_durable_shared_queue.rb +5 -8
- data/examples/guides/queues/02b_declaring_a_durable_shared_queue.rb +5 -8
- data/examples/guides/queues/03a_declaring_a_temporary_exclusive_queue.rb +7 -8
- data/examples/guides/queues/04_bind_a_queue_using_exchange_instance.rb +9 -10
- data/examples/guides/queues/05_bind_a_queue_using_exchange_name.rb +8 -10
- data/examples/guides/queues/06_subscribe_to_receive_messages.rb +10 -12
- data/examples/guides/queues/07_fetch_a_message_from_the_queue.rb +14 -14
- data/examples/guides/queues/08_unsubscribing_a_consumer.rb +13 -16
- data/examples/guides/queues/09_unbinding_from_exchange.rb +16 -22
- data/examples/guides/queues/10_purge_a_queue.rb +13 -18
- data/examples/guides/queues/11_deleting_a_queue.rb +14 -19
- data/examples/guides/queues/12_objects_that_consume_messages.rb +69 -0
- data/examples/guides/queues/13_objects_that_consume_messages_take_two.rb +89 -0
- data/examples/hello_world.rb +1 -3
- data/examples/hello_world_with_an_empty_string.rb +5 -6
- data/examples/inspecting_server_information.rb +45 -0
- data/examples/issues/issue_93.rb +23 -0
- data/examples/issues/issue_94.rb +23 -0
- data/examples/patterns/command/consumer.rb +45 -0
- data/examples/patterns/command/producer.rb +26 -0
- data/examples/patterns/request_reply/client.rb +29 -0
- data/examples/patterns/request_reply/server.rb +26 -0
- data/examples/publishing/publishing_a_one_off_message.rb +6 -4
- data/examples/publishing/returned_messages.rb +2 -10
- data/examples/queues/accessing_message_metadata.rb +15 -13
- data/examples/queues/queue_status.rb +12 -15
- data/examples/routing/fanout_routing.rb +33 -0
- data/examples/routing/headers_routing.rb +17 -15
- data/examples/routing/round_robin_with_direct_exchange.rb +39 -0
- data/examples/routing/round_robin_with_the_default_exchange.rb +38 -0
- data/examples/routing/unroutable_mandatory_message_is_returned.rb +33 -0
- data/examples/routing/weather_updates.rb +15 -20
- data/examples/tls/using_tls.rb +41 -0
- data/lib/amqp/bit_set.rb +80 -0
- data/lib/amqp/broker.rb +72 -0
- data/lib/amqp/channel.rb +93 -13
- data/lib/amqp/client.rb +11 -22
- data/lib/amqp/compatibility/ruby187_patchlevel_check.rb +2 -0
- data/lib/amqp/connection.rb +2 -3
- data/lib/amqp/consumer.rb +208 -0
- data/lib/amqp/deprecated/fork.rb +2 -0
- data/lib/amqp/deprecated/mq.rb +2 -0
- data/lib/amqp/exchange.rb +6 -4
- data/lib/amqp/extensions/rabbitmq.rb +3 -1
- data/lib/amqp/header.rb +76 -14
- data/lib/amqp/int_allocator.rb +96 -0
- data/lib/amqp/logger.rb +2 -0
- data/lib/amqp/queue.rb +242 -86
- data/lib/amqp/rpc.rb +2 -0
- data/lib/amqp/session.rb +169 -9
- data/lib/amqp/utilities/event_loop_helper.rb +2 -0
- data/lib/amqp/utilities/server_type.rb +2 -0
- data/lib/amqp/version.rb +2 -2
- data/lib/mq.rb +4 -2
- data/lib/mq/logger.rb +3 -1
- data/lib/mq/rpc.rb +3 -1
- data/spec/integration/authentication_spec.rb +17 -10
- data/spec/integration/automatic_binding_for_default_direct_exchange_spec.rb +1 -1
- data/spec/integration/automatic_recovery_predicate_spec.rb +68 -0
- data/spec/integration/basic_get_spec.rb +2 -1
- data/spec/integration/{extensions/basic_return_spec.rb → basic_return_spec.rb} +2 -1
- data/spec/integration/channel_level_exception_handling_spec.rb +53 -0
- data/spec/integration/connection_level_exception_handling_spec.rb +49 -0
- data/spec/integration/declare_and_immediately_bind_a_server_named_queue_spec.rb +38 -17
- data/spec/integration/declare_one_hundred_server_named_queues_spec.rb +44 -0
- data/spec/integration/direct_exchange_routing_spec.rb +125 -0
- data/spec/integration/exchange_declaration_spec.rb +75 -46
- data/spec/integration/extensions/rabbitmq/publisher_confirmations_spec.rb +180 -0
- data/spec/integration/{workload_distribution_spec.rb → fanout_exchange_routing_spec.rb} +10 -9
- data/spec/integration/headers_exchange_routing_spec.rb +269 -0
- data/spec/integration/hello_world_spec.rb +77 -0
- data/spec/integration/immediate_messages_spec.rb +59 -0
- data/spec/integration/mandatory_messages_spec.rb +52 -0
- data/spec/integration/message_metadata_access_spec.rb +106 -0
- data/spec/integration/multiple_consumers_per_queue_spec.rb +319 -0
- data/spec/integration/ordering_of_published_messages_spec.rb +96 -0
- data/spec/integration/queue_declaration_spec.rb +8 -8
- data/spec/integration/queue_status_spec.rb +66 -0
- data/spec/integration/recovery/per_channel_automatic_recovery_on_graceful_broker_shutdown_spec.rb +76 -0
- data/spec/integration/recovery/per_channel_automatic_recovery_spec.rb +72 -0
- data/spec/integration/redelivery_of_unacknowledged_messages_spec.rb +96 -0
- data/spec/integration/regressions/concurrent_publishing_on_the_same_channel_spec.rb +91 -0
- data/spec/integration/regressions/empty_message_body_spec.rb +56 -0
- data/spec/integration/regressions/issue66_spec.rb +2 -1
- data/spec/integration/reply_queue_communication_spec.rb +2 -1
- data/spec/integration/store_and_forward_spec.rb +4 -3
- data/spec/integration/topic_subscription_spec.rb +2 -1
- data/spec/integration/tx_commit_spec.rb +124 -0
- data/spec/integration/tx_rollback_spec.rb +167 -0
- data/spec/spec_helper.rb +44 -71
- data/spec/unit/amqp/bit_set_spec.rb +127 -0
- data/spec/unit/amqp/channel_id_allocation_spec.rb +40 -0
- data/spec/unit/amqp/connection_spec.rb +4 -2
- data/spec/unit/amqp/int_allocator_spec.rb +116 -0
- metadata +92 -26
- data/CONTRIBUTORS +0 -29
- data/docs/Routing.textile +0 -30
- data/examples/real-world/task-queue/README.textile +0 -3
- data/examples/real-world/task-queue/consumer.rb +0 -27
- data/examples/real-world/task-queue/producer.rb +0 -22
- data/spec/unit/amqp/basic_spec.rb +0 -39
- data/tasks.rb +0 -4
@@ -6,17 +6,15 @@ require "amqp"
|
|
6
6
|
|
7
7
|
# Binding a queue to an exchange
|
8
8
|
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
9
|
-
AMQP::Channel.new
|
10
|
-
|
9
|
+
channel = AMQP::Channel.new(connection)
|
10
|
+
exchange_name = "amq.fanout"
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
12
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
13
|
+
queue.bind(exchange_name)
|
14
|
+
puts "Bound #{queue.name} to #{exchange_name}"
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
16
|
+
connection.close {
|
17
|
+
EventMachine.stop { exit }
|
18
|
+
}
|
21
19
|
end
|
22
20
|
end
|
@@ -5,21 +5,19 @@ require "rubygems"
|
|
5
5
|
require "amqp"
|
6
6
|
|
7
7
|
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
8
|
-
AMQP::Channel.new
|
9
|
-
|
8
|
+
channel = AMQP::Channel.new(connection)
|
9
|
+
exchange = channel.fanout("amq.fanout")
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue|
|
12
|
+
queue.bind(exchange).subscribe do |metadata, payload|
|
13
|
+
puts "Received a message: #{payload.inspect}. Shutting down..."
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
}
|
18
|
-
end
|
15
|
+
connection.close { EventMachine.stop }
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
EventMachine.add_timer(0.2) do
|
19
|
+
puts "=> Publishing..."
|
20
|
+
exchange.publish("Ohai!")
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
@@ -5,23 +5,23 @@ require "rubygems"
|
|
5
5
|
require "amqp"
|
6
6
|
|
7
7
|
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
8
|
-
AMQP::Channel.new
|
9
|
-
|
8
|
+
channel = AMQP::Channel.new(connection)
|
9
|
+
exchange = channel.fanout("amq.fanout")
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
EventMachine.add_timer(0.5) do
|
18
|
-
queue.pop do |response|
|
19
|
-
puts "Fetched a message: #{response.inspect}. Shutting down..."
|
11
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
12
|
+
queue.bind(exchange)
|
13
|
+
puts "Bound. Publishing a message..."
|
14
|
+
exchange.publish("Ohai!")
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
EventMachine.add_timer(0.5) do
|
17
|
+
queue.pop do |metadata, payload|
|
18
|
+
if payload
|
19
|
+
puts "Fetched a message: #{payload.inspect}, content_type: #{metadata.content_type}. Shutting down..."
|
20
|
+
else
|
21
|
+
puts "No messages in the queue"
|
24
22
|
end
|
23
|
+
|
24
|
+
connection.close { EventMachine.stop }
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -4,23 +4,20 @@
|
|
4
4
|
require "rubygems"
|
5
5
|
require "amqp"
|
6
6
|
|
7
|
-
AMQP.start("amqp://guest:guest@dev.rabbitmq.com
|
8
|
-
AMQP::Channel.new
|
9
|
-
|
7
|
+
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
8
|
+
channel = AMQP::Channel.new(connection)
|
9
|
+
exchange = channel.fanout("amq.fanout")
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
12
|
+
queue.bind(exchange).subscribe do |headers, payload|
|
13
|
+
puts "Received a new message"
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
EventMachine.add_timer(0.3) do
|
17
|
+
queue.unsubscribe
|
18
|
+
puts "Unsubscribed. Shutting down..."
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end # EventMachine.add_timer
|
24
|
-
end # channel.queue
|
25
|
-
end
|
20
|
+
connection.close { EventMachine.stop }
|
21
|
+
end # EventMachine.add_timer
|
22
|
+
end # channel.queue
|
26
23
|
end
|
@@ -4,29 +4,23 @@
|
|
4
4
|
require "rubygems"
|
5
5
|
require "amqp"
|
6
6
|
|
7
|
-
AMQP.start("amqp://guest:guest@dev.rabbitmq.com
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
raise "Channel-level exception: #{channel_close.reply_text}"
|
13
|
-
end
|
14
|
-
exchange = channel.fanout("amq.fanout")
|
7
|
+
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
8
|
+
channel = AMQP::Channel.new(connection)
|
9
|
+
channel.on_error do |ch, channel_close|
|
10
|
+
raise "Channel-level exception: #{channel_close.reply_text}"
|
11
|
+
end
|
15
12
|
|
16
|
-
|
17
|
-
queue.bind(exchange) do |_|
|
18
|
-
puts "Bound"
|
19
|
-
end
|
13
|
+
exchange = channel.fanout("amq.fanout")
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
puts "Unbound. Shutting down..."
|
15
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
16
|
+
queue.bind(exchange)
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
18
|
+
EventMachine.add_timer(0.5) do
|
19
|
+
queue.unbind(exchange) do |_|
|
20
|
+
puts "Unbound. Shutting down..."
|
21
|
+
|
22
|
+
connection.close { EventMachine.stop }
|
23
|
+
end
|
24
|
+
end # EventMachine.add_timer
|
25
|
+
end # channel.queue
|
32
26
|
end
|
@@ -5,24 +5,19 @@ require "rubygems"
|
|
5
5
|
require "amqp"
|
6
6
|
|
7
7
|
AMQP.start("amqp://guest:guest@dev.rabbitmq.com:5672") do |connection, open_ok|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
exchange = channel.fanout("amq.fanout")
|
8
|
+
channel = AMQP::Channel.new(connection)
|
9
|
+
channel.on_error do |ch, channel_close|
|
10
|
+
raise "Channel-level exception: #{channel_close.reply_text}"
|
11
|
+
end
|
12
|
+
exchange = channel.fanout("amq.fanout")
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
15
|
+
queue.purge do |_|
|
16
|
+
puts "Purged #{queue.name}"
|
17
|
+
end
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end # EventMachine.add_timer
|
26
|
-
end # channel.queue
|
27
|
-
end
|
19
|
+
EventMachine.add_timer(0.5) do
|
20
|
+
connection.close { EventMachine.stop }
|
21
|
+
end # EventMachine.add_timer
|
22
|
+
end # channel.queue
|
28
23
|
end
|
@@ -4,24 +4,19 @@
|
|
4
4
|
require "rubygems"
|
5
5
|
require "amqp"
|
6
6
|
|
7
|
-
AMQP.start("amqp://guest:guest@dev.rabbitmq.com
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
channel.on_error do |ch, channel_close|
|
12
|
-
raise "Channel-level exception: #{channel_close.reply_text}"
|
13
|
-
end
|
14
|
-
exchange = channel.fanout("amq.fanout")
|
15
|
-
|
16
|
-
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
17
|
-
EventMachine.add_timer(0.5) do
|
18
|
-
queue.delete do
|
19
|
-
puts "Deleted #{queue.name}"
|
20
|
-
connection.close {
|
21
|
-
EM.stop { exit }
|
22
|
-
}
|
23
|
-
end
|
24
|
-
end # EventMachine.add_timer
|
25
|
-
end # channel.queue
|
7
|
+
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
8
|
+
channel = AMQP::Channel.new(connection)
|
9
|
+
channel.on_error do |ch, channel_close|
|
10
|
+
raise "Channel-level exception: #{channel_close.reply_text}"
|
26
11
|
end
|
12
|
+
exchange = channel.fanout("amq.fanout")
|
13
|
+
|
14
|
+
channel.queue("", :auto_delete => true, :exclusive => true) do |queue, declare_ok|
|
15
|
+
EventMachine.add_timer(0.5) do
|
16
|
+
queue.delete do
|
17
|
+
puts "Deleted #{queue.name}"
|
18
|
+
connection.close { EventMachine. stop }
|
19
|
+
end
|
20
|
+
end # EventMachine.add_timer
|
21
|
+
end # channel.queue
|
27
22
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "amqp"
|
6
|
+
|
7
|
+
class Consumer
|
8
|
+
|
9
|
+
#
|
10
|
+
# API
|
11
|
+
#
|
12
|
+
|
13
|
+
def initialize(channel, queue_name = AMQ::Protocol::EMPTY_STRING)
|
14
|
+
@queue_name = queue_name
|
15
|
+
|
16
|
+
@channel = channel
|
17
|
+
@channel.on_error(&method(:handle_channel_exception))
|
18
|
+
end # initialize
|
19
|
+
|
20
|
+
def start
|
21
|
+
@queue = @channel.queue(@queue_name, :exclusive => true)
|
22
|
+
@queue.subscribe(&method(:handle_message))
|
23
|
+
end # start
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
#
|
28
|
+
# Implementation
|
29
|
+
#
|
30
|
+
|
31
|
+
def handle_message(metadata, payload)
|
32
|
+
puts "Received a message: #{payload}, content_type = #{metadata.content_type}"
|
33
|
+
end # handle_message(metadata, payload)
|
34
|
+
|
35
|
+
def handle_channel_exception(channel, channel_close)
|
36
|
+
puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
|
37
|
+
end # handle_channel_exception(channel, channel_close)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
class Producer
|
42
|
+
|
43
|
+
#
|
44
|
+
# API
|
45
|
+
#
|
46
|
+
|
47
|
+
def initialize(channel, exchange)
|
48
|
+
@channel = channel
|
49
|
+
@exchange = exchange
|
50
|
+
end # initialize(channel, exchange)
|
51
|
+
|
52
|
+
def publish(message, options = {})
|
53
|
+
@exchange.publish(message, options)
|
54
|
+
end # publish(message, options = {})
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
59
|
+
channel = AMQP::Channel.new(connection)
|
60
|
+
worker = Consumer.new(channel, "amqpgem.objects.integration")
|
61
|
+
worker.start
|
62
|
+
|
63
|
+
producer = Producer.new(channel, channel.default_exchange)
|
64
|
+
puts "Publishing..."
|
65
|
+
producer.publish("Hello, world", :routing_key => "amqpgem.objects.integration")
|
66
|
+
|
67
|
+
# stop in 2 seconds
|
68
|
+
EventMachine.add_timer(2.0) { connection.close { EventMachine.stop } }
|
69
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "amqp"
|
6
|
+
|
7
|
+
class Consumer
|
8
|
+
|
9
|
+
#
|
10
|
+
# API
|
11
|
+
#
|
12
|
+
|
13
|
+
def handle_message(metadata, payload)
|
14
|
+
puts "Received a message: #{payload}, content_type = #{metadata.content_type}"
|
15
|
+
end # handle_message(metadata, payload)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
class Worker
|
20
|
+
|
21
|
+
#
|
22
|
+
# API
|
23
|
+
#
|
24
|
+
|
25
|
+
|
26
|
+
def initialize(channel, queue_name = AMQ::Protocol::EMPTY_STRING, consumer = Consumer.new)
|
27
|
+
@queue_name = queue_name
|
28
|
+
|
29
|
+
@channel = channel
|
30
|
+
@channel.on_error(&method(:handle_channel_exception))
|
31
|
+
|
32
|
+
@consumer = consumer
|
33
|
+
end # initialize
|
34
|
+
|
35
|
+
def start
|
36
|
+
@queue = @channel.queue(@queue_name, :exclusive => true)
|
37
|
+
@queue.subscribe(&@consumer.method(:handle_message))
|
38
|
+
end # start
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
#
|
43
|
+
# Implementation
|
44
|
+
#
|
45
|
+
|
46
|
+
def handle_channel_exception(channel, channel_close)
|
47
|
+
puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
|
48
|
+
end # handle_channel_exception(channel, channel_close)
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
class Producer
|
53
|
+
|
54
|
+
#
|
55
|
+
# API
|
56
|
+
#
|
57
|
+
|
58
|
+
def initialize(channel, exchange)
|
59
|
+
@channel = channel
|
60
|
+
@exchange = exchange
|
61
|
+
end # initialize(channel, exchange)
|
62
|
+
|
63
|
+
def publish(message, options = {})
|
64
|
+
@exchange.publish(message, options)
|
65
|
+
end # publish(message, options = {})
|
66
|
+
|
67
|
+
|
68
|
+
#
|
69
|
+
# Implementation
|
70
|
+
#
|
71
|
+
|
72
|
+
def handle_channel_exception(channel, channel_close)
|
73
|
+
puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
|
74
|
+
end # handle_channel_exception(channel, channel_close)
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
|
79
|
+
channel = AMQP::Channel.new(connection)
|
80
|
+
worker = Worker.new(channel, "amqpgem.objects.integration")
|
81
|
+
worker.start
|
82
|
+
|
83
|
+
producer = Producer.new(channel, channel.default_exchange)
|
84
|
+
puts "Publishing..."
|
85
|
+
producer.publish("Hello, world", :routing_key => "amqpgem.objects.integration")
|
86
|
+
|
87
|
+
# stop in 2 seconds
|
88
|
+
EventMachine.add_timer(2.0) { connection.close { EventMachine.stop } }
|
89
|
+
end
|
data/examples/hello_world.rb
CHANGED
@@ -19,9 +19,7 @@ EventMachine.run do
|
|
19
19
|
queue.subscribe do |payload|
|
20
20
|
puts "Received a message: #{payload}. Disconnecting..."
|
21
21
|
|
22
|
-
connection.close {
|
23
|
-
EM.stop { exit }
|
24
|
-
}
|
22
|
+
connection.close { EventMachine.stop }
|
25
23
|
end
|
26
24
|
|
27
25
|
exchange.publish "Hello, world!", :routing_key => queue.name, :app_id => "Hello world"
|
@@ -9,25 +9,24 @@ $:.unshift(File.expand_path("../../lib", __FILE__))
|
|
9
9
|
require 'amqp'
|
10
10
|
|
11
11
|
EventMachine.run do
|
12
|
-
connection = AMQP.connect(:host => '127.0.0.1'
|
12
|
+
connection = AMQP.connect(:host => '127.0.0.1')
|
13
13
|
puts "Connected to AMQP broker. Running #{AMQP::VERSION} version of the gem..."
|
14
14
|
|
15
15
|
|
16
16
|
connection.on_error do |conn, connection_close|
|
17
17
|
puts "Handling a connection-level exception: #{connection_close.reply_text}"
|
18
|
+
EventMachine.stop
|
18
19
|
end
|
19
20
|
|
20
21
|
channel = AMQP::Channel.new(connection)
|
21
|
-
queue = channel.queue("amqpgem.examples.hello_world", :auto_delete =>
|
22
|
+
queue = channel.queue("amqpgem.examples.hello_world", :auto_delete => true)
|
22
23
|
exchange = channel.direct("")
|
23
24
|
|
24
25
|
queue.subscribe do |payload|
|
25
26
|
puts "Received a message: #{payload}. Disconnecting..."
|
26
27
|
|
27
|
-
connection.close {
|
28
|
-
EM.stop { exit }
|
29
|
-
}
|
28
|
+
connection.close { EventMachine.stop }
|
30
29
|
end
|
31
30
|
|
32
|
-
exchange.publish "
|
31
|
+
exchange.publish "", :routing_key => queue.name, :app_id => "Hello world"
|
33
32
|
end
|