Obtaining Connection Factories
A Connection Factory, or an XA Connection Factory, can be obtained through one of the following ways:
- Programmatically Creating Connection Factories.
- Looking Up Connection Factories from Solace JNDI Stores
- Looking Up Connection Factories from External JNDI Stores
Programmatically Creating Connection Factories
The figure below shows the basic process for programmatically creating a new instance of a Connection Factory object (the same process is used to create an XA Connection Factory, but the method calls differ). Instead of looking up an existing Connection Factory from a JNDI store, the client provides the Connection Factory properties that are required to establish a data channel connection to an event broker in the compiled application.
Programmatically Creating a Connection Factory
To programmatically create a Solace proprietary implementation of a ConnectionFactory object, call SolJmsUtility.createConnectionFactory(...)
.
The IP address, and optional port, of the event broker to establish the JMS connection to can be set through the Host
Connection Factory property. This Host
value overrides the value that is implicitly set through the URL
property used for the JNDI connection.
The following code snippet shows how to programmatically create a Connection Factory.
try {
// Create the connection factory
SolConnectionFactory cf = SolJmsUtility.createConnectionFactory();
cf.setHost(“192.168.1.1”);
cf.setUsername(“user1”);
cf.setPassword(“mypassword”);
cf.setDirectTransport(false);
if (vpn != null) {
cf.setVPN(“myvpn”);
}
Related Samples
For a complete example of how to programmatically create a new Connection Factory object, refer to the SolJMSProgConsumer.java
sample.
Looking Up Connection Factories from Solace JNDI Stores
The figure below shows the process for connecting a JMS client when the Connection Factory used to create the JMS connection is looked up from the JNDI store on the event broker. The basic steps are as follows:
- Establish a JNDI connection (refer to Working with JNDI).
- A SolConnection Factory is obtained through a lookup to the JNDI store on the event broker. (To look up an SolXAConnection Factory, the same basic process is used, but the method calls differ.)
- Using the obtained SolConnection Factory, create a JMS connection.
The properties set for the JNDI connection are inherited by the subsequent JMS connection that is required. A client can use these same JMS properties for the JMS connection or choose to override them with new values when making the JMS connection.
Looking Up a Connection Factory from the Solace JNDI Store
The following code snippet shows a JNDI environment that is configured by passing in properties using a hash table; an InitialContext
is then created, and a lookup for a SolConnectionFactory
in the JNDI store that resides on the event broker is performed.
Hashtable<String,Object> environment = new Hashtable<String,Object>();
environment.put(InitialContext.INITIAL_CONTEXT_FACTORY, "com.solacesystems.jndi.SolJNDIInitialContextFactory");
environment.put(InitialContext.PROVIDER_URL, "smf://192.168.1.1:55555");
environment.put(Context.SECURITY_PRINCIPAL, "userName");
environment.put(Context.SECURITY_CREDENTIALS, "userPassword");
environment.put(SupportedProperty.SOLACE_JMS_VPN, "vpnName");
InitialContext initialContext = new InitialContext(environment);
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("connectionFactoryName");
Looking Up Connection Factories from External JNDI Stores
JMS clients can look up Solace Connection Factory objects that have been added to an external JNDI store on a remote host.
The figure below shows the process for connecting a JMS client when the Connection Factory used to create the JMS connection is looked up from a JNDI store that resides on an external JNDI server.
The steps are:
- Establish a JNDI connection (refer to Working with JNDI).
- Obtain a SolConnectionFactory through a lookup to the JNDI store on a configured external JNDI server. (To look up an SolXAConnectionFactory, the same basic process is used, but the method calls differ.)
- Using the obtained SolConnectionFactory, create a JMS connection.
The IP address, and optional port, of the event broker to establish the JMS connection to can be set through the
Host
Connection Factory property. ThisHost
value overrides the value that is implicitly set through theURL
property used for the JNDI connection.
Looking Up a Connection Factory From an External JNDI Store
The following code snippet shows a JNDI environment that is configured by passing in properties using a hash table; an InitialContext
is then created, and a lookup for a SolConnectionFactory
in the JNDI store that resides in the external store is performed.
Use the class name com.solacesystems.jms.SolXAConnectionFactoryImpl
and com.solacesystems.jms.SolConnectionFactoryImpl
for XA and non-XA (local) lookup respectively.
Although it's possible to create a non-XA (local) lookup by calling SolConnectionFactory cf = (SolConnectionFactory)ctx.lookup(CF_OBJECT_NAME);
even when the com.solacesystems.jms.SolXAConnectionFactoryImpl
class is set in JNDI. However, it's not recommended. For clarity, you should use the appropriate and distinct class names.
For connection factories, the connection factory type is solely determined by the configured javaClassName
, which can be either com.solacesystems.jms.SolConnectionFactoryImpl
or com.solacesystems.jms.SolXAConnectionFactoryImpl
and the XA property value has no effect.
// Create the Initial Context
// JNDI_PROVIDER_INITIAL_CONTEXT_FACTORY is the provider’s
// specific InitialContextFactory implementation’s class name
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_PROVIDER_INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, providerURL);
env.put(Context.REFERRAL, "throw");
env.put(Context.SECURITY_PRINCIPAL, providerUsername);
env.put(Context.SECURITY_CREDENTIALS, providerPassword);
ctx = new InitialContext(env);
// Lookup the connection factory by name
// assuming CF_OBJECT_NAME has been configured in provider JNDI
// Example for non-XA connection factory
SolConnectionFactory cf = (SolConnectionFactory)ctx.lookup(CF_OBJECT_NAME);
cf.setHost(routerIP);
cf.setUsername(username);
cf.setPassword(password);
cf.setVPN(vpn);
// Example for XA connection factory
SolXAConnectionFactory cf = (SolXAConnectionFactory)ctx.lookup(CF_OBJECT_NAME);
cf.setHost(routerIP);
cf.setUsername(username);
cf.setPassword(password);
cf.setVPN(vpn);
// Create JMS connection to the Solace router
Connection connection = cf.createConnection();
Related Samples
- For an example of how to add/bind Solace implementations of JMS Connection Factory, Factory, Queue, and Topic objects to an LDAP-based JNDI store, refer to the
SolJMSLDAPBind.java
sample. - For an example of how to look up JMS-administered objects from an LDAP‑based JNDI store, refer to the
SolJMSLDAPLookup.java
sample.