Sha256: 7bbfb78da873521a39fdfba5238c1922632c9cbb7857227aaf71dd1059bbf29e

Contents?: true

Size: 1.69 KB

Versions: 13

Compression:

Stored size: 1.69 KB

Contents

require 'jms'

# Handle Messaging and Queuing
module ModernTimes
  module JMS
    module Connection
      # Singleton-ize
      extend self

      # Initialize the messaging system and connection pool for this VM
      def init(config)
        @config = config
        @inited = true
        @log_times = config.delete(:log_times)
        # Default to true
        @log_times = true if @log_times.nil?

        # Let's not create a session_pool unless we're going to use it
        @session_pool_mutex = Mutex.new

        @connection = ::JMS::Connection.new(config)
        @connection.start

        at_exit do
          close
        end
      end

      def inited?
        @inited
      end

      def log_times?
        @log_times
      end

      # Create a session targeted for a consumer (producers should use the session_pool)
      def create_consumer_session
        connection.create_session(@config || {})
      end

      def session_pool
        # Don't use the mutex unless we have to!
        return @session_pool if @session_pool
        @session_pool_mutex.synchronize do
          # if it's been created in between the above call and now, return it
          return @session_pool if @session_pool
          return @session_pool = connection.create_session_pool(@config)
        end
      end

      def close
        return if @closed
        ModernTimes.logger.info "Closing #{self.name}"
        @session_pool.close if @session_pool
        if @connection
          @connection.stop
          @connection.close
        end
        @closed = true
      end

      def connection
        raise "#{self.name} never had it's init method called" unless @connection
        @connection
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
modern_times-0.3.12 lib/modern_times/jms/connection.rb
modern_times-0.3.11 lib/modern_times/jms/connection.rb
modern_times-0.3.10 lib/modern_times/jms/connection.rb
modern_times-0.3.9 lib/modern_times/jms/connection.rb
modern_times-0.3.8 lib/modern_times/jms/connection.rb
modern_times-0.3.7 lib/modern_times/jms/connection.rb
modern_times-0.3.6 lib/modern_times/jms/connection.rb
modern_times-0.3.5 lib/modern_times/jms/connection.rb
modern_times-0.3.4 lib/modern_times/jms/connection.rb
modern_times-0.3.3 lib/modern_times/jms/connection.rb
modern_times-0.3.2 lib/modern_times/jms/connection.rb
modern_times-0.3.1 lib/modern_times/jms/connection.rb
modern_times-0.3.0 lib/modern_times/jms/connection.rb