Sha256: 2bf0b951581f974a4b7e5bdd7497c30989de1ae0fa15bee13a341857ba1c4439

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

require 'bunny'

module Untied
  module Publisher
    module Bunny
      class Producer < BaseProducer
        def initialize(opts={})
          super

          begin
            connection.start
          rescue ::Bunny::TCPConnectionFailed => e
            say "Can't connect to RabbitMQ: #{e.message}"
          end
        end

        # Publish the given event.
        #   event: object which is going to be serialized and sent through the
        #   wire. It should respond to #to_json.
        def safe_publish(event)
          if connection.status == :open
            exchange.publish(event.to_json, :routing_key => routing_key)
          else
            say "Event not sent. Connection status is #{connection.status}: " + \
                "#{event.to_json}"
          end
        end

        protected

        def connection
          @connection ||= ::Bunny.new
        end

        def exchange
          @exchange ||= channel.topic('untied', :auto_delete => true)
        end

        def channel
          @channel ||= connection.create_channel
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
untied-publisher-0.0.7.pre3 lib/untied-publisher/bunny/producer.rb
untied-publisher-0.0.7.pre2 lib/untied-publisher/bunny/producer.rb
untied-publisher-0.0.7.pre1 lib/untied-publisher/bunny/producer.rb
untied-publisher-0.0.7.pre lib/untied-publisher/bunny/producer.rb
untied-publisher-0.0.6 lib/untied-publisher/bunny/producer.rb