4

我想向 EMS 主题发布一条测试消息,并且可以使用一些指示。到目前为止,我已经设法做到了

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);
    }
}

这会产生以下内容。如何将消息发布到主题“topic1”或队列“Q1”

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

3 回答 3

9

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.

于 2014-05-14T19:09:56.957 回答
1

我已经有一段时间没有接触 EMS 了——但基本上 EMS 只不过是一个 JMS 实现。所有实现特定的东西都已为您隐藏。您只需使用标准 JMS 方式发布/订阅主题,您可以在 Java 教程和在线资源中找到很好的示例。我会在这里保存我丑陋的示例代码:-)

于 2014-05-14T09:04:19.900 回答
0

您可以查看@gelnyang 构建的这个测试项目。这是专门发布EMS消息类。在该项目下,您还可以找到其他与 EMS 相关的功能。

于 2015-12-09T08:27:12.583 回答