来自远程 ActiveMQ Artemis 的 Wildfly JMS Consumer

Wildfly JMS Consumer from remote ActiveMQ Artemis

我正在尝试在 Wildfly(版本 24)和远程 ActiveMQ Artemis 代理中实现简单的 JMS 生产者和消费者。

standalone.xml

<subsystem xmlns="urn:jboss:domain:messaging-activemq:13.0">
    <remote-connector name="remote-artemis" socket-binding="remote-artemis"/>
    <pooled-connection-factory
        name="remote-artemis"
        entries="java:/jms/remoteCF" 
        connectors="remote-artemis" 
        client-id="producer-pooled-connection-factory"
        user="${artemismq.user}"
        password="${artemismq.password}"
        enable-amq1-prefix="true"
    />
    <external-jms-queue name="testQueue" entries="java:/queue/testQueue"/>
</subsystem>

<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
    <socket-binding name="ajp" port="${jboss.ajp.port:8009}"/>
    <socket-binding name="http" port="${jboss.http.port:8080}"/>
    <socket-binding name="https" port="${jboss.https.port:8443}"/>
    <socket-binding name="management-http" interface="management" port="${jboss.management.http.port:9990}"/>
    <socket-binding name="management-https" interface="management" port="${jboss.management.https.port:9993}"/>
    <socket-binding name="txn-recovery-environment" port="4712"/>
    <socket-binding name="txn-status-manager" port="4713"/>
    <outbound-socket-binding name="mail-smtp">
        <remote-destination host="${jboss.mail.server.host:localhost}" port="${jboss.mail.server.port:25}"/>
    </outbound-socket-binding>
    <outbound-socket-binding name="remote-artemis">
        <remote-destination host="${artemismq.host}" port="${artemismq.port}"/>
    </outbound-socket-binding>
</socket-binding-group>

生产者和消费者

    @Inject
    @JMSConnectionFactory("java:/jms/remoteCF")
    private JMSContext context;

    @Resource(lookup = "java:/queue/testQueue")
    private Queue queue;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        try {
            out.write("<p>Sending messages to <em>" + queue + "</em></p>");
            out.write("<p>Using context <em>" + context + "</em></p>");
            out.write("<h2>The following messages will be sent to the destination:</h2>");
            for (int i = 0; i < MSG_COUNT; i++) {
                String text = "This is message " + (i + 1);
                context.createProducer().send(queue, text);
                out.write("Message (" + i + "): " + text + "</br>");

                JMSConsumer consumer = context.createConsumer(queue);
                TextMessage message = (TextMessage) consumer.receive();
                out.write("Message received (" + i + "): " + message.getText() + "</br>");
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

杂项: 制作人一个人工作。

结果: 经纪人收到第一条消息,但未执行消费并且没有任何反应(无日志)。

关于 pooled-connection-factory the documentation 说:

It should only be used to send (i.e. produce) messages when looked up in JNDI or injected.

您应该创建一个“普通”connection-factory 供您的消费者使用,例如:

<connection-factory name="MyConnectionFactory"
                    connectors="remote-artemis"
                    entries="java:/jms/MyConnectionFactory" />

或通过 JBoss CLI:

/subsystem=messaging-activemq/server=default/connection-factory=MyConnectionFactory:add(connectors=["remote-artemis"], entries=["java:/jms/MyConnectionFactory"])

然后在您的代码中,您可以执行如下操作:

@Inject
@JMSConnectionFactory("java:/jms/remoteCF")
private JMSContext context;

@Inject
@JMSConnectionFactory("java:/jms/MyConnectionFactory")
private JMSContext consumerContext;

@Resource(lookup = "java:/queue/testQueue")
private Queue queue;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    try {
        out.write("<p>Sending messages to <em>" + queue + "</em></p>");
        out.write("<p>Using context <em>" + context + "</em></p>");
        out.write("<h2>The following messages will be sent to the destination:</h2>");
        for (int i = 0; i < MSG_COUNT; i++) {
            String text = "This is message " + (i + 1);
            context.createProducer().send(queue, text);
            out.write("Message (" + i + "): " + text + "</br>");

            try (JMSConsumer consumer = consumerContext.createConsumer(queue)) {
               TextMessage message = (TextMessage) consumer.receive();
               out.write("Message received (" + i + "): " + message.getText() + "</br>");
            }
        }
    } catch (JMSException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

如果您需要在注入的 JMSContext 上设置凭据,那么您可以使用 @JMSPasswordCredential.

对其进行注释