title: Java client listening to WebSphere MQ Server?
body: <p>I need to write a Java client listening to WebSphere MQ Server. Message is put into a queue in the server. </p>
  
  <p>I developed this code, but am not sure it is correct or not. If correct, then how can I test it? </p>
  
  <p>This is a standalone Java project, no application server support. Which jars I should put into classpath? </p>
  
  <p>I have the MQ settings, where I should put into my codes? Standard JMS can skip these settings? confusing ....</p>
  
  <pre><code>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();
  
      }
  }
  </code></pre>
  
  <p>MQ  Queue setting</p>
  
  <pre><code>     &lt;queue-manager&gt;
           &lt;name&gt;AAA&lt;/name&gt;
           &lt;port&gt;1423&lt;/port&gt;
           &lt;hostname&gt;ddd&lt;/hostname&gt;
          &lt;clientChannel&gt;EEE.CLIENTS.00&lt;/clientChannel&gt;
          &lt;securityClass&gt;PKIJCExit&lt;/securityClass&gt;
           &lt;transportType&gt;1&lt;/transportType&gt;
          &lt;targetClientMatching&gt;1&lt;/targetClientMatching&gt;
   &lt;/queue-manager&gt;
   &lt;queues&gt;
       &lt;queue-details id="queue-1"&gt;
          &lt;name&gt;GGGG.NY.00&lt;/name&gt;
          &lt;transacted&gt;false&lt;/transacted&gt;
          &lt;acknowledgeMode&gt;1&lt;/acknowledgeMode&gt;
          &lt;targetClient&gt;1&lt;/targetClient&gt;
       &lt;/queue-details&gt;
  &lt;/queues&gt;
  </code></pre>