Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop Messaging resources > Program to use asynchronous messaging


Program to use JMS and messaging directly

Your enterprise applications can use Java Message Service (JMS) programming interfaces directly to provide messaging services, and methods that implement business logic.

WAS supports asynchronous messaging as a method of communication based on JMS programming interfaces. Using JMS, enterprise applications can exchange messages asynchronously with other JMS clients by using JMS destinations (queues or topics). An enterprise application can explicitly poll for messages on a destination.

If you choose not to use JNDI to obtain configuration information for your messaging provider, for example for connection factories or destinations, you can instead use an API provided by your messaging provider to specify that configuration information programmatically.

If to transmit messages between JMS applications and traditional WebSphere MQ applications, consider how the JMS message structure is mapped onto a WebSphere MQ message . This includes scenarios where to use WebSphere MQ to manipulate messages transmitted between two JMS applications; for example, by using WebSphere MQ as a message broker.

By default, JMS messages held on WebSphere MQ queues use an MQRFH2 header to hold some of the JMS message header information. Many traditional WebSphere MQ applications cannot process messages with these headers, and require their own characteristic headers, for example the MQWIH for WebSphere MQ Workflow applications. For more information about how the JMS message structure is mapped onto a WebSphere MQ message, see the section Map JMS messages in the WebSphere MQ information center.


Procedure


Example

This following example shows how to programmatically configure a resource for the default messaging provider.

In this example, a JMS connection to a service integration bus is created by using the API in the com.ibm.websphere.sib package. This is an alternative to using JNDI to look up administratively configured connection factories. After the connection is established, the sample program reads lines of input from the console and sends them as JMS text messages to the specified destination.

This example can be run as a thin client application, or as a stand-alone client application.

/*
 * Sample program
 * © COPYRIGHT International Business Machines Corp. 2009
 * All Rights Reserved * Licensed Materials - Property of IBM  *
 * This sample program is provided AS IS and may be used, executed,  * copied and modified without royalty payment by customer
 *
 * (a) for its own instruction and study,  * (b) in order to develop applications designed to run with an IBM  *     WebSphere product for the customer's own internal use.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.websphere.sib.api.jms.JmsConnectionFactory;
import com.ibm.websphere.sib.api.jms.JmsFactoryFactory;
import com.ibm.websphere.sib.api.jms.JmsQueue;
import com.ibm.websphere.sib.api.jms.JmsTopic;

/**
 * Sample code to programmatically create a connection to a bus and
  *  send a text message.
 *
 * Example command lines:
 *   SIBusSender topic://my/topic?topicSpace=Default.Topic.Space MyBus localhost:7276
 *   SIBusSender queue://myQueue MyBus localhost:7286:BootstrapSecureMessaging InboundSecureMessaging
 */
public class SIBusSender {

  /**
   * @param args DEST_URL,BUS_NAME,PROVIDER_ENDPOINTS,[TRANSPORT_CHAIN]
   */
  public static void main(String[] args) throws JMSException, IOException {

    // Parse the arguments
    if (args.length
< 3) {
      throw new IllegalArgumentException(
          "Usage: SIBusSender
<DEST_URL>
<BUS_NAME>
<PROVIDER_ENDPOINTS> [TARGET_TRANSPORT_CHAIN]");
    }
    String destUrl = args[0];
    String busName = args[1];
    String providerEndpoints = args[2];
    String targetTransportChain = "InboundBasicMessaging";
    if (args.length >= 4) targetTransportChain = args[3];

    // Obtain the factory factory
    JmsFactoryFactory jmsFact = JmsFactoryFactory.getInstance();

    // Create a JMS destination
    Destination dest;
    if (destUrl.startsWith("topic://")) {
      JmsTopic topic = jmsFact.createTopic(destUrl);

     // Setter methods could be called here to configure the topic
      dest = topic ;
    }
    else {
      JmsQueue queue = jmsFact.createQueue(destUrl);
      // Setter methods could be called here to configure the queue
      dest = queue;
    }
 
    // Create a unified JMS connection factory
    JmsConnectionFactory connFact = jmsFact.createConnectionFactory();

    // Configure the connection factory
    connFact.setBusName(busName);
    connFact.setProviderEndpoints(providerEndpoints);
    connFact.setTargetTransportChain(targetTransportChain);

    // Create the connection
    Connection conn = connFact.createConnection();

    Session session = null;
    MessageProducer producer = null;
    try {


     // Create a session
      session = conn.createSession(false, // Not transactional
                                   Session.AUTO_ACKNOWLEDGE);

      // Create a message producer
      producer = session.createProducer(dest);

      // Loop reading lines of text from the console to send
      .println("Ready to send to " + dest + " on bus " + busName);
      BufferedReader lineInput = new BufferedReader(new InputStreamReader(System.in));
      String line = lineInput.readLine();
      while (line != null && line.length() > 0) {
 
        // Create a text message containing the line
TextMessage message = session.createTextMessage();
        message.setText(line);
 
        // Send the message
producer.send(message,

     Message.DEFAULT_DELIVERY_MODE,

     Message.DEFAULT_PRIORITY,

     Message.DEFAULT_TIME_TO_LIVE); 
 
        // Read the next line
line = lineInput.readLine();
      }

    }
    // Finally block to ensure we close our JMS objects
    finally {

      // Close the message producer
      try {
        if (producer != null) producer.close();
      }
      catch (JMSException e) {
        System.err.println("Failed to close message producer: " + e);
      }


     // Close the session
      try {
        if (session != null) session.close();
      }
      catch (JMSException e) {
        System.err.println("Failed to close session: " + e);
      }

      // Close the connection
      try {
        conn.close();
      }
      catch (JMSException e) {
        System.err.println("Failed to close connection: " + e);
      }

    }
  }

}


Related


Design an enterprise application to use JMS
Develop an enterprise application to use JMS
Develop a JMS client
JMS interfaces
Deploy an enterprise application to use JMS
Program to use asynchronous messaging


Related


Additional Application Programming Interfaces (APIs)
WebSphere MQ library
JMS documentation at java.sun.com

+

Search Tips   |   Advanced Search