我正在嘗試在 Wildfly(版本 24)和遠程 ActiveMQ Artemis 代理中實作簡單的 JMS 生產者和消費者。
獨立的.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();
}
}
}
雜項: 制片人獨自作業。
結果: 代理收到第一條訊息,但沒有執行消費,也沒有任何反應(沒有日志)。
uj5u.com熱心網友回復:
關于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
.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/445244.html
標籤:爪哇 杰姆斯 野蝇 activemq-artemis
下一篇:while回圈增加了額外的時間