sparqcode_bunny 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.rspec +3 -0
- data/.travis.yml +15 -0
- data/.yardopts +9 -0
- data/CHANGELOG +27 -0
- data/Gemfile +39 -0
- data/LICENSE +21 -0
- data/README.textile +82 -0
- data/Rakefile +14 -0
- data/bunny.gemspec +43 -0
- data/examples/simple_08.rb +32 -0
- data/examples/simple_09.rb +32 -0
- data/examples/simple_ack_08.rb +35 -0
- data/examples/simple_ack_09.rb +35 -0
- data/examples/simple_consumer_08.rb +55 -0
- data/examples/simple_consumer_09.rb +55 -0
- data/examples/simple_fanout_08.rb +41 -0
- data/examples/simple_fanout_09.rb +41 -0
- data/examples/simple_headers_08.rb +42 -0
- data/examples/simple_headers_09.rb +42 -0
- data/examples/simple_publisher_08.rb +29 -0
- data/examples/simple_publisher_09.rb +29 -0
- data/examples/simple_topic_08.rb +61 -0
- data/examples/simple_topic_09.rb +61 -0
- data/ext/amqp-0.8.json +616 -0
- data/ext/amqp-0.9.1.json +388 -0
- data/ext/config.yml +4 -0
- data/ext/qparser.rb +469 -0
- data/lib/bunny/channel08.rb +39 -0
- data/lib/bunny/channel09.rb +39 -0
- data/lib/bunny/client08.rb +472 -0
- data/lib/bunny/client09.rb +374 -0
- data/lib/bunny/consumer.rb +35 -0
- data/lib/bunny/exchange08.rb +171 -0
- data/lib/bunny/exchange09.rb +159 -0
- data/lib/bunny/queue08.rb +403 -0
- data/lib/bunny/queue09.rb +325 -0
- data/lib/bunny/subscription08.rb +87 -0
- data/lib/bunny/subscription09.rb +87 -0
- data/lib/bunny/system_timer.rb +14 -0
- data/lib/bunny/version.rb +5 -0
- data/lib/bunny.rb +109 -0
- data/lib/qrack/amq-client-url.rb +165 -0
- data/lib/qrack/channel.rb +20 -0
- data/lib/qrack/client.rb +235 -0
- data/lib/qrack/errors.rb +5 -0
- data/lib/qrack/protocol/protocol08.rb +134 -0
- data/lib/qrack/protocol/protocol09.rb +135 -0
- data/lib/qrack/protocol/spec08.rb +828 -0
- data/lib/qrack/protocol/spec09.rb +524 -0
- data/lib/qrack/qrack08.rb +20 -0
- data/lib/qrack/qrack09.rb +20 -0
- data/lib/qrack/queue.rb +40 -0
- data/lib/qrack/subscription.rb +112 -0
- data/lib/qrack/transport/buffer08.rb +278 -0
- data/lib/qrack/transport/buffer09.rb +280 -0
- data/lib/qrack/transport/frame08.rb +117 -0
- data/lib/qrack/transport/frame09.rb +97 -0
- data/spec/spec_08/bunny_spec.rb +77 -0
- data/spec/spec_08/connection_spec.rb +25 -0
- data/spec/spec_08/exchange_spec.rb +173 -0
- data/spec/spec_08/queue_spec.rb +235 -0
- data/spec/spec_09/amqp_url_spec.rb +19 -0
- data/spec/spec_09/bunny_spec.rb +76 -0
- data/spec/spec_09/connection_spec.rb +29 -0
- data/spec/spec_09/exchange_spec.rb +173 -0
- data/spec/spec_09/queue_spec.rb +248 -0
- metadata +151 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# https://github.com/travis-ci/travis-ci/wiki/.travis.yml-options
|
2
|
+
bundler_args: --without development
|
3
|
+
script: "bundle exec rspec spec"
|
4
|
+
rvm:
|
5
|
+
- 1.8.7
|
6
|
+
- ree
|
7
|
+
- 1.9.2
|
8
|
+
- 1.9.3
|
9
|
+
- rbx-2.0
|
10
|
+
notifications:
|
11
|
+
recipients:
|
12
|
+
- celldee@gmail.com
|
13
|
+
- jakub@rabbitmq.com
|
14
|
+
- skaes@railsexpress.de
|
15
|
+
- eric@5stops.com
|
data/.yardopts
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
= Version 0.7.8
|
2
|
+
|
3
|
+
* test suite cleanup (eliminated some race conditions related to queue.message_count)
|
4
|
+
|
5
|
+
= Version 0.7.7
|
6
|
+
|
7
|
+
* avoid warnings caused by duplicating code from the amq-client gem
|
8
|
+
|
9
|
+
= Version 0.7.6
|
10
|
+
|
11
|
+
* API mismatch between Timer/SystemTimer on Ruby 1.9 vs Ruby 1.8 is resolved.
|
12
|
+
|
13
|
+
= Version 0.7.3
|
14
|
+
|
15
|
+
* AMQP connection URI parser now respects port
|
16
|
+
|
17
|
+
= Version 0.7.2
|
18
|
+
|
19
|
+
* Fixes for irb tab completion
|
20
|
+
|
21
|
+
= Version 0.7.0
|
22
|
+
|
23
|
+
* Added :content_type option to Exchange#publish
|
24
|
+
* Lots of minor fixes
|
25
|
+
|
26
|
+
= Version 0.6.3
|
27
|
+
* Encoding of source files is set to UTF-8 on Ruby 1.9.
|
data/Gemfile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
source :rubygems
|
4
|
+
|
5
|
+
# Use local clones if possible.
|
6
|
+
# If you want to use your local copy, just symlink it to vendor.
|
7
|
+
# See http://blog.101ideas.cz/posts/custom-gems-in-gemfile.html
|
8
|
+
extend Module.new {
|
9
|
+
def gem(name, *args)
|
10
|
+
options = args.last.is_a?(Hash) ? args.last : Hash.new
|
11
|
+
|
12
|
+
local_path = File.expand_path("../vendor/#{name}", __FILE__)
|
13
|
+
if File.exist?(local_path)
|
14
|
+
super name, options.merge(:path => local_path).
|
15
|
+
delete_if { |key, _| [:git, :branch].include?(key) }
|
16
|
+
else
|
17
|
+
super name, *args
|
18
|
+
end
|
19
|
+
end
|
20
|
+
}
|
21
|
+
|
22
|
+
gem "SystemTimer", "1.2", :platform => :ruby_18
|
23
|
+
|
24
|
+
group :development do
|
25
|
+
gem "rake"
|
26
|
+
|
27
|
+
gem "yard", ">= 0.7.2"
|
28
|
+
|
29
|
+
# Yard tags this buddy along.
|
30
|
+
gem "RedCloth", :platform => :mri
|
31
|
+
|
32
|
+
gem "changelog"
|
33
|
+
end
|
34
|
+
|
35
|
+
group :test do
|
36
|
+
gem "rspec", "~> 2.6.0"
|
37
|
+
end
|
38
|
+
|
39
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2009 – 2011 Chris Duncan, Jakub Stastny aka botanicus,
|
2
|
+
Michael S. Klishin, Eric Lindvall, Stefan Kaes and contributors.
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
h1. About
|
2
|
+
|
3
|
+
Bunny is a synchronous "AMQP":http://bit.ly/hw2ELX client. It supports Ruby 1.9.2, 1.8.7, Ruby Enterprise Edition and JRuby. Protocol-wise, Bunny supports AMQP 0.9.1 and 0.8. Support for AMQP 0.8 will be dropped in the next version of Bunny (0.7) because most of popular AMQP brokers such as RabbitMQ already stopped or planning to stop supporting it in the near future.
|
4
|
+
|
5
|
+
Bunny is based on a great deal of useful code from the "amqp Ruby gem":http://github.com/ruby-amqp/amqp and "Carrot":http://github.com/famoseagle/carrot.
|
6
|
+
|
7
|
+
You can use Bunny to:
|
8
|
+
|
9
|
+
* Create and delete exchanges
|
10
|
+
* Create and delete queues
|
11
|
+
* Publish and consume messages
|
12
|
+
|
13
|
+
h1. Differences from Official Gem
|
14
|
+
|
15
|
+
We have added two things to the gem:
|
16
|
+
* the ability to unsubscribe from a queue once the queue is empty (unreliable)
|
17
|
+
* the ability to break out of a subscription loop if a condition has been met
|
18
|
+
|
19
|
+
h1. Quick Start
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
require "bunny"
|
23
|
+
|
24
|
+
b = Bunny.new(:logging => true)
|
25
|
+
|
26
|
+
# start a communication session with the amqp server
|
27
|
+
b.start
|
28
|
+
b.qos # must be enabled for rpc features to work
|
29
|
+
|
30
|
+
# declare a queue
|
31
|
+
q = b.queue("test1")
|
32
|
+
e = b.exchange('')
|
33
|
+
|
34
|
+
correlation_id = 123
|
35
|
+
|
36
|
+
# publish a message to the queue
|
37
|
+
e.publish("Junk", :key => q.name, :correlation_id => 0)
|
38
|
+
e.publish("Junk", :key => q.name, :correlation_id => 1)
|
39
|
+
e.publish("Hello everybody!", :key => q.name, :correlation_id => correlation_id)
|
40
|
+
e.publish("Junk", :key => q.name, :correlation_id => 2)
|
41
|
+
|
42
|
+
expected_msg = nil
|
43
|
+
|
44
|
+
# get message from the queue
|
45
|
+
q.subscribe(:ack => true) do |msg|
|
46
|
+
if msg[:header].attributes[:correlation_id].to_i == correlation_id
|
47
|
+
expected_msg = msg[:payload]
|
48
|
+
msg[:subscribed] = false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
puts "This is the correct message: " + expected_msg + "\n\n"
|
53
|
+
|
54
|
+
# close the connection
|
55
|
+
b.stop
|
56
|
+
</pre>
|
57
|
+
|
58
|
+
... or just:
|
59
|
+
|
60
|
+
<pre>
|
61
|
+
require "bunny"
|
62
|
+
|
63
|
+
# Create a direct queue named "my_testq"
|
64
|
+
Bunny.run { |c| c.queue("my_testq") }
|
65
|
+
</pre>
|
66
|
+
|
67
|
+
Please see the @examples@ directory for additional usage information.
|
68
|
+
|
69
|
+
h2. Intended Use
|
70
|
+
|
71
|
+
# Generate a correlation_id
|
72
|
+
# Send a message to the queue with the correlation_id set
|
73
|
+
# Subscribe to the reply queue
|
74
|
+
# Process messages until you get the correlation_id you want
|
75
|
+
# tell the subscription you are no longer interested in messages
|
76
|
+
|
77
|
+
h1. Links
|
78
|
+
|
79
|
+
* "Source code":http://github.com/sparqcode/bunny
|
80
|
+
* "@rubyamqp":http://twitter.com/rubyamqp at Twitter
|
81
|
+
* "Ruby AMQP Google Group":http://groups.google.com/group/ruby-amqp
|
82
|
+
* "Blog":http://bunnyamqp.wordpress.com
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
desc "Run rspec tests"
|
6
|
+
task :spec do
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
RSpec::Core::RakeTask.new("spec") do |t|
|
9
|
+
t.pattern = "spec/spec_*/*_spec.rb"
|
10
|
+
t.rspec_opts = ['--color', '--format doc']
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [ :spec ]
|
data/bunny.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env gem build
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require "base64"
|
5
|
+
require File.expand_path("../lib/bunny/version", __FILE__)
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "sparqcode_bunny"
|
9
|
+
s.version = Bunny::VERSION.dup
|
10
|
+
s.homepage = "http://github.com/sparqcode/bunny"
|
11
|
+
s.summary = "Bunny with some RPC friendly extensions"
|
12
|
+
s.description = "A synchronous Ruby AMQP client that enables interaction with AMQP-compliant brokers with minor modifications to make RPC easier. The official gem will be best for you 99.9973% of the time."
|
13
|
+
|
14
|
+
# Sorted alphabetically.
|
15
|
+
s.authors = [
|
16
|
+
"Chris Duncan",
|
17
|
+
"Eric Lindvall",
|
18
|
+
"Jakub Stastny aka botanicus",
|
19
|
+
"Michael S. Klishin",
|
20
|
+
"Stefan Kaes"]
|
21
|
+
|
22
|
+
s.email = [
|
23
|
+
"Y2VsbGRlZUBnbWFpbC5jb20=\n",
|
24
|
+
"ZXJpY0A1c3RvcHMuY29t\n",
|
25
|
+
"c3Rhc3RueUAxMDFpZGVhcy5jeg==\n",
|
26
|
+
"bWljaGFlbEBub3ZlbWJlcmFpbi5jb20=\n",
|
27
|
+
"c2thZXNAcmFpbHNleHByZXNzLmRl\n"].
|
28
|
+
map { |mail| Base64.decode64(mail) }
|
29
|
+
|
30
|
+
# Files.
|
31
|
+
s.has_rdoc = true
|
32
|
+
s.extra_rdoc_files = ["README.textile"]
|
33
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
34
|
+
s.files = `git ls-files`.split("\n")
|
35
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
|
38
|
+
begin
|
39
|
+
require "changelog"
|
40
|
+
s.post_install_message = CHANGELOG.new.version_changes
|
41
|
+
rescue LoadError
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_08.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new(:logging => true)
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare a queue
|
21
|
+
q = b.queue('test1')
|
22
|
+
|
23
|
+
# publish a message to the queue
|
24
|
+
q.publish('➸ Hello everybody ☺!')
|
25
|
+
|
26
|
+
# get message from the queue
|
27
|
+
msg = q.pop[:payload]
|
28
|
+
|
29
|
+
puts 'This is the message: ' + msg + "\n\n"
|
30
|
+
|
31
|
+
# close the client connection
|
32
|
+
b.stop
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_09.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new(:logging => true, :spec => '09')
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare a queue
|
21
|
+
q = b.queue('test1')
|
22
|
+
|
23
|
+
# publish a message to the queue
|
24
|
+
q.publish('➸ Hello everybody ☺!')
|
25
|
+
|
26
|
+
# get message from the queue
|
27
|
+
msg = q.pop[:payload]
|
28
|
+
|
29
|
+
puts 'This is the message: ' + msg + "\n\n"
|
30
|
+
|
31
|
+
# close the client connection
|
32
|
+
b.stop
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_ack_08.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new(:logging => true)
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare a queue
|
21
|
+
q = b.queue('test1')
|
22
|
+
|
23
|
+
# publish a message to the queue
|
24
|
+
q.publish('Testing acknowledgements')
|
25
|
+
|
26
|
+
# get message from the queue
|
27
|
+
msg = q.pop(:ack => true)[:payload]
|
28
|
+
|
29
|
+
# acknowledge receipt of message
|
30
|
+
q.ack
|
31
|
+
|
32
|
+
puts 'This is the message: ' + msg + "\n\n"
|
33
|
+
|
34
|
+
# close the client connection
|
35
|
+
b.stop
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_ack_09.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new(:logging => true, :spec => '09')
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare a queue
|
21
|
+
q = b.queue('test1')
|
22
|
+
|
23
|
+
# publish a message to the queue
|
24
|
+
q.publish('Testing acknowledgements')
|
25
|
+
|
26
|
+
# get message from the queue
|
27
|
+
msg = q.pop(:ack => true)[:payload]
|
28
|
+
|
29
|
+
# acknowledge receipt of message
|
30
|
+
q.ack
|
31
|
+
|
32
|
+
puts 'This is the message: ' + msg + "\n\n"
|
33
|
+
|
34
|
+
# close the client connection
|
35
|
+
b.stop
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_consumer_08.rb
|
4
|
+
|
5
|
+
# N.B. To be used in conjunction with simple_publisher.rb
|
6
|
+
|
7
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
8
|
+
# and that it is running on 'localhost'.
|
9
|
+
|
10
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
11
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
12
|
+
|
13
|
+
# How this example works
|
14
|
+
#=======================
|
15
|
+
#
|
16
|
+
# Open up two console windows start this program in one of them by typing -
|
17
|
+
#
|
18
|
+
# ruby simple_consumer_08.rb
|
19
|
+
#
|
20
|
+
# Then switch to the other console window and type -
|
21
|
+
#
|
22
|
+
# ruby simple_publisher_08.rb
|
23
|
+
#
|
24
|
+
# A message will be printed out by the simple_consumer and it will wait for the next message
|
25
|
+
# until the timeout interval is reached.
|
26
|
+
#
|
27
|
+
# Run simple_publisher as many times as you like. When you want the program to stop just stop
|
28
|
+
# sending messages and the subscribe loop will timeout after 30 seconds, the program will
|
29
|
+
# unsubscribe from the queue and close the connection to the server.
|
30
|
+
|
31
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
32
|
+
|
33
|
+
require 'bunny'
|
34
|
+
|
35
|
+
b = Bunny.new(:logging => true)
|
36
|
+
|
37
|
+
# start a communication session with the amqp server
|
38
|
+
b.start
|
39
|
+
|
40
|
+
# create/get queue
|
41
|
+
q = b.queue('po_box')
|
42
|
+
|
43
|
+
# create/get exchange
|
44
|
+
exch = b.exchange('sorting_room')
|
45
|
+
|
46
|
+
# bind queue to exchange
|
47
|
+
q.bind(exch, :key => 'fred')
|
48
|
+
|
49
|
+
# subscribe to queue
|
50
|
+
q.subscribe(:consumer_tag => 'testtag1', :timeout => 30) do |msg|
|
51
|
+
puts "#{q.subscription.message_count}: #{msg[:payload]}"
|
52
|
+
end
|
53
|
+
|
54
|
+
# Close client
|
55
|
+
b.stop
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_consumer_09.rb
|
4
|
+
|
5
|
+
# N.B. To be used in conjunction with simple_publisher.rb
|
6
|
+
|
7
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
8
|
+
# and that it is running on 'localhost'.
|
9
|
+
|
10
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
11
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
12
|
+
|
13
|
+
# How this example works
|
14
|
+
#=======================
|
15
|
+
#
|
16
|
+
# Open up two console windows start this program in one of them by typing -
|
17
|
+
#
|
18
|
+
# ruby simple_consumer_09.rb
|
19
|
+
#
|
20
|
+
# Then switch to the other console window and type -
|
21
|
+
#
|
22
|
+
# ruby simple_publisher_09.rb
|
23
|
+
#
|
24
|
+
# A message will be printed out by the simple_consumer and it will wait for the next message
|
25
|
+
# until the timeout interval is reached.
|
26
|
+
#
|
27
|
+
# Run simple_publisher as many times as you like. When you want the program to stop just stop
|
28
|
+
# sending messages and the subscribe loop will timeout after 30 seconds, the program will
|
29
|
+
# unsubscribe from the queue and close the connection to the server.
|
30
|
+
|
31
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
32
|
+
|
33
|
+
require 'bunny'
|
34
|
+
|
35
|
+
b = Bunny.new(:logging => true, :spec => '09')
|
36
|
+
|
37
|
+
# start a communication session with the amqp server
|
38
|
+
b.start
|
39
|
+
|
40
|
+
# create/get queue
|
41
|
+
q = b.queue('po_box')
|
42
|
+
|
43
|
+
# create/get exchange
|
44
|
+
exch = b.exchange('sorting_room')
|
45
|
+
|
46
|
+
# bind queue to exchange
|
47
|
+
q.bind(exch, :key => 'fred')
|
48
|
+
|
49
|
+
# subscribe to queue
|
50
|
+
q.subscribe(:consumer_tag => 'testtag1', :timeout => 30) do |msg|
|
51
|
+
puts "#{q.subscription.message_count}: #{msg[:payload]}"
|
52
|
+
end
|
53
|
+
|
54
|
+
# Close client
|
55
|
+
b.stop
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_fanout_08.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new(:logging => true)
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare queues
|
21
|
+
q1 = b.queue('test_fan1')
|
22
|
+
q2 = b.queue('test_fan2')
|
23
|
+
|
24
|
+
# create a fanout exchange
|
25
|
+
exch = b.exchange('test_fan', :type => :fanout)
|
26
|
+
|
27
|
+
# bind the queues to the exchange
|
28
|
+
q1.bind(exch)
|
29
|
+
q2.bind(exch)
|
30
|
+
|
31
|
+
# publish a message to the exchange
|
32
|
+
exch.publish('This message will be fanned out')
|
33
|
+
|
34
|
+
# get message from the queues
|
35
|
+
msg = q1.pop[:payload]
|
36
|
+
puts 'This is the message from q1: ' + msg + "\n\n"
|
37
|
+
msg = q2.pop[:payload]
|
38
|
+
puts 'This is the message from q2: ' + msg + "\n\n"
|
39
|
+
|
40
|
+
# close the client connection
|
41
|
+
b.stop
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_fanout_09.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new(:logging => true, :spec => '09')
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare queues
|
21
|
+
q1 = b.queue('test_fan1')
|
22
|
+
q2 = b.queue('test_fan2')
|
23
|
+
|
24
|
+
# create a fanout exchange
|
25
|
+
exch = b.exchange('test_fan', :type => :fanout)
|
26
|
+
|
27
|
+
# bind the queues to the exchange
|
28
|
+
q1.bind(exch)
|
29
|
+
q2.bind(exch)
|
30
|
+
|
31
|
+
# publish a message to the exchange
|
32
|
+
exch.publish('This message will be fanned out')
|
33
|
+
|
34
|
+
# get message from the queues
|
35
|
+
msg = q1.pop[:payload]
|
36
|
+
puts 'This is the message from q1: ' + msg + "\n\n"
|
37
|
+
msg = q2.pop[:payload]
|
38
|
+
puts 'This is the message from q2: ' + msg + "\n\n"
|
39
|
+
|
40
|
+
# close the client connection
|
41
|
+
b.stop
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# simple_headers_08.rb
|
4
|
+
|
5
|
+
# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
|
6
|
+
# and that it is running on 'localhost'.
|
7
|
+
|
8
|
+
# If this is not the case, please change the 'Bunny.new' call below to include
|
9
|
+
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
|
10
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
12
|
+
|
13
|
+
require 'bunny'
|
14
|
+
|
15
|
+
b = Bunny.new
|
16
|
+
|
17
|
+
# start a communication session with the amqp server
|
18
|
+
b.start
|
19
|
+
|
20
|
+
# declare queues
|
21
|
+
q = b.queue('header_q1')
|
22
|
+
|
23
|
+
# create a headers exchange
|
24
|
+
header_exch = b.exchange('header_exch', :type => :headers)
|
25
|
+
|
26
|
+
# bind the queue to the exchange
|
27
|
+
q.bind(header_exch, :arguments => {'h1'=>'a','x-match'=>'all'})
|
28
|
+
|
29
|
+
# publish messages to the exchange
|
30
|
+
header_exch.publish('Headers test msg 1', :headers => {'h1'=>'a'})
|
31
|
+
header_exch.publish('Headers test msg 2', :headers => {'h1'=>'z'})
|
32
|
+
|
33
|
+
|
34
|
+
# get messages from the queue - should only be msg 1 that got through
|
35
|
+
msg = ""
|
36
|
+
until msg == :queue_empty do
|
37
|
+
msg = q.pop[:payload]
|
38
|
+
puts 'This is a message from the header_q1 queue: ' + msg + "\n" unless msg == :queue_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
# close the client connection
|
42
|
+
b.stop
|