Method: JMS::Connection#initialize
- Defined in:
- lib/jms/connection.rb
#initialize(params = {}) ⇒ Connection
Create a connection to the JMS provider
Note: Connection::start must be called before any consumers will be
able to receive
In JMS we need to start by obtaining the JMS Factory class that is supplied by the JMS Vendor.
There are 3 ways to establish a connection to a JMS Provider
1. Supply the name of the JMS Providers Factory Class
2. Supply an instance of the JMS Provider class itself
3. Use a JNDI lookup to return the JMS Provider Factory class
Parameters:
:factory => String: Name of JMS Provider Factory class
=> Class: JMS Provider Factory class itself
:jndi_name => String: Name of JNDI entry at which the Factory can be found
:jndi_context => Mandatory if jndi lookup is being used, contains details
on how to connect to JNDI server etc.
:factory and :jndi_name are mutually exclusive, both cannot be supplied at the same time. :factory takes precedence over :jndi_name
JMS Provider specific properties can be set if the JMS Factory itself has setters for those properties. Some known examples:
For HornetQ
:factory => 'org.hornetq.jms.client.HornetQConnectionFactory',
:discovery_address => '127.0.0.1',
:discovery_port => '5445',
:username => 'guest',
:password => 'guest'
For HornetQ using JNDI lookup technique
:jndi_name => '/ConnectionFactory',
:jndi_context => {
'java.naming.factory.initial' => 'org.jnp.interfaces.NamingContextFactory',
'java.naming.provider.url' => 'jnp://localhost:1099',
'java.naming.factory.url.pkgs' => 'org.jboss.naming:org.jnp.interfaces',
'java.naming.security.principal' => 'guest',
'java.naming.security.credentials' => 'guest'
}
On Java 6, HornetQ needs the following jar files on your CLASSPATH:
hornetq-core-client.jar
netty.jar
hornetq-jms-client.jar
jboss-jms-api.jar
jnp-client.jar
On Java 5, HornetQ needs the following jar files on your CLASSPATH:
hornetq-core-client-java5.jar
netty.jar
hornetq-jms-client-java5.jar
jboss-jms-api.jar
jnp-client.jar
For: WebSphere MQ
:factory => 'com.ibm.mq.jms.MQQueueConnectionFactory',
:queue_manager=>'REID',
:host_name=>'localhost',
:channel=>'MY.CLIENT.CHL',
:port=>1414,
:transport_type => com.ibm.mq.jms.JMSC::MQJMS_TP_CLIENT_MQ_TCPIP,
:username => 'mqm'
For: Active MQ
:factory => 'org.apache.activemq.ActiveMQConnectionFactory',
:broker_url => 'tcp://localhost:61616'
ActiveMQ requires the following jar files on your CLASSPATH
For Oracle AQ 9 Server
:factory => 'JMS::OracleAQConnectionFactory',
:url => 'jdbc:oracle:thin:@hostname:1521:instanceid',
:username => 'aquser',
:password => 'mypassword'
For JBoss, which uses JNDI lookup technique
:jndi_name => 'ConnectionFactory',
:jndi_context => {
'java.naming.factory.initial' => 'org.jnp.interfaces.NamingContextFactory',
'java.naming.provider.url' => 'jnp://localhost:1099'
'java.naming.security.principal' => 'user',
'java.naming.security.credentials' => 'pwd'
}
For Apache Qpid / Redhat Messaging, using Factory class directly
:factory: org.apache.qpid.client.AMQConnectionFactory
:broker_url: tcp://localhost:5672
For Apache Qpid / Redhat Messaging, via JNDI lookup
:jndi_name => 'local',
:jndi_context => {
'java.naming.factory.initial' => 'org.apache.qpid.jndi.PropertiesFileInitialContextFactory',
'connectionfactory.local' => "amqp://guest:guest@clientid/testpath?brokerlist='tcp://localhost:5672'"
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/jms/connection.rb', line 219 def initialize(params = {}) # Used by ::on_message @sessions = [] @consumers = [] # Load Jar files on demand so that they do not need to be in the CLASSPATH # of JRuby lib directory fetch_dependencies(params[:require_jars]) if params[:require_jars] connection_factory = nil factory = params[:factory] if factory # If factory is a string, then it is the name of a class, not the class itself factory = eval(factory) if factory.respond_to? :to_str connection_factory = factory.new elsif jndi_name = params[:jndi_name] raise "Missing mandatory parameter :jndi_context missing in call to Connection::connect" unless jndi_context = params[:jndi_context] jndi = javax.naming.InitialContext.new(java.util.Hashtable.new(jndi_context)) begin connection_factory = jndi.lookup jndi_name ensure jndi.close end else raise "Missing mandatory parameter :factory or :jndi_name missing in call to Connection::connect" end JMS::logger.debug "Using Factory: #{connection_factory.java_class}" if connection_factory.respond_to? :java_class params.each_pair do |key, val| method = key.to_s+'=' if connection_factory.respond_to? method connection_factory.send method, val JMS::logger.debug " #{key} = #{connection_factory.send key}" if connection_factory.respond_to? key.to_sym end end if params[:username] @jms_connection = connection_factory.create_connection(params[:username], params[:password]) else @jms_connection = connection_factory.create_connection end end |