Created
November 21, 2013 10:13
-
-
Save bbozo/7579163 to your computer and use it in GitHub Desktop.
Another example of https://github.com/eventmachine/eventmachine/issues/477
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bbozo@vilma:~/dev/0000_dallywagging/rabbitmq$ rvm use ruby | |
Using /home/bbozo/.rvm/gems/ruby-2.0.0-p247 | |
bbozo@vilma:~/dev/0000_dallywagging/rabbitmq$ ruby amqp_hello.rb | |
Connecting to RabbitMQ. Running 1.1.1 version of the gem... | |
amqpgem.examples.hello_world | |
================================================================================ | |
foo | |
roko | |
Received a message: Hello, world!. Disconnecting... | |
tick | |
tick | |
bbozo@vilma:~/dev/0000_dallywagging/rabbitmq$ rvm use jruby | |
Using /home/bbozo/.rvm/gems/jruby-1.7.5 | |
bbozo@vilma:~/dev/0000_dallywagging/rabbitmq$ ruby amqp_hello.rb | |
Connecting to RabbitMQ. Running 1.1.1 version of the gem... | |
amqpgem.examples.hello_world | |
================================================================================ | |
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require "amqp" | |
require "eventmachine" | |
require "pry" | |
unless EM.reactor_running? | |
Thread.abort_on_exception = true | |
Thread.new do | |
EM.run do | |
AMQP.channel ||= AMQP::Channel.new(AMQP.connect(:host => '127.0.0.1')) | |
end | |
end | |
end | |
until EM.reactor_running?; sleep 0.1; end | |
EM.next_tick{} | |
EM::add_periodic_timer 1 do | |
puts "tick" | |
end | |
connection = AMQP.connect(:host => '127.0.0.1') | |
puts "Connecting to RabbitMQ. Running #{AMQP::VERSION} version of the gem..." | |
ch = AMQP::Channel.new(connection) | |
q = ch.queue("amqpgem.examples.hello_world", :auto_delete => true) | |
x = ch.default_exchange | |
q.subscribe do |metadata, payload| | |
puts "Received a message: #{payload}. Disconnecting..." | |
end | |
puts q.name | |
x.publish "Hello, world!", :routing_key => q.name | |
module Messaging | |
class RabbitMqWrapper | |
attr_accessor :channel, :queue, :exchange, :options | |
def initialize(queue, options = {}) | |
@channel = AMQP.channel | |
@queue = channel.queue(queue, :auto_delete => true) | |
@exchange = channel.default_exchange | |
@options = {} | |
end | |
def self.start(*args, &block) | |
new(*args, &block) | |
end | |
def publish payload | |
puts queue.name | |
exchange.publish payload, :routing_key => queue.name | |
end | |
def subscribe &block | |
queue.subscribe &block | |
end | |
end | |
end | |
puts "="*80 | |
queue = Messaging::RabbitMqWrapper.new('foo') | |
queue.subscribe do |metadata, payload| | |
puts payload | |
end | |
queue.publish 'roko' | |
sleep 3 | |
EM.stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment