Sha256: 2649688b63dad49652847abe1d1ddbbfb8978009c05326736ad6f870423fac91

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 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,
        :port                          => ::ActivePublisher.configuration.port,
        :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

8 entries across 8 versions & 1 rubygems

Version Path
active_publisher-0.1.2-java lib/active_publisher/connection.rb
active_publisher-0.1.3-java lib/active_publisher/connection.rb
active_publisher-0.1.3 lib/active_publisher/connection.rb
active_publisher-0.1.2 lib/active_publisher/connection.rb
active_publisher-0.1.1 lib/active_publisher/connection.rb
active_publisher-0.1.1-java lib/active_publisher/connection.rb
active_publisher-0.1.0-java lib/active_publisher/connection.rb
active_publisher-0.1.0 lib/active_publisher/connection.rb