使用 Java 发送 JMS 消息

发布于 2024-10-13 12:08:00 字数 141 浏览 5 评论 0 原文

您好,我想连接 JMS 消息并将其从 JSP 发送到我在 Tibco 安装过程中安装的 JMS 服务器。现在,通过浏览互联网上的各种内容,我知道如何将消息从 JAVA 发送到 JMS 队列,但问题是我不知道如何连接到 JMS 服务器本身。 任何人都可以帮助我吗? 谢谢

Hi I want to connect and send a JMS message from my JSP to a JMS server which I had intalled as part of Tibco installation. Now by browsing through various stuff on internet I know how to send the message from JAVA to a JMS queue but the problem is I don't know hoe to connect to JMS server itself.
Can any one please help me in this.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风和你 2024-10-20 12:08:00

您需要在 Java EE 应用服务器上配置它 - WebLogic、JBOSS、Glassfish 等。

如果您在 Tomcat 或 Jetty 上部署 JSP,并且不使用成熟的 Java EE 应用服务器,则必须添加JMS 模块 - 查找 ActiveMQ 或 RabbitMQ 或 OpenJMS。

You need to configure it on a Java EE app server - WebLogic, JBOSS, Glassfish, etc.

If you deploy your JSP on Tomcat or Jetty, and don't use a full-fledged Java EE app server, you'll have to add a JMS module to it - look for ActiveMQ or RabbitMQ or OpenJMS.

飘逸的'云 2024-10-20 12:08:00

基本上,您需要通过在 JNDI 目录中查找连接工厂来获取它,所有其他对象都是从该连接工厂创建的。

这是一个示例(来自 JBoss 文档),展示了如何创建主题会话:

InitialContext iniCtx = new InitialContext();
Object tmp = iniCtx.lookup("ConnectionFactory");
TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
conn = tcf.createTopicConnection();
topic = (Topic) iniCtx.lookup("topic/testTopic");
session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
conn.start();

更多示例 这里

Basically you need to get a connection factory by looking it up in the JNDI directory, all the other objects are created from that connection factory.

This is an example (from the JBoss docs) showing how to create a topic session:

InitialContext iniCtx = new InitialContext();
Object tmp = iniCtx.lookup("ConnectionFactory");
TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
conn = tcf.createTopicConnection();
topic = (Topic) iniCtx.lookup("topic/testTopic");
session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
conn.start();

More examples here

剧终人散尽 2024-10-20 12:08:00

这是一个老话题,但也许会有所帮助。

要从用户界面(单击按钮)发送消息,您需要将按钮单击事件映射到 Java 端。然后您可以编写一段 java 代码来向 JMS 发送消息。

要将消息发送到 JMS 队列实际上需要执行以下操作:

Context context = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue;

这就是它的工作原理:

context = getContext(host, port, user, password);
queueConnection = getConnectionFactory(context, connectionFactoryJndi);
queueSession = getQueueSession(queueConnection);
queue = getQueue(context, queueJndi);

// send a text message
queueConnection.start();
String message = "hello";
sendMessageToQueue(verbose, message, queueSession, queue);
queueConnection.stop();

要获取需要连接到服务器的上下文:

private Context getContext(String host, int port, String user, String password) throws NamingException {
    String url = String.format("%s://%s:%d", protocol, host, port);

    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return new InitialContext(env);
}

获取连接工厂:

private QueueConnection getConnectionFactory(Context context, String jndiName)
        throws NamingException, JMSException {

    QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(jndiName);
    return connectionFactory.createQueueConnection();
}

打开队列会话:

private QueueSession getQueueSession(QueueConnection queueConnection) throws JMSException {
    return queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
}

获取队列:

private Queue getQueue(Context context, String jndiName) throws NamingException {
    return (Queue) context.lookup(jndiName);
}

最后,发送您发送到队列的消息:

private static void sendMessageToQueue(boolean verbose,
字符串消息,
队列会话队列会话,
Queue 队列) throws JMSException {

TextMessage textMessage = queueSession.createTextMessage(message);

try (QueueSender queueSender = queueSession.createSender(queue)) {
    queueSender.send(textMessage);
}

}

这些代码片段来自这里: https://github.com/ zappee/jms-消息发送者
这是一个JMS发送方命令行工具,您可以使用这个项目作为示例。

希望有帮助。

This is an old topic, but maybe it can help.

To send a message from the user interface (clicking on a button) you need to map the button click event to the Java side. Then you can write a java code that sends a message to the JMS.

To send a message to a JMS queue actually need the followings:

Context context = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue;

And this is how it works:

context = getContext(host, port, user, password);
queueConnection = getConnectionFactory(context, connectionFactoryJndi);
queueSession = getQueueSession(queueConnection);
queue = getQueue(context, queueJndi);

// send a text message
queueConnection.start();
String message = "hello";
sendMessageToQueue(verbose, message, queueSession, queue);
queueConnection.stop();

To obtain the context you need to connect to the server:

private Context getContext(String host, int port, String user, String password) throws NamingException {
    String url = String.format("%s://%s:%d", protocol, host, port);

    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return new InitialContext(env);
}

Get the connection factory:

private QueueConnection getConnectionFactory(Context context, String jndiName)
        throws NamingException, JMSException {

    QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(jndiName);
    return connectionFactory.createQueueConnection();
}

Open a queue session:

private QueueSession getQueueSession(QueueConnection queueConnection) throws JMSException {
    return queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
}

Get the queue:

private Queue getQueue(Context context, String jndiName) throws NamingException {
    return (Queue) context.lookup(jndiName);
}

And finally, send your message to the queue:

private static void sendMessageToQueue(boolean verbose,
String message,
QueueSession queueSession,
Queue queue) throws JMSException {

TextMessage textMessage = queueSession.createTextMessage(message);

try (QueueSender queueSender = queueSession.createSender(queue)) {
    queueSender.send(textMessage);
}

}

These code snippets come from here: https://github.com/zappee/jms-message-sender
This is a JMS sender command-line tool, you can use this project as an example.

Hope that it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文