Sha256: 470b4d5e176ff8cfa7a0e293e2ab21f2042655e3c51ae05df3850ced2fcf17bf

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

$:.unshift File.dirname(__FILE__) + '/../../lib'
require 'mq'

EM.run{

  def log *args
    p [ Time.now, *args ]
  end

  def publish_stock_prices
    mq = MQ.new
    EM.add_periodic_timer(1){
      puts

      { :appl => 170+rand(1000)/100.0,
        :msft => 22+rand(500)/100.0
      }.each do |stock, price|
        stock = "usd.#{stock}"

        log :publishing, stock, price
        mq.topic('stocks').publish(price, :key => stock)
      end
    }
  end

  def watch_appl_stock
    mq = MQ.new
    mq.queue('apple stock').bind(mq.topic('stocks'), :key => 'usd.appl').subscribe{ |price|
      log 'apple stock', price
    }
  end

  def watch_us_stocks
    mq = MQ.new
    mq.queue('us stocks').bind(mq.topic('stocks'), :key => 'usd.*').subscribe{ |info, price|
      log 'us stock', info.routing_key, price
    }
  end

  publish_stock_prices
  watch_appl_stock
  watch_us_stocks

}

__END__

[Fri Aug 15 01:39:00 -0700 2008, :publishing, "usd.appl", 173.45]
[Fri Aug 15 01:39:00 -0700 2008, :publishing, "usd.msft", 26.98]
[Fri Aug 15 01:39:00 -0700 2008, "apple stock", "173.45"]
[Fri Aug 15 01:39:00 -0700 2008, "us stock", "usd.appl", "173.45"]
[Fri Aug 15 01:39:00 -0700 2008, "us stock", "usd.msft", "26.98"]

[Fri Aug 15 01:39:01 -0700 2008, :publishing, "usd.appl", 179.72]
[Fri Aug 15 01:39:01 -0700 2008, :publishing, "usd.msft", 26.56]
[Fri Aug 15 01:39:01 -0700 2008, "apple stock", "179.72"]
[Fri Aug 15 01:39:01 -0700 2008, "us stock", "usd.appl", "179.72"]
[Fri Aug 15 01:39:01 -0700 2008, "us stock", "usd.msft", "26.56"]

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
tmm1-amqp-0.5.9 examples/mq/stocks.rb
tmm1-amqp-0.6.0 examples/mq/stocks.rb
amqp-0.5.9 examples/mq/stocks.rb
amqp-0.6.0 examples/mq/stocks.rb