Search code examples
javajmsmq

MQ JMS simple download message


I am trying to implement a simple client that downloads messages from a MQ server. The MQ server already exists, and I have no access to it. In all examples that I found online, it is used the "createQueue()" method, as such:

  MQQueueConnectionFactory cf = new MQQueueConnectionFactory();

  // Config
  cf.setHostName("localhost");
  cf.setPort(1414);
  cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
  cf.setQueueManager("QM_thinkpad");
  cf.setChannel("SYSTEM.DEF.SVRCONN");

  MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
  MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
  MQQueueSender sender =  (MQQueueSender) session.createSender(queue);
  MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
  ...
  JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);

However, in my case I don't want to create a Queue, I just want to use an existing remote queue. Or I am wrong? What should I do?


Solution

  • createQueue() does not actually create an MQ Queue. It just creates an identifier for the queue , which can be used for subsequent calls, such as, in your case, createReceiver.