Class: TorqueBox::Messaging::ConnectionFactory
- Inherits:
-
Object
- Object
- TorqueBox::Messaging::ConnectionFactory
- Defined in:
- lib/torquebox/messaging/connection_factory.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#internal_connection_factory ⇒ Object
readonly
Returns the value of attribute internal_connection_factory.
Class Method Summary collapse
Instance Method Summary collapse
- #activate(connection, client_id) ⇒ Object
- #connections ⇒ Object
- #create_connection(options = {}) ⇒ Object
- #create_connection_factory(host, port) ⇒ Object
- #create_internal_connection_factory(options) ⇒ Object
- #create_xa_connection(options = {}) ⇒ Object
- #current ⇒ Object
- #deactivate ⇒ Object
-
#initialize(internal_connection_factory = nil) ⇒ ConnectionFactory
constructor
A new instance of ConnectionFactory.
- #to_s ⇒ Object
- #transaction ⇒ Object
- #with_new_connection(options, enlist_tx = true, &block) ⇒ Object
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_factory ⇒ Object (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 |
#connections ⇒ Object
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(={}) username = [:username] password = [: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() host = [:host] || "localhost" port = [: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(={}) username = [:username] password = [:password] XaConnection.new( @internal_connection_factory.create_xa_connection( username, password ), self ) end |
#current ⇒ Object
62 63 64 |
# File 'lib/torquebox/messaging/connection_factory.rb', line 62 def current connections.last end |
#deactivate ⇒ Object
72 73 74 |
# File 'lib/torquebox/messaging/connection_factory.rb', line 72 def deactivate connections.pop.close end |
#to_s ⇒ Object
109 110 111 |
# File 'lib/torquebox/messaging/connection_factory.rb', line 109 def to_s "[ConnectionFactory: internal_connection_factory=#{internal_connection_factory}]" end |
#transaction ⇒ Object
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(, enlist_tx = true, &block) client_id = [:client_id] create_internal_connection_factory( ) if !enlist_tx || (current.nil? && !transaction) connection = create_connection( ) 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( ), client_id ) ) # XaSession's afterCompletion callback deactivates XA connections else result = block.call( current ) end result end |