bunny 0.4.4 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/Rakefile +21 -3
  2. data/bunny.gemspec +34 -20
  3. data/examples/{simple.rb → simple_08.rb} +0 -0
  4. data/examples/simple_09.rb +30 -0
  5. data/examples/{simple_ack.rb → simple_ack_08.rb} +0 -0
  6. data/examples/simple_ack_09.rb +33 -0
  7. data/examples/{simple_consumer.rb → simple_consumer_08.rb} +0 -0
  8. data/examples/simple_consumer_09.rb +55 -0
  9. data/examples/{simple_fanout.rb → simple_fanout_08.rb} +0 -0
  10. data/examples/simple_fanout_09.rb +39 -0
  11. data/examples/{simple_headers.rb → simple_headers_08.rb} +0 -0
  12. data/examples/simple_headers_09.rb +40 -0
  13. data/examples/{simple_publisher.rb → simple_publisher_08.rb} +0 -0
  14. data/examples/simple_publisher_09.rb +27 -0
  15. data/examples/{simple_topic.rb → simple_topic_08.rb} +0 -0
  16. data/examples/simple_topic_09.rb +59 -0
  17. data/lib/bunny.rb +19 -14
  18. data/lib/bunny/channel08.rb +38 -0
  19. data/lib/bunny/channel09.rb +38 -0
  20. data/lib/bunny/client08.rb +167 -74
  21. data/lib/bunny/{client091.rb → client09.rb} +195 -92
  22. data/lib/bunny/{exchange091.rb → exchange09.rb} +12 -11
  23. data/lib/bunny/queue08.rb +1 -0
  24. data/lib/bunny/{queue091.rb → queue09.rb} +38 -28
  25. data/lib/qrack/client.rb +7 -0
  26. data/lib/qrack/protocol/{protocol.rb → protocol08.rb} +1 -4
  27. data/lib/qrack/protocol/protocol09.rb +133 -0
  28. data/lib/qrack/protocol/{spec091.rb → spec09.rb} +5 -5
  29. data/lib/qrack/qrack08.rb +2 -10
  30. data/lib/qrack/qrack09.rb +20 -0
  31. data/lib/qrack/transport/{buffer.rb → buffer08.rb} +0 -0
  32. data/lib/qrack/transport/buffer09.rb +276 -0
  33. data/lib/qrack/transport/{frame091.rb → frame09.rb} +8 -8
  34. data/spec/{bunny_spec.rb → spec_08/bunny_spec.rb} +4 -4
  35. data/spec/{exchange_spec.rb → spec_08/exchange_spec.rb} +3 -10
  36. data/spec/{queue_spec.rb → spec_08/queue_spec.rb} +1 -1
  37. data/spec/spec_09/bunny_spec.rb +40 -0
  38. data/spec/spec_09/exchange_spec.rb +131 -0
  39. data/spec/spec_09/queue_spec.rb +106 -0
  40. metadata +38 -22
  41. data/lib/qrack/qrack091.rb +0 -28
data/Rakefile CHANGED
@@ -1,6 +1,24 @@
1
- task :spec do
1
+ desc "Run AMQP 0-8 rspec tests"
2
+ task :spec08 do
2
3
  require 'spec/rake/spectask'
3
- Spec::Rake::SpecTask.new do |t|
4
+ puts "===== Running 0-8 tests ====="
5
+ Spec::Rake::SpecTask.new("spec08") do |t|
6
+ t.spec_files = FileList["spec/spec_08/*_spec.rb"]
4
7
  t.spec_opts = ['--color']
5
8
  end
6
- end
9
+ end
10
+
11
+ desc "Run AMQP 0-9 rspec tests"
12
+ task :spec09 do
13
+ require 'spec/rake/spectask'
14
+ puts "===== Running 0-9 tests ====="
15
+ Spec::Rake::SpecTask.new("spec09") do |t|
16
+ t.spec_files = FileList["spec/spec_09/*_spec.rb"]
17
+ t.spec_opts = ['--color']
18
+ end
19
+ end
20
+
21
+ task :default => [ :spec08 ]
22
+
23
+ desc "Run all rspec tests"
24
+ task :all => [:spec08, :spec09]
data/bunny.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{bunny}
3
- s.version = "0.4.4"
3
+ s.version = "0.5.1"
4
4
  s.authors = ["Chris Duncan"]
5
- s.date = %q{2009-06-19}
5
+ s.date = %q{2009-08-08}
6
6
  s.description = %q{Another synchronous Ruby AMQP client}
7
7
  s.email = %q{celldee@gmail.com}
8
8
  s.rubyforge_project = %q{bunny-amqp}
