Skip to content

Instantly share code, notes, and snippets.

@bbozo
Created November 21, 2013 10:13
Show Gist options
  • Save bbozo/7579163 to your computer and use it in GitHub Desktop.
Save bbozo/7579163 to your computer and use it in GitHub Desktop.
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
#!/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