Obtain or create JMS objects

 

The next step is to obtain or create a number of JMS objects:

  1. Obtain a TopicConnectionFactory

  2. Create a TopicConnection

  3. Create a TopicSession

  4. Obtain a Topic from JNDI

  5. Create TopicPublishers and TopicSubscribers

Many of these processes are similar to those that are used for point-to-point, as shown in the following:

Obtain a TopicConnectionFactory

The preferred way to do this is to use JNDI lookup, to maintain portability of the application code. The following code initializes a JNDI context:
String icf = "com.sun.jndi.ldap.LdapCtxFactory";  // initial context factory
String url = "ldap://server.company.com/o=company_us,c=us";  // url

// Initialise JNDI properties
Java.util.Hashtable env = new Hashtable();
env.put( Context.INITIAL_CONTEXT_FACTORY, icf );
env.put( Context.PROVIDER_URL, url );
env.put( Context.REFERRAL, "throw" );

Context ctx = null;
try {
      System.out.print( "Initialising JNDI... " );
      ctx = new InitialDirContext( env );
      System.out.println( "Done!" );
} catch ( NamingException nx ) {
      System.out.println( "ERROR: " + nx );
      System.exit(-1);
}

Change the icf and url variables to suit your installation and your JNDI service provider.

The properties required by JNDI initialization are in a Hashtable, which is passed to the InitialDirContext constructor. If this connection fails, an exception is thrown to indicate that the administered objects required later in the application are not available.

Obtain a TopicConnectionFactory using a lookup key that the administrator has defined:

// LOOKUP TCF
try {
    System.out.print( "Obtaining TCF from JNDI... " );
    fact = (TopicConnectionFactory)ctx.lookup( tcfLookup );
    System.out.println( "Done!" );
} catch ( NamingException nx ) {
    System.out.println( "ERROR: " + nx );
    System.exit(-1);
}

If a JNDI namespace is not available, we can create a TopicConnectionFactory at runtime. You create a new com.ibm.mq.jms.MQTopicConnectionFactory as described in Creating factories at runtime.

Create a TopicConnection

This is created from the TopicConnectionFactory object. Connections are always initialized in a stop state and must be started with the following code:
// create connection
TopicConnection conn = fact.createTopicConnection();
//start connection
conn.start();

Create a TopicSession

This is created using the TopicConnection. This method takes two parameters: one to signify whether the session is transacted, and one to specify the acknowledgement mode:
TopicSession sess = conn.createTopicSession(false,
                                Session.AUTO_ACKNOWLEDGE);

Obtain a Topic
This object can be obtained from JNDI, for use with TopicPublishers and TopicSubscribers that are created later. The following code retrieves a Topic:
Topic topic = null;
try {
    System.out.print( "Obtaining topic T from JNDI... " );
    topic = (Topic)ctx.lookup( tLookup );
    System.out.println( "Done!" );
}
catch ( NamingException nx ) {
    System.out.println( "ERROR: " + nx );
    System.exit(-1);
}
If a JNDI namespace is not available, we can create a Topic at runtime, as described in Creating topics at runtime.

The following code creates a Topic at runtime:

// topic
Topic t = sess.createTopic("myTopic");

Create consumers and producers of publications

Depending on the nature of the JMS client application that you write, a subscriber, a publisher, or both must be created. Use the createPublisher and createSubscriber methods as follows:
// publisher
TopicPublisher pub = sess.createPublisher(t);
// subscriber
TopicSubscriber sub = sess.createSubscriber(t);
// publisher
TopicPublisher pubA = sess.createPublisher(topic);
// subscriber
TopicSubscriber subA = sess.createSubscriber(topic);


uj24680_