@@ -15,30 +15,44 @@ Gem::Specification.new do |s|
15
15
  "README",
16
16
  "Rakefile",
17
17
  "bunny.gemspec",
18
- "examples/simple.rb",
19
- "examples/simple_ack.rb",
20
- "examples/simple_consumer.rb",
21
- "examples/simple_fanout.rb",
22
- "examples/simple_publisher.rb",
23
- "examples/simple_topic.rb",
24
- "examples/simple_headers.rb",
18
+ "examples/simple_08.rb",
19
+ "examples/simple_ack_08.rb",
20
+ "examples/simple_consumer_08.rb",
21
+ "examples/simple_fanout_08.rb",
22
+ "examples/simple_publisher_08.rb",
23
+ "examples/simple_topic_08.rb",
24
+ "examples/simple_headers_08.rb",
25
+ "examples/simple_09.rb",
26
+ "examples/simple_ack_09.rb",
27
+ "examples/simple_consumer_09.rb",
28
+ "examples/simple_fanout_09.rb",
29
+ "examples/simple_publisher_09.rb",
30
+ "examples/simple_topic_09.rb",
31
+ "examples/simple_headers_09.rb",
25
32
  "lib/bunny.rb",
33
+ "lib/bunny/channel08.rb",
34
+ "lib/bunny/channel09.rb",
26
35
  "lib/bunny/client08.rb",
27
- "lib/bunny/client091.rb",
36
+ "lib/bunny/client09.rb",
28
37
  "lib/bunny/exchange08.rb",
29
- "lib/bunny/exchange091.rb",
38
+ "lib/bunny/exchange09.rb",
30
39
  "lib/bunny/queue08.rb",
31
- "lib/bunny/queue091.rb",
40
+ "lib/bunny/queue09.rb",
32
41
  "lib/qrack/client.rb",
33
- "lib/qrack/protocol/protocol.rb",
42
+ "lib/qrack/protocol/protocol08.rb",
43
+ "lib/qrack/protocol/protocol09.rb",
34
44
  "lib/qrack/protocol/spec08.rb",
35
- "lib/qrack/protocol/spec091.rb",
45
+ "lib/qrack/protocol/spec09.rb",
36
46
  "lib/qrack/qrack08.rb",
37
- "lib/qrack/qrack091.rb",
38
- "lib/qrack/transport/buffer.rb",
47
+ "lib/qrack/qrack09.rb",
48
+ "lib/qrack/transport/buffer08.rb",
49
+ "lib/qrack/transport/buffer09.rb",
39
50
  "lib/qrack/transport/frame08.rb",
40
- "lib/qrack/transport/frame091.rb",
41
- "spec/bunny_spec.rb",
42
- "spec/exchange_spec.rb",
43
- "spec/queue_spec.rb"]
51
+ "lib/qrack/transport/frame09.rb",
52
+ "spec/spec_08/bunny_spec.rb",
53
+ "spec/spec_08/exchange_spec.rb",
54
+ "spec/spec_08/queue_spec.rb",
55
+ "spec/spec_09/bunny_spec.rb",
56
+ "spec/spec_09/exchange_spec.rb",
57
+ "spec/spec_09/queue_spec.rb"]
44
58
  end
