activemqjms与tomcat的连接错误

tmb3ates  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(243)

我目前已经配置了我的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时,同样的配置也能工作。
目前我使用的是tomcat9.0.45和activemq5.16.1。

koaltpgm

koaltpgm1#

你对 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 哪个是正确的 type 对于jms连接工厂,而不是队列。此外 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-HVDIVD17CA50359 或者 jms/ConnectionFactory 在这个代码里。我没有看到任何使用这些名称的jndi查找。因此,您可以简单地从应用程序中删除这些资源定义 context.xml (假设应用程序中没有其他程序使用它们),这也可能解决错误。
也就是说,我鼓励您使用这些资源,因为这将使将来更新应用程序更加容易,因为如果您需要更改连接工厂或队列配置,那么您只需要修改 context.xml 而不是应用程序的代码。这是在tomcat这样的服务器上运行应用程序的主要好处之一。

相关问题