Class: TorqueBox::Messaging::ConnectionFactory

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

Direct Known Subclasses

XaConnectionFactory

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ConnectionFactory) initialize(internal_connection_factory = nil)

Returns a new instance of ConnectionFactory



32
33
34
35
# File 'messaging/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

- (Object) :internal_connection_factory (readonly)

Returns the value of attribute :internal_connection_factory



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

def :internal_connection_factory
  @:internal_connection_factory
end

Class Method Details

+ (Object) new(internal_connection_factory = nil)



27
28
29
30
# File 'messaging/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

- (Object) activate(connection, client_id)



66
67
68
69
70
# File 'messaging/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

- (Object) connections



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

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

- (Object) create_connection(options = {})



88
89
90
91
92
# File 'messaging/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

- (Object) create_connection_factory(host, port)



100
101
102
103
104
105
106
# File 'messaging/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

- (Object) create_internal_connection_factory(options)



80
81
82
83
84
85
86
# File 'messaging/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

- (Object) create_xa_connection(options = {})



94
95
96
97
98
# File 'messaging/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

- (Object) current



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

def current
  connections.last
end

- (Object) deactivate



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

def deactivate
  connections.pop.close
end

- (Object) to_s



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

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

- (Object) transaction



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

def transaction
  @tm && @tm.transaction
end

- (Object) with_new_connection(options, enlist_tx = true, &block)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'messaging/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