使用 http-remoting 协议将 Tomcat 连接到 JBoss EAP 7.2 中嵌入的 ActiveMQ

Connect Tomcat to ActiveMQ embedded in JBoss EAP 7.2 using http-remoting Protocol

为了更好的重现性,我在 docker-compose.yml 中定义了以下系统。

version: '2.2'
services:
  jmstool:
    image: spx01/jmstool-activemq
    container_name: jmstool
    ports:
      - 8181:8080
    volumes:
      - ./context.xml:/usr/local/tomcat/conf/context.xml
  jboss:
    image: daggerok/jboss-eap-7.2
    container_name: jboss
    environment:
      - PREPEND_JAVA_OPTS=-Djboss.server.default.config=standalone-full.xml
    ports:
      - 8080:8080
    volumes:
      - ./configure.cli:/tmp/configure.cli

工具 jmstool 是一个部署在 Tomcat 9 上的 Spring 应用程序,用于通过 JMS 与不同队列进行通信。我可以让它与独立的 activeMQ 一起工作。

这些容器启动后,我将一个应用程序用户和两个队列添加到 运行 JBoss:

的消息传递子系统
docker exec jboss jboss-eap-7.2/bin/add-user.sh -a -u myUser -p "myPa##word"
docker exec jboss jboss-eap-7.2/bin/jboss-cli.sh --file=/tmp/configure.cli

内容为configure.cli

connect
/subsystem=messaging-activemq/server=default/jms-queue=ImportantMessages:add(entries=[java:jboss/exported/ImportantMessages])
/subsystem=messaging-activemq/server=default/jms-queue=ImportantMessagesOut:add(entries=[java:jboss/exported/ImportantMessagesOut])

standalone-full.xml 看起来不错,与[另一个示例][2] 非常相似。

    <subsystem xmlns="urn:jboss:domain:messaging-activemq:4.0">
        <server name="default">
            <journal pool-files="10"/>
            <security-setting name="#">
                <role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
            </security-setting>
            <address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/>
            <http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
            <http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
                <param name="batch-delay" value="50"/>
            </http-connector>
            <in-vm-connector name="in-vm" server-id="0">
                <param name="buffer-pooling" value="false"/>
            </in-vm-connector>
            <http-acceptor name="http-acceptor" http-listener="default"/>
            <http-acceptor name="http-acceptor-throughput" http-listener="default">
                <param name="batch-delay" value="50"/>
                <param name="direct-deliver" value="false"/>
            </http-acceptor>
            <in-vm-acceptor name="in-vm" server-id="0">
                <param name="buffer-pooling" value="false"/>
            </in-vm-acceptor>
            <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
            <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
            <jms-queue name="ImportantMessages" entries="java:jboss/exported/ImportantMessages"/>
            <jms-queue name="ImportantMessagesOut" entries="java:jboss/exported/ImportantMessagesOut"/>
            <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
            <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
            <pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
        </server>
    </subsystem>

现在我尝试在 Spring 应用程序的 context.xml 中配置此远程连接。我(遗憾地)必须使用 http-remoting 协议,而不是通过 tcp.

的连接
<Context>
<JarScanner scanClassPath="false" scanAllFiles="false" scanAllDirectories="false"/>

<Resource
        name="jms/ImportantMessages"
        auth="Container"
        type="org.apache.activemq.command.ActiveMQQueue"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
        physicalName="ImportantMessages"/>

<Resource
        name="jms/ImportantMessagesOut"
        auth="Container"
        type="org.apache.activemq.command.ActiveMQQueue"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
        physicalName="ImportantMessagesOut"/>

<Environment name="spring.jms.jndi-name"
             value="jms/RemoteConnectionFactory"
             type="java.lang.String"
             override="false"/>

<Environment name="jmstool.outgoingQueues"
             value="java:comp/env/jms/ImportantMessages"
             type="java.lang.String"
             override="false"/>

<Environment name="jmstool.incomingQueues"
             value="java:comp/env/jms/ImportantMessagesOut"
             type="java.lang.String"
             override="false"/>

</Context>

jmstool 日志中的异常并不让我感到意外:

ackOff{interval=5000, currentAttempts=14, maxAttempts=unlimited}. Cause: Could not create Transport. Reason: java.io.IOException: Transport scheme NOT recognized: [http-remoting]

