How to publish messages to EMS Topic using Java

Bala picture Bala · May 14, 2014 · Viewed 30.5k times · Source

I would like to publish a test message to EMS topic and could use some directions. So far I have managed to do this

import com.tibco.tibjms.TibjmsConnectionFactory;
import com.tibco.tibjms.TibjmsTopicConnectionFactory;

public class Connect {
    public static void main(String[] args) {

        String url = "tcp://host:6600";
        TibjmsConnectionFactory cf = new TibjmsTopicConnectionFactory(url);

        cf.setUserName("user1");
        cf.setUserPassword("");
        System.out.println(cf);
    }
}

which produces the below. How do I publish a message to topic "topic1" or queue "Q1"

TopicConnectionFactory[URL=tcp://localhost:6600;clientID=null;Properties={com.tibco.tibjms.factory.password=, com.tibco.tibjms.factory.username=user1}]

Answer

GhislainCote picture GhislainCote · May 14, 2014

I created the following code by modifying the "tibjmsMsgProducer.java" from my EMS 8.0 "sample" folder. Look at all the Java example in this folder for further references.

This code publish a simple hard-coded text message to a local EMS with default user and password. The target Topic is "topic1" (on the last line).

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

public class tibjmsMsgTopicProducer {

static String serverUrl = "localhost";
static String userName = "admin";
static String password = "admin";

public static void sendTopicMessage(String topicName, String messageStr) {

    Connection connection = null;
    Session session = null;
    MessageProducer msgProducer = null;
    Destination destination = null;

    try {
        TextMessage msg;

        System.out.println("Publishing to destination '" + topicName
                + "'\n");

        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
                serverUrl);

        connection = factory.createConnection(userName, password);

        /* create the session */
        session = connection
                .createSession(javax.jms.Session.AUTO_ACKNOWLEDGE);

        /* create the destination */
        destination = session.createTopic(topicName);

        /* create the producer */
        msgProducer = session.createProducer(null);

        /* publish messages */
        /* create text message */
        msg = session.createTextMessage();

        /* set message text */
        msg.setText(messageStr);

        /* publish message */
        msgProducer.send(destination, msg);

        System.out.println("Published message: " + messageStr);

        /* close the connection */
        connection.close();

    } catch (JMSException e) {
        e.printStackTrace();
    }
}

/*-----------------------------------------------------------------------
 * main
 *----------------------------------------------------------------------*/
public static void main(String[] args) {
    tibjmsMsgTopicProducer.sendTopicMessage("topic1",
            "This is the message content !");
}

}

Note : You could also want to use EMS with Spring-JMS for a more "Enterprise grade" solution. The code above is a lot simpler.

Note2: I made the method "static". This is only for demonstration purpose. Connections are costly in JMS, so normally we try to reuse them. See all TIBCO provided example for better setup of the Java classes. Instantiate and reuse connections if you can. Additionally, J2EE or Spring solutions will have support for connection pools built-in.