Question

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}]
Was it helpful?

Solution

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.

OTHER TIPS

I haven't touched EMS for some time - but basically EMS is nothing but a JMS implementation. All implementation specific stuff has been hidden for you. You just use standard JMS way to pub/sub to topics, which you can find good example on Java tutorial and online sources. I would save my ugly sample code here :-)

You can take a look at this test project @gelnyang built. And this is the class for publishing EMS message specifically. Under the project, you can find other EMS related functionalities as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top