3
votes

I am trying to put some messages from a JMS code to a local queue defined in local queue manager. I have defined a local Queue LQ in WebSphere MQ and using JMS code to put messages. Am I doing it proper here. I am not seeing the messages in 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");
} 
1

1 Answers

3
votes

According to this, you've created a transacted session:

    connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);

According to the docs,

When an application closes a transacted session, an implicit rollback occurs. When an application closes a connection, an implicit rollback occurs for all the connection's transacted sessions.

A rollback would remove any messages on the queue before they ever became visible outside the application that PUT them. So I'm thinking you might want to COMMIT the transaction, yes?

    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: Please see the section on JMS Exception handling. The JMS Exception is a multi-tiered structure in which it is expected that vendor-specific diagnostics are placed in the lower-level elements. Only by printing those lower-level elements is it possible to know what the transport layer thinks is wrong.