Java client listening to WebSphere MQ Server?

Posted by user595234 on Stack Overflow See other posts from Stack Overflow or by user595234
Published on 2011-12-06T17:15:14Z Indexed on 2013/11/03 9:54 UTC
Read the original article Hit count: 261

Filed under:
|
|

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>

© Stack Overflow or respective owner

Related posts about java

Related posts about jms