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).
CodePudding user response:
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
.