Sha256: 092837da6eabb4425cac8b068264b20b13be8a1977e9b39d77f216532c6fe365

Contents?: true

Size: 1.46 KB

Versions: 46

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8

$LOAD_PATH.unshift File.expand_path("../../../../lib", __FILE__)

require "amqp"
require "yaml"

t = Thread.new { EventMachine.run }
sleep(0.5)

connection = AMQP.connect
channel    = AMQP::Channel.new(connection)
exchange   = channel.fanout("amqpgem.patterns.events", :durable => true, :auto_delete => false)


EVENTS     = {
  "pages.show" => {
    :url      => "https://mysite.local/widgets/81772",
    :referrer => "http://www.google.com/search?client=safari&rls=en&q=widgets&ie=UTF-8&oe=UTF-8"
  },
  "widgets.created" => {
    :id       => 10,
    :shape    => "round",
    :owner_id => 1000
  },
  "widgets.destroyed" => {
    :id        => 10,
    :person_id => 1000
  },
  "files.created" => {
    :sha1      => "1a62429f47bc8b405d17e84b648f2fbebc555ee5",
    :filename  => "document.pdf"
  },
  "files.indexed" => {
    :sha1      => "1a62429f47bc8b405d17e84b648f2fbebc555ee5",
    :filename  => "document.pdf",
    :runtime   => 1.7623,
    :shared    => "shard02"
  }
}

def generate_event
  n       = (EVENTS.size * Kernel.rand).floor
  type    = EVENTS.keys[n]
  payload = EVENTS[type]

  [type, payload]
end

# broadcast events
EventMachine.add_periodic_timer(2.0) do
  event_type, payload = generate_event

  puts "Publishing a new event of type #{event_type}"
  exchange.publish(payload.to_yaml, :type => event_type)
end

puts "[boot] Ready. Will be publishing events every few seconds."
Signal.trap("INT") { connection.close { EventMachine.stop } }
t.join

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
amqp-1.8.0 examples/patterns/event/producer.rb
amqp-1.7.0 examples/patterns/event/producer.rb
amqp-1.6.0 examples/patterns/event/producer.rb
amqp-1.5.3 examples/patterns/event/producer.rb
amqp-1.5.2 examples/patterns/event/producer.rb
amqp-1.5.1 examples/patterns/event/producer.rb
amqp-1.5.0 examples/patterns/event/producer.rb
amqp-1.4.2 examples/patterns/event/producer.rb
amqp-1.4.1 examples/patterns/event/producer.rb
amqp-1.4.0 examples/patterns/event/producer.rb
amqp-1.3.0 examples/patterns/event/producer.rb
amqp-1.2.1 examples/patterns/event/producer.rb
amqp-1.2.0 examples/patterns/event/producer.rb
amqp-1.1.8 examples/patterns/event/producer.rb
amqp-1.1.7 examples/patterns/event/producer.rb
amqp-1.1.6 examples/patterns/event/producer.rb
amqp-1.1.5 examples/patterns/event/producer.rb
amqp-1.1.4 examples/patterns/event/producer.rb
amqp-1.1.3 examples/patterns/event/producer.rb
amqp-1.0.4 examples/patterns/event/producer.rb