Unable to see JMS messages in WebSphere MQ queue

I am trying to put some messages from JMS code into a local queue defined in the local queue manager. I have defined a local Queue LQ

in WebSphere MQ and using JMS code to post messages. I am doing it here. I do not see messages on the WebSphere queue.

Here is the code:

try {
    MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
    //set up the Connection Configuration values

    // Set the properties
      cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "HostName");
      cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
      cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN");
      cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
      cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "TEST.JMS");

    //JNDI is Not used Here
    MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();

    MQQueueSession session = (MQQueueSession) connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
    MQQueue queue = (MQQueue) session.createQueue("queue:///LQ");
    MQQueueSender sender = (MQQueueSender) session.createSender(queue);
    MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);

    long uniqueNumber = System.currentTimeMillis() % 1000;

    JMSTextMessage message = (JMSTextMessage) session.createTextMessage("MQJMSTest "+ uniqueNumber);  
    //MQMessage message =  (MQMessage) session.createTextMessage("MQJMSTest "+ uniqueNumber);


    // Start the connection
    connection.start();

    sender.send(message);
    System.out.println("Sent message:\\n" + message);

    //JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
    // System.out.println("\\nReceived message:\\n" + receivedMessage);

    sender.close();
    receiver.close();
    session.close();
    connection.stop();
    connection.close();

    System.out.println("\\nSUCCESS\\n");

} catch (JMSException e) {
    // TODO Auto-generated catch block
    //e.printStackTrace();
    System.out.println(e);
    System.out.println("\\nFAILURE\\n");
}
  catch (Exception e){
      System.out.println(e);
      System.out.println("\\nFAILURE\\n");
} 

      

+3


source to share


1 answer


According to this, you created a transaction session:

    connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);

      

According to the docs ,

When an application closes a transactional session, an implicit rollback takes place. When the application closes the connection, an implicit rollback occurs for all sessions associated with the connection.



Rollback will remove any messages in the queue before they become visible outside of the application that issues them. So, I think you can COMMIT the transaction, huh?

    sender.send(message);

    // ADD THIS HERE ===========================
    session.commit();
    // ADD THIS HERE ===========================

    System.out.println("Sent message:\\n" + message);

    // THE COMMIT MUST HAVE OCCURRED BEFORE YOU CAN GET THE MESSAGE
    // BACK OUT OF THE QUEUE IN THE NEXT LINES

    //JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
    // System.out.println("\\nReceived message:\\n" + receivedMessage);

    // ===================================================
    // DON'T FORGET TO COMMIT THE MESSAGE ON THE WAY BACK OUT OR IT WILL
    // JUST GE TPUT BACK ON THE QUEUE AGAIN.
    // session.commit();
    // ===================================================


    sender.close();
    receiver.close();
    session.close();

      

PS: see JMS Exception Handling . JMS Exception is a layered framework where vendor-specific diagnostics are expected to be placed in lower-level elements. Only by printing these lower level elements can one know what the transport layer thinks is wrong.

+3


source







All Articles