当我将 [jboss-remoting][3]、[jboss-remoting3][4] 或 [activemq-optional][5] 添加到 [=62 时,仍然出现此错误=]的lib文件夹。

有人知道我做错了什么吗?

编辑:贾斯汀的回答帮助了我。在将 artemis-jms-client-all.jar 添加到 tomcats lib 文件并重新配置 context.xml 后,可以建立连接。此外,由于以下异常,客户端无法查找队列。我在哪里可以配置队列的地址属性?

当我打电话时

new JndiLocatorDelegate().lookup("java:comp/env/jms/ImportantMessages", javax.jms.Queue.class)

抛出这个异常

        Caused by: java.lang.IllegalArgumentException: address cannot be null
            at org.apache.activemq.artemis.jms.client.ActiveMQDestination.setSimpleAddress(ActiveMQDestination.java:414)
            at org.apache.activemq.artemis.jms.client.ActiveMQDestination.setAddress(ActiveMQDestination.java:399)
            at org.apache.activemq.artemis.jms.client.ActiveMQDestination.buildFromProperties(ActiveMQDestination.java:540)
            at org.apache.activemq.artemis.jndi.JNDIStorable.setProperties(JNDIStorable.java:58)
            at org.apache.activemq.artemis.jndi.JNDIReferenceFactory.getObjectInstance(JNDIReferenceFactory.java:65)
            at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
            at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
            at org.apache.naming.NamingContext.lookup(NamingContext.java:840)
            ... 68 more

当我将非空字符串设置为定义资源节点的队列的 address="asdf" 时,出现异常

DefaultMessageListenerContainer  : Could not refresh JMS Connection for destination 'java:comp/env/jms/ImportantMessagesOut' - retrying using FixedB
ackOff{interval=5000, currentAttempts=9, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is ActiveMQConnectionTimedOutException[errorType=CONNECTION_TIMEDOUT message=AMQ219013:
Timed out waiting to receive cluster topology. Group:null]

首先要注意的是,通过与 JBoss EAP 集成,您并不是在使用 ActiveMQ 5.x。您正在使用 ActiveMQ Artemis。因此,您需要使用 ActiveMQ Artemis 的集成 类,例如:

<Context>
<JarScanner scanClassPath="false" scanAllFiles="false" scanAllDirectories="false"/>

<Resource
        name="jms/ImportantMessages"
        auth="Container"
        type="org.apache.activemq.artemis.jms.client.ActiveMQQueue"
        factory="org.apache.activemq.artemis.jndi.JNDIReferenceFactory"
        address="ImportantMessages"/>

<Resource
        name="jms/ImportantMessagesOut"
        auth="Container"
        type="org.apache.activemq.artemis.jms.client.ActiveMQQueue"
        factory="org.apache.activemq.artemis.jndi.JNDIReferenceFactory"
        address="ImportantMessagesOut"/>

<Resource
        name="jms/connectionFactory"
        auth="Container"
        userName="myUser"
        password="myPa##word"
        type="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"
        factory="org.apache.activemq.artemis.jndi.JNDIReferenceFactory"
        brokerURL="tcp://jboss:8080?httpEnabled=true"/>

<Environment name="jmstool.outgoingQueues"
             value="java:comp/env/jms/ImportantMessages"
             type="java.lang.String"
             override="false"/>

<Environment name="jmstool.incomingQueues"
             value="java:comp/env/jms/ImportantMessagesOut"
             type="java.lang.String"
             override="false"/>

</Context>

这在 ActiveMQ Artemis documentation on Tomcat integration.

中讨论

注意连接工厂 brokerURL 上的 httpEnabled=true。这是一个重要的参数,它使客户端能够使用 JBoss EAP 上定义的 http-acceptor

此外,请务必在您的申请中包含 artemis-jms-client-all.jar

您可以使用来自 JBoss EAP(即 org.wildfly.naming.client.WildFlyInitialContextFactory)的 JNDI 实现获得等效的解决方案。一般来说,在将 JMS 与 JBoss EAP 集成时,这是推荐的方法。然而,前面提到的 org.apache.activemq.artemis.jndi.JNDIReferenceFactory 提供了与 Tomcat 的简单、直接的集成。此外,我还没有确认在这种情况下使用 JBoss EAP JNDI 实际上是可能的,而且它几乎肯定比我上面概述的直接集成更复杂。