Sha256: c478d75a1ac2c240559385ff671cf00462a886475a2815a0178d0e503adc4aa6

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

require 'thread'

module ActivePublisher
  module Connection
    CONNECTION_MUTEX = ::Mutex.new
    NETWORK_RECOVERY_INTERVAL = 1.freeze

    def self.connected?
      connection.try(:connected?)
    end

    def self.connection
      CONNECTION_MUTEX.synchronize do
        return @connection if @connection
        @connection = create_connection
      end
    end

    def self.disconnect!
      CONNECTION_MUTEX.synchronize do
        if @connection && @connection.connected?
          @connection.close
        end

        @connection = nil
      end
    end

    # Private API
    def self.create_connection
      if ::RUBY_PLATFORM == "java"
        connection = ::MarchHare.connect(connection_options)
      else
        connection = ::Bunny.new(connection_options)
        connection.start
        connection
      end
    end
    private_class_method :create_connection

    def self.connection_options
      {
        :heartbeat                     => ::ActivePublisher.configuration.heartbeat,
        :hosts                         => ::ActivePublisher.configuration.hosts,
        :pass                          => ::ActivePublisher.configuration.password,
        :port                          => ::ActivePublisher.configuration.port,
        :user                          => ::ActivePublisher.configuration.username,
        :continuation_timeout          => ::ActivePublisher.configuration.timeout * 1_000.0, #convert sec to ms
        :automatically_recover         => true,
        :network_recovery_interval     => NETWORK_RECOVERY_INTERVAL,
        :recover_from_connection_close => true,
      }
    end
    private_class_method :connection_options
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_publisher-0.1.5-java lib/active_publisher/connection.rb
active_publisher-0.1.5 lib/active_publisher/connection.rb
active_publisher-0.1.4-java lib/active_publisher/connection.rb
active_publisher-0.1.4 lib/active_publisher/connection.rb