File without changes
@@ -0,0 +1,30 @@
1
+ # simple.rb
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
9
+ $:.unshift File.dirname(__FILE__) + '/../lib'
10
+
11
+ require 'bunny'
12
+
13
+ b = Bunny.new(:logging => true, :spec => '09')
14
+
15
+ # start a communication session with the amqp server
16
+ b.start
17
+
18
+ # declare a queue
19
+ q = b.queue('test1')
20
+
21
+ # publish a message to the queue
22
+ q.publish('Hello everybody!')
23
+
24
+ # get message from the queue
25
+ msg = q.pop
26
+
27
+ puts 'This is the message: ' + msg + "\n\n"
28
+
29
+ # close the client connection
30
+ b.stop
File without changes
@@ -0,0 +1,33 @@
1
+ # simple_ack.rb
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
9
+ $:.unshift File.dirname(__FILE__) + '/../lib'
10
+
11
+ require 'bunny'
12
+
13
+ b = Bunny.new(:logging => true, :spec => '09')
14
+
15
+ # start a communication session with the amqp server
16
+ b.start
17
+
18
+ # declare a queue
19
+ q = b.queue('test1')
20
+
21
+ # publish a message to the queue
22
+ q.publish('Testing acknowledgements')
23
+
24
+ # get message from the queue
25
+ msg = q.pop(:ack => true)
26
+
27
+ # acknowledge receipt of message
28
+ q.ack
29
+
30
+ puts 'This is the message: ' + msg + "\n\n"
31
+
32
+ # close the client connection
33
+ b.stop
@@ -0,0 +1,55 @@
1
+ # consumer.rb
2
+
3
+ # N.B. To be used in conjunction with simple_publisher.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
+ # How this example works
12
+ #=======================
13
+ #
14
+ # Open up two console windows start this program in one of them by typing -
15
+ #
16
+ # ruby simple_consumer.rb
17
+ #
18
+ # Then switch to the other console window and type -
19
+ #
20
+ # ruby simple_publisher.rb
21
+ #
22
+ # A message will be printed out by the simple_consumer and it will wait for the next message
23
+ #
24
+ # Run simple_publisher 3 more times. After the last run simple_consumer will stop.
25
+
26
+ $:.unshift File.dirname(__FILE__) + '/../lib'
27
+
28
+ require 'bunny'
29
+
30
+ b = Bunny.new(:logging => true, :spec => '09')
31
+
32
+ # start a communication session with the amqp server
33
+ b.start
34
+
35
+ # create/get queue
36
+ q = b.queue('po_box')
37
+
38
+ # create/get exchange
39
+ exch = b.exchange('sorting_room')
40
+
41
+ # bind queue to exchange
42
+ q.bind(exch, :key => 'fred')
43
+
44
+ # initialize counter
45
+ i = 1
46
+
47
+ # subscribe to queue
48
+ q.subscribe(:consumer_tag => 'testtag1') do |msg|
49
+ puts i.to_s + ': ' + msg
50
+ i+=1
51
+ q.unsubscribe(:consumer_tag => 'testtag1') if i == 5
52
+ end
53
+
54
+ # close the connection
55
+ b.stop
File without changes
@@ -0,0 +1,39 @@
1
+ # fanout.rb
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
9
+ $:.unshift File.dirname(__FILE__) + '/../lib'
10
+
11
+ require 'bunny'
12
+
13
+ b = Bunny.new(:logging => true, :spec => '09')
14
+
15
+ # start a communication session with the amqp server
16
+ b.start
17
+
18
+ # declare queues
19
+ q1 = b.queue('test_fan1')
20
+ q2 = b.queue('test_fan2')
21
+
22
+ # create a fanout exchange
23
+ exch = b.exchange('test_fan', :type => :fanout)
24
+
25
+ # bind the queues to the exchange
26
+ q1.bind(exch)
27
+ q2.bind(exch)
28
+
29
+ # publish a message to the exchange
30
+ exch.publish('This message will be fanned out')
31
+
32
+ # get message from the queues
33
+ msg = q1.pop
34
+ puts 'This is the message from q1: ' + msg + "\n\n"
35
+ msg = q2.pop
36
+ puts 'This is the message from q2: ' + msg + "\n\n"
37
+
38
+ # close the client connection
39
+ b.stop
@@ -0,0 +1,40 @@
1
+ # simple_headers.rb
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
9
+ $:.unshift File.dirname(__FILE__) + '/../lib'
10
+
11
+ require 'bunny'
12
+
13
+ b = Bunny.new(:spec => '09')
14
+
15
+ # start a communication session with the amqp server
16
+ b.start
17
+
18
+ # declare queues
19
+ q = b.queue('header_q1')
20
+
21
+ # create a headers exchange
22
+ header_exch = b.exchange('header_exch', :type => :headers)
23
+
24
+ # bind the queue to the exchange
25
+ q.bind(header_exch, :arguments => {'h1'=>'a','x-match'=>'all'})
26
+
27
+ # publish messages to the exchange
28
+ header_exch.publish('Headers test msg 1', :headers => {'h1'=>'a'})
29
+ header_exch.publish('Headers test msg 2', :headers => {'h1'=>'z'})
30
+
31
+
32
+ # get messages from the queue - should only be msg 1 that got through
33
+ msg = ""
34
+ until msg == :queue_empty do
35
+ msg = q.pop
36
+ puts 'This is a message from the header_q1 queue: ' + msg + "\n" unless msg == :queue_empty
37
+ end
38
+
39
+ # close the client connection
40
+ b.stop
@@ -0,0 +1,27 @@
1
+ # simple_publisher.rb
2
+
3
+ # N.B. To be used in conjunction with simple_consumer.rb. See simple_consumer.rb for explanation.
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
+ # create/get exchange
21
+ exch = b.exchange('sorting_room')
22
+
23
+ # publish message to exchange
24
+ exch.publish('This is a message from the publisher', :key => 'fred')
25
+
26
+ # message should now be picked up by the consumer so we can stop
27
+ b.stop
File without changes
@@ -0,0 +1,59 @@
1
+ # simple_topic.rb
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
9
+ $:.unshift File.dirname(__FILE__) + '/../lib'
10
+
11
+ require 'bunny'
12
+
13
+ b = Bunny.new(:spec => '09')
14
+
15
+ # start a communication session with the amqp server
16
+ b.start
17
+
18
+ # declare queues
19
+ soccer = b.queue('topic_soccer')
20
+ cricket = b.queue('topic_cricket')
21
+ rugby = b.queue('topic_rugby')
22
+ allsport = b.queue('topic_allsport')
23
+
24
+ # create a topic exchange
25
+ sports_results = b.exchange('sports_results', :type => :topic)
26
+
27
+ # bind the queues to the exchange
28
+ soccer.bind(sports_results, :key => 'soccer.*')
29
+ cricket.bind(sports_results, :key => 'cricket.*')
30
+ rugby.bind(sports_results, :key => 'rugby.*')
31
+ allsport.bind(sports_results, :key => '*.result')
32
+
33
+ # publish messages to the exchange
34
+ sports_results.publish('Manchester United 1 : Hull City 4', :key => 'soccer.result')
35
+ sports_results.publish('England beat Australia by 5 wickets in first test', :key => 'cricket.result')
36
+ sports_results.publish('British Lions 15 : South Africa 12', :key => 'rugby.result')
37
+
38
+ # get message from the queues
39
+
40
+ # soccer queue got the soccer message
41
+ msg = soccer.pop
42
+ puts 'This is a message from the soccer q: ' + msg + "\n\n"
43
+
44
+ # cricket queue got the cricket message
45
+ msg = cricket.pop
46
+ puts 'This is a message from the cricket q: ' + msg + "\n\n"
47
+
48
+ # rugby queue got the rugby message
49
+ msg = rugby.pop
50
+ puts 'This is a message from the rugby q: ' + msg + "\n\n"
51
+
52
+ # allsport queue got all of the messages
53
+ until msg == :queue_empty do
54
+ msg = allsport.pop
55
+ puts 'This is a message from the allsport q: ' + msg + "\n\n" unless msg == :queue_empty
56
+ end
57
+
58
+ # close the client connection
59
+ b.stop
data/lib/bunny.rb CHANGED
@@ -12,7 +12,7 @@ module Bunny
12
12
  class ConnectionError < StandardError; end
