Developing a simple application

This topic describes the steps required to create a MQTT JMS application that subscribes for data.

This sample application uses a JNDI repository to get the JMS administered objects and assumes that the administered object have already been created in the JNDI repository, as described in Registering JMS administered objects in JNDI.

Import the JMS interface classes plus the additional classes required to create the administered objects:
import javax.jms.Connection; 
import javax.jms.ConnectionFactory; 
import javax.jms.Destination; 
import javax.jms.MessageConsumer; 
import javax.jms.MessageProducer; 
import javax.jms.Session; 
import javax.jms.TextMessage;
Next, create a method to run the JMS application. The application will perform the following functions:
  1. Get a reference to the JMS connection factory from JNDI
  2. Get a reference to the JMS topic from JNDI
  3. Create a JMS topic destination
  4. Create a JMS destination consumer
  5. Start the JMS session
  6. Publish a message to the broker and commit the transaction
  7. Receive the message back from the broker and commit the transaction
  8. Close the connection to disconnect from the broker
public void runJMSApplication() throws Exception {
  1. Get a reference to the JMS connection factory from JNDI.
    InitialContext jndiContext = new InitialContext();
    ConnectionFactory connFactory = (ConnectionFactory)jndiContext.lookup
          ("jms/my_connection_factory");
  2. Get a reference to the JMS topic from JNDI.
    Destination topic = (Destination)jndiContext.lookup("jms/my_topic");
  3. Create the JMS connection and give the client an identifier. A client identifier must be set and it must not be the same as any other client which connects to the same broker:
    Connection conn = connFactory.createConnection(); 
    conn.setClientID("MQTT_JMS_CLIENT");
  4. Create the JMS transacted session:
    Session session = conn.createSession(true,  	
         session.AUTO_ACKNOWLEDGE);
  5. Create the JMS consumer and producer so that they use the same destination. This allows the application to receive messages it originally sent:
    MessageConsumer consumer = session.createConsumer(topic); 
    MessageProducer producer = session.createProducer(topic); 
  6. Now that all resources have been created, start the connection:
    conn.start();
  7. Create a Hello World text message and send it to the broker. Commit the session to cause the broker to release the message to all matching subscribers:
    TextMessage message = session. 
         createTextMessage("Hello World"); 
    producer.send(message); 
    session.commit();
  8. Receive the message back from the broker, specifying a five second timeout. Commit the session to confirm the message has been received:
    TextMessage receivedMessage =  
          (TextMessage)consumer.receive(5000); 
    session.commit();
    
    System.out.println("Message:"+receivedMessage.getText()); 
  9. Close the connection to disconnect the client from the broker and terminate the method:
    conn.close(); 
    }
Related information
Setting ConnectionFactory properties
ConnectionFactory properties
Accessing the administered objects without using JNDI


Library | Support | Terms of use |

Last updated: Tuesday, October 17, 2006
(C) Copyright IBM Corporation 2004, 2006. All Rights Reserved.
This information center is built on Eclipse. (http://www.eclipse.org)