java 使用 JBoss 向远程 JMS 队列发布消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1037526/
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-10-29 14:53:12  来源:igfitidea点击:

Post a message to a remote JMS queue using JBoss

javapostconnectionjmsmessage

提问by Vincent Robert

This looks simple but I can't find a simple answer.

这看起来很简单,但我找不到简单的答案。

I want to open a connection to a remote JMS broker (IP and port are known), open a session to the a specific queue (name known) and post a message to this queue.

我想打开到远程 JMS 代理的连接(IP 和端口已知),打开到特定队列(名称已知)的会话并将消息发布到该队列。

Is there any simple Java API (standard if possible) to do that ?

是否有任何简单的 Java API(如果可能是标准的)来做到这一点?



EDIT

编辑

Ok I understand now that JMS is a driver spec just like JDBC and not a communication protocol as I thought.

好的,我现在明白 JMS 是一个驱动程序规范,就像 JDBC 一样,而不是我想象的通信协议。

Given I am running in JBoss, I still don't understand how to create a JBossConnectionFactory.

鉴于我在 JBoss 中运行,我仍然不明白如何创建JBossConnectionFactory



EDIT

编辑

I actually gave the problem some thoughts (hmmm) and if JMS needs to be treated the same as JDBC, then I need to use a client provided by my MQ implementation. Since we are using SonicMQ for our broker, I decided to embed the sonic_Client.jar library provided with SonicMQ.

我实际上给了这个问题一些想法(嗯),如果 JMS 需要与 JDBC 一样对待,那么我需要使用我的 MQ 实现提供的客户端。由于我们使用 SonicMQ 作为我们的代理,我决定嵌入 SonicMQ 提供的 sonic_Client.jar 库。

This is working in a standalone Java application and in our JBoss service.

这适用于独立的 Java 应用程序和我们的 JBoss 服务。

Thanks for the help

谢谢您的帮助

采纳答案by Nick Holt

You'll need to use JMS, create a QueueConnectionFactoryand go from there. Exactly how you create the QueueConnectionFactorywill be vendor specific (JMS is basically a driver spec for message queues just as JDBC is for databases) but on IBM MQ it something like this:

您需要使用 JMS,创建一个QueueConnectionFactory并从那里开始。具体如何创建QueueConnectionFactory将取决于供应商(JMS 基本上是消息队列的驱动程序规范,就像 JDBC 是用于数据库一样)但在 IBM MQ 上,它是这样的:

MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
connectionFactory.setHostName(<hostname>);
connectionFactory.setPort(<port>);
connectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
connectionFactory.setQueueManager(<queue manager>);
connectionFactory.setChannel("SYSTEM.DEF.SVRCONN");

QueueConnection queueConnection = connectionFactory.createQueueConnection();
QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

Queue queue = queueSession.createQueue(<queue name>);

QueueSender queueSender = session.createSender(queue);
QueueReceiver queueReceiver = session.createReceiver(queue); 

EDIT (following question edit)

编辑(以下问题编辑)

The best way to access a remote queue, or any queue for that matter, is to add a Queueinstance to the JNDI registry. For remote queues this is achieved using MBeans that add the Queueinstance when the server starts.

访问远程队列或与此相关的任何队列的最佳方法是向QueueJNDI 注册中心添加一个实例。对于远程队列,这是使用Queue在服务器启动时添加实例的MBean 来实现的。

Take a look at http://www.jboss.org/community/wiki/UsingWebSphereMQSeriesWithJBossASPart4, which while it's an example with IBM MQ, is essentially what you have to do to connect to any remote queue.

看看http://www.jboss.org/community/wiki/UsingWebSphereMQSeriesWithJBossASPart4,虽然它是 IBM MQ 的一个例子,但本质上是连接到任何远程队列所必须做的。

If you look at jbossmq-destinations-service.xmland org.jboss.mq.server.jmxyou'll see the MBeans you need to create in relation to a JBoss queue.

如果您查看jbossmq-destinations-service.xml并且org.jboss.mq.server.jmx您将看到您需要创建的与 JBoss 队列相关的 MBean。

回答by Vincent Robert

Here is the code we used to connect to the SonicMQ broker using the sonic_Client.jarlibrary:

这是我们用来使用sonic_Client.jar库连接到 SonicMQ 代理的代码:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;


public class JmsClient
{
    public static void main(String[] args) throws JMSException
    {
        ConnectionFactory factory = new progress.message.jclient.ConnectionFactory("tcp://<host>:<port>", "<user>", "<password>");
        Connection connection = factory.createConnection();

        try
        {
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            try
            {
                MessageProducer producer = session.createProducer(session.createQueue("<queue>"));
                try
                {
                    producer.send(session.createTextMessage("<message body>"));
                }
                finally
                {
                    producer.close();
                }
            }
            finally
            {
                session.close();
            }
        }
        finally
        {
            connection.close();
        }
    }
}

回答by Betlista

Actually I'm using JBoss 4 and JNDI is not difficult to use.

实际上我使用的是 JBoss 4,而 JNDI 并不难使用。

First of all you have to know where your JNDI is running.

首先,您必须知道 JNDI 在哪里运行。

In my JBoss (conf\jboss-service.xml) I have:

在我的 JBoss (conf\jboss-service.xml) 中,我有:

<mbean code="org.jboss.naming.NamingService" name="jboss:service=Naming" xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">
    ...
    <attribute name="Port">7099</attribute>
    ...
</mbean>

This is important, this is port you want to connect to.

这很重要,这是您要连接的端口。

Now you can easily connect to JNDI using this code:

现在您可以使用以下代码轻松连接到 JNDI:

Hashtable<String, String> contextProperties = new Hashtable<String, String>();
contextProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
contextProperties.put(Context.PROVIDER_URL, "jnp://localhost:7099");

InitialContext initContext = new InitialContext(contextProperties);

Now when you have context, it's very similar to @Nick Holt's answer, except connection factory creation, you have to use:

现在,当您有上下文时,它与@Nick Holt 的答案非常相似,除了连接工厂创建之外,您必须使用:

QueueConnectionFactory connFactory = (QueueConnectionFactory) initContext.lookup("ConnectionFactory");

Also you do not need to create queue if there is deployed some

如果部署了一些,您也不需要创建队列

Queue queue = (Queue) initContext.lookup("queueName");

All the code above was tested with JBoss 4.2.2 GA and JBossMQ (JBossMQ was, if I'm correct, replaced in 4.2.3 with JBoss messaging).

上面的所有代码都用 JBoss 4.2.2 GA 和 JBossMQ 测试过(如果我没记错的话,JBossMQ 在 4.2.3 中被 JBoss 消息替换了)。