4
votes

I need to write a Java client listening to WebSphere MQ Server. Message is put into a queue in the server.

I developed this code, but am not sure it is correct or not. If correct, then how can I test it?

This is a standalone Java project, no application server support. Which jars I should put into classpath?

I have the MQ settings, where I should put into my codes? Standard JMS can skip these settings? confusing ....

import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    Context jndiContext = null;
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue controlQueue = null;
    QueueReceiver queueReceiver = null;

    private String queueSubject = "";

    private void start() {
        try {
            queueConnection.start();

            queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = queueSession.createQueue(queueSubject);
            MessageConsumer consumer = queueSession.createConsumer(destination);
            consumer.setMessageListener(new MyListener());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void close() {
        try {
            queueSession.close();
            queueConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        try {
            jndiContext = new InitialContext();
            queueConnectionFactory = (QueueConnectionFactory) this.jndiLookup("QueueConnectionFactory");
            queueConnection = queueConnectionFactory.createQueueConnection();
            queueConnection.start();
        } catch (Exception e) {
            System.err.println("Could not create JNDI API " + "context: " + e.toString());
            System.exit(1);
        }
    }

    private class MyListener implements MessageListener {
        @Override
        public void onMessage(Message message) {
            System.out.println("get message:" + message);
        }
    }

    private Object jndiLookup(String name) throws NamingException {
        Object obj = null;

        if (jndiContext == null) {
            try {
                jndiContext = new InitialContext();
            } catch (NamingException e) {
                System.err.println("Could not create JNDI API " + "context: " + e.toString());
                throw e;
            }
        }
        try {
            obj = jndiContext.lookup(name);
        } catch (NamingException e) {
            System.err.println("JNDI API lookup failed: " + e.toString());
            throw e;
        }
        return obj;
    }

    public Main() {

    }

    public static void main(String[] args) {
        new Main();

    }
}

MQ Queue setting

     <queue-manager>
         <name>AAA</name>
         <port>1423</port>
         <hostname>ddd</hostname>
        <clientChannel>EEE.CLIENTS.00</clientChannel>
        <securityClass>PKIJCExit</securityClass>
         <transportType>1</transportType>
        <targetClientMatching>1</targetClientMatching>
 </queue-manager>
 <queues>
     <queue-details id="queue-1">
        <name>GGGG.NY.00</name>
        <transacted>false</transacted>
        <acknowledgeMode>1</acknowledgeMode>
        <targetClient>1</targetClient>
     </queue-details>
</queues>
3

3 Answers

3
votes

There is an article with sample code Running a standalone Java application on WebSphere MQ V6.0 which walks you through most of your questions, including how you might test with a free WMQ trial install. The main difference (as discussed in the comments) to v7 or v7.1 is that there is no broker component to start if you want to use topics. Other than that, the article should work fine with the current WMQ client and/or server.

In addition, please refer to the WebSphere MQ Using Java manual (v7.0 client) or the Using WebSphere MQ Classes for Java manual (v7.1 client) for the appropriate CLASSPATH and other settings for your client. Remember to use the Infocenter appropriate to your client version and not to the server version. You can mix and match client and server version but you get only the features supported by the server. For example, using the WMQ v7.1 client with the WMQ v7.0 server is perfectly valid.

Finally, there are a number of sample programs supplied with the free client download that do exactly what you are describing. Some use JNDI to lookup the WMQ resources, others use Java methods and can be populated with standard Java property files. The ones with a -nojndi option show how to initialize your WMQ objects in the code at run time. These are under

[WMQ Install path]\tools\wmqjava\samples

...in the latest Windows client install (SupportPac MQC71). You can also use the v7.0 client (SupportPac MQC7). I would recommend using the samples to get started rather than starting from scratch. Why reinvent the wheel, right?

In addition to the many sample programs, the vendor install includes all of the requisite jar files. Note that what goes in the CLASSPATH changes by WMQ client version so refer to the Infocenter. The later versions are much simpler and require only a couple of jar files in the CLASSPATH.

If you want to download the WMQ trial for testing and do not have Administrator rights on your Windows workstation, you can install it easily on a RedHat or SUSE virtual machine. With a bit of massaging you can also easily install on Ubuntu as described in an Andy Piper blog post.

1
votes

If you have the option, I would recommend that you introduce the Spring Framework to handle the JMS communication. That way you only need to write your business logic and can leave the error handling up to Spring.

Spring JMS

Download the latest Spring JARS and look at configuring a DefaultMessageListenerContainer for your application. You will then write your own POJO (plain old java object) with an onMessage() event that gets called everytime a new message arrives.

I found this tutorial that you may find useful to start with

0
votes

There is a small client application that I developed in JavaFx2 for windows and Mac osx. It's available on source forge (https://sourceforge.net/projects/mqconsole).

You can see an example of listening to a queue for new messages.

The program lists the queues, the messages in each queue, view the details of the message and send a message to a queue and listen to the response.

You can check out the code, use it and improve it.