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

 LISA_W186 发布于 2023-01-15 11:39

我想向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}]

GhislainCote.. 8

我通过修改EMS 8.0"sample"文件夹中的"tibjmsMsgProducer.java"创建了以下代码.查看此文件夹中的所有Java示例以获取进一步的参考.

此代码使用默认用户和密码将简单的硬编码文本消息发布到本地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 !");
}

}

注意:您可能还希望将EMS与Spring-JMS结合使用,以获得更"企业级"的解决方案.上面的代码更简单.

注2:我把方法设为"静态".这仅用于演示目的.JMS中的连接成本很高,因此通常我们会尝试重用它们.有关更好地设置Java类的信息,请参阅所有TIBCO提供的示例.如果可以,实例化并重用连接.此外,J2EE或Spring解决方案将支持内置的连接池.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有