Categories
Computing

JMS 2.0 Standalone Applications

For a project of mine, I was looking to connect a standalone Java application via JMS with a GlassFish 4 backend service. To put it in other words, I wanted to run GlassFish 4 and get access to the JMS queues and topics from a standalone container.

Having done this before with GlassFish 3.1. and JMS 1.8, I figured this be an easy migration task.

Wrong. As it turns out, it is not quite as simple. However, here is the solution to it.

I assume you are building your standalone application with Maven or, preferably, Gradle. The only artifact that you need to add to your build file is

compile 'org.glassfish.main.appclient:gf-client:4.0'

That alone should give you access to your GlassFish’s JMS queues. It doesn’t though. Instead, it will give you

Lookup failed for 'jms/__defaultConnectionFactory'

if you try to connect to your queues. As an example, use the following code:

QueueConnectionFactory connectionFactory = ServiceLocator.getInstance().
                getQueueConnectionFactory(Constants.JMS_CONNECTION_FACTORY);
Queue queue = ServiceLocator.getInstance().getQueue(Constants.JMS_QUEUE_OUTBOUND);

Connection connection = connectionFactory.createConnection();
connection.setExceptionListener(new JMSExceptionListener());
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(this);
connection.start();

After trying around for a while, I found that (at least in the current version that I am using) you will also require two additional jars:

  • imqbroker.jar
  • imqjmsra.jar

See also this thread on stackoverflow. If you have both jars in a subdirectory called “jms”, you can just add the following dependency to your gradle build script:

compile fileTree(dir: '../lib/jms', includes: ['*.jar'])

Note: The GlassFish Maven artifacts have huge dependencies, in particular gf-client:4.0 requires about 60 MB of libraries.