来自远程 ActiveMQ Artemis 的 Wildfly JMS Consumer

发布于 2025-01-14 14:56:47 字数 3440 浏览 1 评论 0原文

我正在尝试在 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();
            }
        }
    }

杂项: 制作人独自工作。

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

I am trying to implement simple JMS Producer and Consumer within Wildfly(Version 24) and remote ActiveMQ Artemis broker.

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>

Producer and Consumer

    @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();
            }
        }
    }

Misc:
Producer alone works.

Result:
First message is received by broker but consumption is not executed and nothing happens(no logs).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-01-21 14:56:47

关于pooled-connection-factory文档说:

它只能用于在 JNDI 中查找或注入时发送(即生成)消息。

您应该创建一个“正常”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

Regarding the pooled-connection-factory the documentation says:

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

You should create a "normal" connection-factory for your consumer to use, e.g.:

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

Or via the JBoss CLI:

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

Then in your code you could do something like the following:

@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();
        }
    }
}

If you need to set credentials on the injected JMSContext then you can annotate it with @JMSPasswordCredential.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文