13
13
  class MessageError < StandardError; end
14
14
 
15
- VERSION = '0.4.4'
15
+ VERSION = '0.5.1'
16
16
 
17
17
  # Returns the Bunny version number
18
18
 
@@ -25,9 +25,10 @@ module Bunny
25
25
  def self.new(opts = {})
26
26
  # Set up Bunny according to AMQP spec version required
27
27
  spec_version = opts[:spec] || '08'
28
- setup(spec_version)
28
+ setup(spec_version, opts)
29
29
 
30
- Bunny::Client.new(opts)
30
+ # Return client
31
+ @client
31
32
  end
32
33
 
33
34
  # Runs a code block using a short-lived connection
@@ -37,14 +38,13 @@ module Bunny
37
38
 
38
39
  # Set up Bunny according to AMQP spec version required
39
40
  spec_version = opts[:spec] || '08'
40
- setup(spec_version)
41
+ setup(spec_version, opts)
41
42
 
42
- client = Bunny::Client.new(opts)
43
- client.start
43
+ @client.start
44
44
 
45
- block.call(client)
45
+ block.call(@client)
46
46
 
47
- client.stop
47
+ @client.stop
48
48
 
49
49
  # Return success
50
50
  :run_ok
@@ -52,20 +52,25 @@ module Bunny
52
52
 
53
53
  private
54
54
 
55
- def self.setup(version)
56
-
55
+ def self.setup(version, opts)
57
56
  if version == '08'
58
57
  # AMQP 0-8 specification
59
58
  require 'qrack/qrack08'
60
59
  require 'bunny/client08'
61
60
  require 'bunny/exchange08'
62
61
  require 'bunny/queue08'
62
+ require 'bunny/channel08'
63
+
64
+ @client = Bunny::Client.new(opts)
63
65
  else
64
66
  # AMQP 0-9-1 specification
65
- require 'qrack/qrack091'
66
- require 'bunny/client091'
67
- require 'bunny/exchange091'
68
- require 'bunny/queue091'
67
+ require 'qrack/qrack09'
68
+ require 'bunny/client09'
69
+ require 'bunny/exchange09'
70
+ require 'bunny/queue09'
71
+ require 'bunny/channel09'
72
+
73
+ @client = Bunny::Client09.new(opts)
69
74
  end
70
75
 
71
76
  include Qrack