0
votes

Am trying to create a standalone java application to send message to different application using JMS. My reference code uses queueConnectionFactory and all connection parameters are configured in WAS server and retrieved using JNDI. But I cannot use server in my standalone application. In this case, how can I get or connect to the same host, port and queue without using JNDI.

Below is the code I have tried so far

try {
        //Set connection factory
        ActiveMQConnectionFacotry connectionFactory = new ActiveMQConnectionFactory("");
        
        //create connection with connection factory
        Connection connection = connectionFactory.createConnection();
        connection.start();
        
        //create session from connection
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
        //create queue from session
        Destination destination = session.createQueue("sample.Queuename");
        
        //create messageProducer
        MessageProducer producer = session.createProducer(destination);

My reference code

//@Resource
private static Queue queue;
private QueueConnection con;
private QueueSession session;
private QueueSender sender;
private static InitialContext ctx;

private static JAXBContext jaxb;
private static IBwLogContext logContext = BwLogContextFactory.create();

static{
    try {
        ctx = new InitialContext();
        queue = (Queue) ctx.lookup("jms/sampleJNDI1");
        qcf = (QueueConnectionFactory) ctx.lookup("jms/sampleJNDI2");

JMS configuration is server for jms/sampleJNDI2

enter image description here

Now how do I connect to same xxx,yyy,zzz without server. Can we put these details in properties file and use them. If so, how to do it or is there any other way. I have hard coded queue name copying from the server. But stuck with ConnectionFactory.

Thanks in advance

1
What does WAS have to do with ActiveMQ here? - Justin Bertram

1 Answers

1
votes

You seem to be using the class ActiveMQConnectionFactory. This classname is used by both ActiveMQ 5.x and ActiveMQ Artemis, but with different package names. For better or worse, though, the basic configuration is the same for both:

ActiveMQConnectionFactory qcf = new ActiveMQConnectionFactory
      ("tcp://" + host + ":" + port);
QueueConnection qc = qcf.createQueueConnection ("user", "password");

There are other properties you can set on the connection factory, but host, port, user, and password are probably the most used.

Both ActiveMQ 5.x and ActiveMQ Artemis support other protocols than their proprietary ones (OpenWire and Artemis Core, respectively). For example, you can use the Qpid JMS runtime library to connect using the AMQP protocol. This has a different means of configuration, as do all the other client runtime libraries you might use.

Here is one way to set up a connection factory for IBM MQ, using IBM's "Class for Java" runtime.

import com.ibm.msg.client.wmq.*;
import com.ibm.mq.jms.*;
MQQueueConnectionFactory qcf = new MQQueueConnectionFactory();
qcf.setHostName ("my_mq.acme.com");
qcf.setPort (1414);
qcf.setQueueManager ("QMA");
qcf.setChannel ("SYSTEM.DEF.SVRCONN");
qcf.setTransportType (WMQConstants.WMQ_CM_CLIENT);
QueueConnection qc = qcf.createQueueConnection ("user", "password");

And here is an example for Qpid-JMS, that provides AMQP wire protocol support for many message brokers:

QueueConnectionFactory qcf = new 
  org.apache.qpid.jms.JmsConnectionFactory 
  ("amqp://" + host + ":" + port);
QueueConnection qc = qcf.createQueueConnection ("user", "password");

I'm not sure why, but using a JMS client runtime without JNDI is often not well-documented. It's always possible, in my experience, but sometimes a bit of digging is required, to find the relevant settings.