Java-client-listening-WebSphere MQ-Server?

Schreiben brauche ich einen Java-client hören WebSphere MQ Server. Nachricht in einer Warteschlange auf dem server.

Entwickelte ich diesen code, aber ich bin mir nicht sicher, ob es richtig ist oder nicht. Wenn das stimmt, dann wie kann ich es testen?

Dies ist eine standalone-Java-Projekt, kein application-server-Unterstützung. Welche Gläser sollte ich in classpath?

Ich habe die MQ-Einstellungen, wo ich in meinem Code? Standard JMS überspringen können diese Einstellungen? verwirrend ....

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-Einstellung

     <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>

InformationsquelleAutor user595234 | 2011-12-06

Schreibe einen Kommentar