使用ActiveMQ Artemis发送AMQ消息

问题描述

我想向WildFly服务器上的ActiveMQ Artemis实例发送消息。我正在使用this教程来尝试配置standalone-full.xml。我正在使用jboss/wildfly泊坞窗映像,并暴露以下网络端口:5445

独立配置:

<subsystem xmlns="urn:jboss:domain:messaging-activemq:10.0">
    <server name="default">
        <security enabled="true"/>
        <security-setting name="#">
            <role name="SuperUser" 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" redistribution-delay="1000"/>
        <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"/>
        <remote-connector name="netty" socket-binding="messaging"/>
        <remote-acceptor name="netty" socket-binding="messaging"/>

        <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"/>

        <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
        <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
        <jms-queue name="JoeIsCool" entries="java:/jms/queue/JoeIsCool"/>

        <connection-factory name="InVmConnectionFactory" connectors="in-vm" entries="java:/ConnectionFactory"/>
        <connection-factory name="RemoteConnectionFactory" ha="true" block-on-acknowledge="true" reconnect-attempts="-1" connectors="netty" entries="java:jboss/exported/jms/RemoteConnectionFactory"/>
        <pooled-connection-factory name="activemq-ra" transaction="xa" connectors="in-vm" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory"/>
    </server>
</subsystem>


<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">

    ...
    <socket-binding name="messaging" port="5445"/>
    ...

</socket-binding-group>

我试图创建一个简单的测试用例,该用例将向ActiveMQ Artemis实例发送一条消息:

public void sendAmqMessage() throws Exception {
    final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin","secretPassoword","tcp://localhost:5445");
    final Connection connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    final Destination destination = session.createQueue("JoeIsCool");
    final MessageProducer producer = session.createProducer(destination);
    final TextMessage message =
    session.createTextMessage("Hello !!! Welcome to the world of ActiveMQ.");
    producer.send(message);
    connection.close();
}

执行后,我的客户端出现此错误:

javax.jms.JMSException: Cannot send,channel has already failed: tcp://127.0.0.1:5445

我在服务器上收到此错误:

(Thread-1 (activemq-netty-threads)) AMQ214013: Failed to decode packet: java.lang.IllegalArgumentException: AMQ219032: Invalid type: 1

我试图按照this post回答自己的问题,但找不到有效的解决方案。

在配置ActiveMQ实例时我缺少什么?如何使用Java代码对其进行测试?

解决方法

在您的sendAmqMessage方法中,您正在创建一个javax.jms.ConnectionFactory实例,如下所示:

final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin","secretPassoword","tcp://localhost:5445");

ActiveMQConnectionFactory来自ActiveMQ 5.x客户端,并使用您的remote-acceptor中的standalone-full.xml不理解的OpenWire协议,因此有关未能解码传入的错误备份。

按照the documentation的建议,与其直接实例化ConnectionFactory,不如直接在JNDI中查找它,就像这样:

final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL,"http-remoting://localhost:8080");
InitialContext remotingCtx = new InitialContext(env);
final ConnectionFactory connectionFactory = (ConnectionFactory) remotingCtx.lookup("jms/RemoteConnectionFactory");

此外,请确保您使用的是正确的依赖关系,即:

<dependencies>
  <dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-jms-client-bom</artifactId>
    <type>pom</type>
  </dependency>
</dependencies>

以这种方式使用JNDI还可以将其从standalone-full.xml中删除:

<remote-connector name="netty" socket-binding="messaging"/>
<remote-acceptor name="netty" socket-binding="messaging"/>

还有:

<socket-binding name="messaging" port="5445"/>

您实际上只需要在默认standalone-full.xml中定义的资源。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...