简体   繁体   English

使用 ActiveMQ Artemis 发送 AMQ 消息

[英]Send AMQ Message using ActiveMQ Artemis

I want to send a message to an ActiveMQ Artemis instance on a WildFly server.我想向 WildFly 服务器上的 ActiveMQ Artemis 实例发送消息。 I am using this tutorial in trying to configure the standalone-full.xml .我正在使用教程尝试配置standalone-full.xml I am using the jboss/wildfly docker image and am exposing the below netty port: 5445 .我正在使用jboss/wildfly docker 图像并暴露以下网络端口: 5445

Standalone configuration:独立配置:

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

I tried to create a simple test case that would send a message to the ActiveMQ Artemis instance:我尝试创建一个简单的测试用例,将消息发送到 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();
}

Upon execution, I get this error on my client:执行后,我的客户端出现此错误:

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

I get this error on my server:我在我的服务器上收到此错误:

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

I tried to follow this post in answering my own question but was unable to find a useful solution.我试图按照这篇文章回答我自己的问题,但找不到有用的解决方案。

What am I missing in configuring the ActiveMQ instance, and how can I test it with Java code?我在配置 ActiveMQ 实例时缺少什么,如何使用 Java 代码对其进行测试?

In your sendAmqMessage method you're creating a javax.jms.ConnectionFactory instance like so:在您的sendAmqMessage方法中,您正在创建一个javax.jms.ConnectionFactory实例,如下所示:

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

This ActiveMQConnectionFactory is from the ActiveMQ 5.x client and uses the OpenWire protocol which the remote-acceptor in your standalone-full.xml doesn't understand, hence the error about failing to decode the incoming backup.ActiveMQConnectionFactory来自 ActiveMQ 5.x 客户端并使用 OpenWire 协议,您的standalone-full.xml中的remote-acceptor不理解,因此有关未能解码传入备份的错误。

As the documentation you're following suggests, instead of instantiating the ConnectionFactory directly you should simply look it up in JNDI like so:正如您所遵循的文档所建议的那样,您应该像这样在 JNDI 中简单地查找它,而不是直接实例化ConnectionFactory

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");

Also, make sure you're using the correct dependency, ie:另外,请确保您使用的是正确的依赖项,即:

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

Using JNDI this way will also allow you to remove this from your standalone-full.xml :以这种方式使用 JNDI 还可以让您从standalone-full.xml中删除它:

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

As well as this:还有这个:

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

You really only need the resources defined in the default standalone-full.xml .你真的只需要在默认的standalone-full.xml中定义的资源。

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

相关问题 ActiveMQ持久性:AMQ消息存储的一些麻烦 - ActiveMQ persistence: some troubles with AMQ Message Store 通过 ActiveMQ 代理插件获取 AMQ 消息 - Get AMQ Message via ActiveMQ Broker Plugin 如何在 Activemq-Artemis 中设置 AMQ_SCHEDULED_DELAY OR _HQ_SCHED_DELIVERY 属性 - how to set the AMQ_SCHEDULED_DELAY OR _HQ_SCHED_DELIVERY property in Activemq-Artemis 通过 TCP 连接使用 Jolokia 检查 AMQ Artemis 代理是否健康 - Check if AMQ Artemis broker is healthy using Jolokia through TCP connection 如何使用 ActiveMQ-Artemis 从 Java 程序向 Javascript STOMP 客户端发送消息? - How do I send messages from Java program to a Javascript STOMP client using ActiveMQ-Artemis? Jboss AMQ:从hawtio发送持久消息 - Jboss AMQ: Send persistent message from hawtio 在ACTIVEMQ中将_AMQ_SCHED_DELIVERY属性用于计划的消息 - Using _AMQ_SCHED_DELIVERY property in ACTIVEMQ for scheduled messages 消费者从 Apache ActiveMQ Artemis 中的接收方法接收单个消息 - Consumer receives Single message from receive method in Apache ActiveMQ Artemis 消息计数&lt;1000时ActiveMQ Artemis浏览器为空消息 - ActiveMQ Artemis browser empty messages when message count < 1000 错误:AMQ154003:无法使用 Wildfly 连接到 JMS ActiveMQ - Error: AMQ154003: Unable to connect to JMS ActiveMQ using Wildfly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM