Class: TorqueBox::Messaging::ConnectionFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/torquebox/messaging/connection_factory.rb

Direct Known Subclasses

XaConnectionFactory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(internal_connection_factory = nil) ⇒ ConnectionFactory

Returns a new instance of ConnectionFactory.



32
33
34
35
# File 'lib/torquebox/messaging/connection_factory.rb', line 32

def initialize(internal_connection_factory = nil)
  @internal_connection_factory = internal_connection_factory
  @tm = TorqueBox.fetch('transaction-manager')
end

Instance Attribute Details

#internal_connection_factoryObject (readonly)

Returns the value of attribute internal_connection_factory.



25
26
27
# File 'lib/torquebox/messaging/connection_factory.rb', line 25

def internal_connection_factory
  @internal_connection_factory
end

Class Method Details

.new(internal_connection_factory = nil) ⇒ Object



27
28
29
30
# File 'lib/torquebox/messaging/connection_factory.rb', line 27

def self.new(internal_connection_factory = nil)
  return internal_connection_factory if internal_connection_factory.is_a?( ConnectionFactory )
  super
end

Instance Method Details

#activate(connection, client_id) ⇒ Object



66
67
68
69
70
# File 'lib/torquebox/messaging/connection_factory.rb', line 66

def activate(connection, client_id)
  connection.client_id = client_id if client_id
  connection.start
  connections.push(connection) && current
end

#connectionsObject



58
59
60
# File 'lib/torquebox/messaging/connection_factory.rb', line 58

def connections
  Thread.current[:torquebox_connection] ||= []
end

#create_connection(options = {}) ⇒ Object



88
89
90
91
92
# File 'lib/torquebox/messaging/connection_factory.rb', line 88

def create_connection(options={})
  username = options[:username]
  password = options[:password]
  Connection.new( @internal_connection_factory.create_connection( username, password ), self )
end

#create_connection_factory(host, port) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/torquebox/messaging/connection_factory.rb', line 100

def create_connection_factory(host, port)
  connect_opts = { "host" => host, "port" => port }
  transport_config =
    org.hornetq.api.core.TransportConfiguration.new("org.hornetq.core.remoting.impl.netty.NettyConnectorFactory",
                                                    connect_opts)
  org.hornetq.api.jms.HornetQJMSClient.createConnectionFactoryWithoutHA( org.hornetq.api.jms::JMSFactoryType::CF, transport_config )
end

#create_internal_connection_factory(options) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/torquebox/messaging/connection_factory.rb', line 80

def create_internal_connection_factory(options)
  host     = options[:host] || "localhost"
  port     = options[:port] || 5445
  if !@internal_connection_factory
    @internal_connection_factory = create_connection_factory( host, port )
  end
end

#create_xa_connection(options = {}) ⇒ Object



94
95
96
97
98
# File 'lib/torquebox/messaging/connection_factory.rb', line 94

def create_xa_connection(options={})
  username = options[:username]
  password = options[:password]
  XaConnection.new( @internal_connection_factory.create_xa_connection( username, password ), self )
end

#currentObject



62
63
64
# File 'lib/torquebox/messaging/connection_factory.rb', line 62

def current
  connections.last
end

#deactivateObject



72
73
74
# File 'lib/torquebox/messaging/connection_factory.rb', line 72

def deactivate
  connections.pop.close
end

#to_sObject



109
110
111
# File 'lib/torquebox/messaging/connection_factory.rb', line 109

def to_s
  "[ConnectionFactory: internal_connection_factory=#{internal_connection_factory}]"
end

#transactionObject



76
77
78
# File 'lib/torquebox/messaging/connection_factory.rb', line 76

def transaction
  @tm && @tm.transaction
end

#with_new_connection(options, enlist_tx = true, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/torquebox/messaging/connection_factory.rb', line 37

def with_new_connection(options, enlist_tx = true, &block)
  client_id = options[:client_id]
  create_internal_connection_factory( options )
  if !enlist_tx || (current.nil? && !transaction)
    connection = create_connection( options )
    connection.client_id = client_id if client_id
    begin
      connection.start
      result = block.call( connection )
    ensure
      connection.close
    end
  elsif transaction && (!current.respond_to?(:session_transaction) || current.session_transaction != transaction)
    result = block.call( activate( create_xa_connection( options ), client_id ) )
    # XaSession's afterCompletion callback deactivates XA connections
  else
    result = block.call( current )
  end
  result
end