First, you should create a Maven project in your favorite IDE (Eclipse or IntelliJ, for example) using the maven-archetype-quickstart. Later, I will give a complete pom.xml that supersedes the default one. In this project, let us write a bare-bones synchronous message sender:
package blog.example_JMS;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Sender {
private ConnectionFactory cf;
private Destination d;
public Sender() throws NamingException {
this.cf = InitialContext.doLookup("jms/RemoteConnectionFactory");
this.d = InitialContext.doLookup("jms/queue/QueueExample");
}
public void send(String msg) {
try (JMSContext cntx = this.cf.createContext("joao", "br1o+sa*")) {
JMSProducer prod = cntx.createProducer();
prod.send(d, msg);
}
}
public static void main(String[] args) throws NamingException {
new Sender().send("Hello World, JMS!");
}
}
|
and a receiver:
package blog.example_JMS;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Receiver {
private ConnectionFactory cf;
private Destination d;
public Receiver() throws NamingException {
this.cf = InitialContext.doLookup("jms/RemoteConnectionFactory");
this.d = InitialContext.doLookup("jms/queue/QueueExample");
}
public String receive() {
try (JMSContext cntxt = this.cf.createContext("joao", "br1o+sa*")) {
JMSConsumer cons = cntxt.createConsumer(d);
return cons.receiveBody(String.class);
}
}
public static void main(String[] args) throws NamingException {
System.out.println(new Receiver().receive());
}
}
|
Please be aware of the package: these files should go to the directory src/main/java/blog/example_JMS/.
An issue now is that you should see your IDE pointing out lots of errors in this code, because we haven't changed the pom.xml yet. This a tricky step, as we must find out the right JMS APIs, the ActiveMQ client implementations, and the Java Naming and Directory Interface (JNDI) client. Besides the previous dependencies, we need to correct the default Java version to 1.8 using a plugin (or whatever more recent version you might want to use; Java 9 was just released just four days ago, as I write this message). Overall, this new pom.xml should do the it:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>blog</groupId>
<artifactId>example-JMS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>example-JMS</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- BOM with wildfly JEE7 implementations -->
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-messaging-activemq</artifactId>
<version>11.0.0.CR1</version>
</dependency>
<!-- JMS dependency we need from the BOM -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>11.0.0.CR1</version>
<type>pom</type>
</dependency>
<!-- JNDI dependency we need from the BOM -->
<!-- https://mvnrepository.com/artifact/org.wildfly/wildfly-ejb-client-bom -->
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>11.0.0.CR1</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
If you do a Maven-->update or something similar in your IDE the compiling errors should disappear, as the IDE updates the libraries of the project according to the indications of the pom.xml. To compile your project, you need to define the compile goal. For Eclipse, the following figure should help:
You may press the 'Run' button to compile. Please check my previous message for more details on Maven. We are only a few steps away from running the basic JMS example. First, we need to configure the 'doLookup' operation. For this, we use a jndi.properties file that goes to a new folder that you should create, src/main/resources. This is important, as the programs may not run without the resources folder:
java.naming.factory.initial=org.wildfly.naming.client.WildFlyInitialContextFactory
java.naming.provider.url=http-remoting://localhost:8080
jboss.naming.client.ejb.context=false
|
This sets the class and port that are used to do the lookup for the ConnectionFactory and Destination that both, sender and receiver, must use.
Finally, we need to create the queue that we are using in WildFly, more precisely, 'QueueExample'. Before we do it, we need to create a management user, to create the 'QueueExample' queue in the browser. We also need to set up an application user called 'joao' with password 'br1o+sa*'. For this, you should run add-user.sh command on the bin directory of WildFly:
filipius-portatil:bin filipius$ ./add-user.sh
What type of user do you wish to add?
a) Management User (mgmt-users.properties)
b) Application User (application-users.properties)
The options are really straightforward, but I detailed them in a previous message, here and won't repeat them here.
./standalone.sh -c standalone-full.xml
the standalone-full.xml configuration launches the ActiveMQ provider, which is not active by default (i.e, when the -c option is not provided).
If everything goes well you should see a large number of lines, including some like these, referring to ActiveMQ Artemis:
12:53:38,900 INFO [org.apache.activemq.artemis.core.server] (ServerService Thread Pool -- 68) AMQ221007: Server is now live
12:53:38,900 INFO [org.apache.activemq.artemis.core.server] (ServerService Thread Pool -- 68) AMQ221001: Apache ActiveMQ Artemis Message Broker version 1.5.5.jbossorg-007 [default, nodeID=38071513-93d3-11e7-b595-58b03576e95b]
Now, we must add the previously mentioned queue and give it a public name (compare this to the lookup in the code):
Once this queue is set, we can now run our sender and receiver to get the 'Hello World, JMS!' output.
Let's do another (typical) exercise and create an asynchronous receiver. The System.in.read() in this code illustrates the possibility of doing something else, like reading from another queue, while waiting for an asynchronous event from 'QueueExample':
Let's do another (typical) exercise and create an asynchronous receiver. The System.in.read() in this code illustrates the possibility of doing something else, like reading from another queue, while waiting for an asynchronous event from 'QueueExample':
package blog.example_JMS;
import java.io.IOException;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class AsyncReceiver implements MessageListener {
private ConnectionFactory cf;
private Destination d;
public AsyncReceiver() throws NamingException {
this.cf = InitialContext.doLookup("jms/RemoteConnectionFactory");
this.d = InitialContext.doLookup("jms/queue/QueueExample");
}
@Override
public void onMessage(Message msg) {
try {
System.out.println(((TextMessage) msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public void receive() throws IOException {
try (JMSContext cntxt = this.cf.createContext("joao", "br1o+sa*")) {
JMSConsumer cons = cntxt.createConsumer(d);
cons.setMessageListener(this);
System.in.read();
}
}
public static void main(String[] args) throws NamingException, IOException {
new AsyncReceiver().receive();
}
}
|