简体   繁体   English

如何在 java 代码中连接到 JMS 域?

[英]How to connect to JMS domain in java code?

I am able to run the below java code to send a message to SonicMQ JMS queue.我可以运行下面的 java 代码向 SonicMQ JMS 队列发送消息。 It is copied from here: Post a message to a remote JMS queue using JBoss它是从这里复制的: Post a message to a remote JMS queue using JBoss

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

However, I get error javax.jms.InvalidDestinationException: Queue not found但是,我收到错误 javax.jms.InvalidDestinationException: Queue not found

I think this is because I need to specify queue Domain Name.我认为这是因为我需要指定队列域名。 Where to put Domain Name in this code?在此代码中将域名放在哪里?

As stated here https://jnbridge.com/guides/UsingDotNetAdapterSonicMQ.pdf , the following JNDI parameter should be set sonicsw.jndi.mfcontext.domain=[Domain_Name]如此处所述https://jnbridge.com/guides/UsingDotNetAdapterSonicMQ.pdf ,应设置以下 JNDI 参数 sonicsw.jndi.mfcontext.domain=[Domain_Name]

How to set JNDI parameter in the code above?如何在上面的代码中设置 JNDI 参数?

Thank you谢谢

Typically you would use JNDI to lookup both the javax.jms.ConnectionFactory and the javax.jms.Destination (ie javax.jms.Queue or javax.jms.Topic ).通常,您会使用 JNDI 来查找javax.jms.ConnectionFactoryjavax.jms.Destination (即javax.jms.Queuejavax.jms.Topic )。 This would involve instantiating a javax.naming.InitialContext with a set of properties for whatever specific implementation you're using and then using that javax.naming.InitialContext to perform a lookup.这将涉及使用一组属性为您正在使用的任何特定实现实例化javax.naming.InitialContext ,然后使用该javax.naming.InitialContext执行查找。

However, you're not actually using JNDI at all.但是,您实际上根本没有使用 JNDI You're instantiating the JMS ConnectionFactory directly (ie using new progress.message.jclient.ConnectionFactory(...) ) and later calling javax.jms.Session.createQueue(...) to instantiate the local javax.jms.Queue .您正在直接实例化 JMS ConnectionFactory (即使用new progress.message.jclient.ConnectionFactory(...) ),然后调用javax.jms.Session.createQueue(...)来实例化本地javax.jms.Queue

Keep in mind that using javax.jms.Session.createQueue(...) to instantiate the local javax.jms.Queue has no impact on the JMS server.请记住,使用javax.jms.Session.createQueue(...)实例化本地javax.jms.Queue对 JMS 服务器没有影响 As the JavaDoc notes:正如JavaDoc所指出的:

Note that this method simply creates an object that encapsulates the name of a queue.请注意,此方法只是创建一个封装队列名称的 object。 It does not create the physical queue in the JMS provider.它不会在 JMS 提供程序中创建物理队列。 JMS does not provide a method to create the physical queue, since this would be specific to a given JMS provider. JMS 不提供创建物理队列的方法,因为这将特定于给定的 JMS 提供者。 Creating a physical queue is provider-specific and is typically an administrative task performed by an administrator, though some providers may create them automatically when needed.创建物理队列是特定于提供商的,通常是由管理员执行的管理任务,尽管一些提供商可能会在需要时自动创建它们。

The reason you're getting an InvalidDestinationException is because the queue you're trying to use does not exist on the JMS broker.您收到InvalidDestinationException的原因是您尝试使用的队列在 JMS 代理上不存在。 You need to administratively create that destination or change the name you're passing to createQueue to match a queue that already exists.您需要以管理方式创建该目标或更改您传递给createQueue的名称以匹配已存在的队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM