如何使用 Java 将消息发布到 EMS 主题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23650060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:30:59  来源:igfitidea点击:

How to publish messages to EMS Topic using Java

javatibcotibco-ems

提问by Bala

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

我想将测试消息发布到 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);
    }
}

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

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

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

采纳答案by GhislainCote

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.

我通过修改 EMS 8.0“sample”文件夹中的“tibjmsMsgProducer.java”创建了以下代码。查看此文件夹中的所有 Java 示例以获取更多参考。

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).

此代码使用默认用户和密码将简单的硬编码文本消息发布到本地 EMS。目标主题是“topic1”(在最后一行)。

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-JMSfor a more "Enterprise grade" solution. The code above is a lot simpler.

注意:您可能还希望将EMS 与 Spring-JMS一起使用以获得更“企业级”的解决方案。上面的代码简单很多。

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.

注2:我将方法设为“静态”。这仅用于演示目的。JMS 中的连接开销很大,所以通常我们会尝试重用它们。请参阅 TIBCO 提供的所有示例以更好地设置 Java 类。如果可以,实例化和重用连接。此外,J2EE 或 Spring 解决方案将支持内置的连接池。

回答by Alex Suo

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 :-)

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

回答by Xiawei Zhang

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

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