【问题标题】:ActiveMQ JMS Connection Error with TomcatTomcat 的 ActiveMQ JMS 连接错误
【发布时间】:2021-07-17 19:15:06
【问题描述】:

我目前已经像这样配置了我的 Tomcat 的 context.xml

<Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://MY_LOCALHOST_URL:7270" brokerName="LocalActiveMQBroker" useEmbeddedBroker="false"/>
    
<Resource name="AppJms-HVDIVD17CA50359" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="APP.QUEUE" />

我有我的activemq.xml

<transportConnectors>
    <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
    <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="appjms" uri="tcp://MY_LOCALHOST_URL:7270?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

我正在像这样在 Java 代码中启动 JMS:

public void createMessageSubscriberJms(String host, int port, String jmsDestination) throws JMSException, UnknownHostException {
    
    String hostname = InetAddress.getLocalHost().getCanonicalHostName();
    String providerEndpoints = "tcp://" + hostname + ":" + port + "?wireFormat.maxInactivityDuration=7200000";

    // Set the trusted packages/classes to move back and forth on the ActiveMQ JMS service.
    ArrayList<String> trustedClasses = new ArrayList<String>();

    trustedClasses.add("com.gtt.common.shared.GTCMessage");

    // Obtain the factory
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();

    activeMQConnectionFactory.setBrokerURL(providerEndpoints);

    // Add the trusted packages/classes to the ActiveMQ consumer.
    //activeMQConnectionFactory.setTrustedPackages(trustedClasses);
    activeMQConnectionFactory.setTrustAllPackages(true);

    //Create the connection
    setQueueConnection(activeMQConnectionFactory.createQueueConnection());
    getQueueConnection().setClientID(this.getName());

    // Make a session
    setSession(getQueueConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE));

    getSession().createQueue(jmsDestination);

    // Create the destination
    Destination destination = getSession().createQueue(jmsDestination);

    String selector = "JMSCorrelationID = '" + getActionRequest().getOriginId() + "_" + getActionRequest().getRequestId() + "'" ;

    setConsumer(getSession().createConsumer(destination, selector));
    getConsumer().setMessageListener(new DefaultMessageListener(this));

    // Start ...
    // We'll need a message store now
    gtcMessages = new GTCMessageQueue<GTCMessage>();

    getQueueConnection().start();
}

但是当我启动 Tomcat 并调用该方法时,我收到以下错误:

Name [AppJms-HVDIVD17CA50359] is not bound in this Context. Unable to find [AppJms-HVDIVD17CA50359].

你能告诉我我做错了什么吗?我确信当我使用旧版本的 Tomcat 和 ActiveMQ 时,相同的配置也可以工作。

目前我使用的是 Tomcat 9.0.45 和 ActiveMQ 5.16.1。

【问题讨论】:

    标签: java tomcat jms activemq tomcat9


    【解决方案1】:

    你对AppJms-HVDIVD17CA50359 的定义对我来说有点奇怪:

    <Resource name="AppJms-HVDIVD17CA50359" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="APP.QUEUE" />
    

    看起来您正在定义一个 JMS 队列,因为 physicalName 属性是 APP.QUEUE。但是,typeorg.apache.activemq.ActiveMQConnectionFactory,这对于 JMS 连接工厂而不是队列来说是正确的 type。此外,org.apache.activemq.ActiveMQConnectionFactory 类没有physicalName 属性。我认为您应该在这里使用org.apache.activemq.command.ActiveMQQueue 作为type,例如:

    <Resource name="AppJms-HVDIVD17CA50359" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="APP.QUEUE" />
    

    值得注意的是,您似乎没有在此代码中使用AppJms-HVDIVD17CA50359jms/ConnectionFactory。我没有看到任何使用这些名称的 JNDI 查找。因此,您可以简单地从您的context.xml 中删除这些资源定义(假设您的应用程序中没有其他任何东西使用它们),这也可能会解决错误。

    也就是说,我鼓励您使用这些资源,因为这将使您以后更新应用程序变得更加容易,因为如果您需要更改连接工厂或队列配置,那么您只需要修改 @987654336 @ 而不是您的应用程序的代码。这是在像 Tomcat 这样的服务器上运行应用程序的主要好处之一。

    【讨论】:

    • 感谢您的意见。我使用上下文 XML 来实现代码。问题出在发布者代码而不是消费者代码中。我使用jms/ConnectionFactory 来获取 ActiveMQ 连接工厂并从中创建一个队列,并从 context.xml 中删除了队列定义。您的意见有所帮助。代码很长,所以不能在回复中发给你看。但我让它工作了!谢谢!
    猜你喜欢
    • 2020-09-25
    • 2013-01-24
    • 2015-08-26
    • 2012-01-21
    • 2020-09-14
    • 2016-06-03
    • 2015-02-03
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多