1
votes

I have a java program I run to write messages to Mid-Tier IBM MQ's to test functionality before attaching our main programs to them. The write method looks like the following below:

  private static void sendSingleMessage(ConnectionFactory connectionFactory,
                                      String[] messages, String destination) throws Exception {
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        for (String payload : messages) {
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(destination);
            MessageProducer producer = session.createProducer(queue);
            Message msg = session.createTextMessage(payload);
            System.out.println("Sending text '" + payload + "'");
            producer.send(msg);
            session.close();
            System.out.println("Message sent");
        }
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

The connectionFactory is setup before this method executes, but within that method I set the MQConncetionFactory properties(host,port,channel,queuemanager, etc...) This send method works and I can see the queue depth increasing on my IBM MQ Explorer when I call it from my main method.

When I run a similar readSingleMessage method, the code gets stuck on the consumer.receive() and never finishes executing. See below:

   private static void readSingleMessage(ConnectionFactory connectionFactory,
                                      String[] messages, String destination) throws Exception {
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        for (String payload : messages) {
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(destination);
            MessageConsumer consumer = session.createConsumer(queue);
            System.out.println("Recieving text '" + payload + "'");
            consumer.receive();
            session.close();
            System.out.println("Received message");
        }
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

Is there anyway I can further debug this, or find why I am able to write to the queue, but unable to read a message off of it?

1

1 Answers

2
votes

You have to start the JMS Connection by calling the start() method on it. You cannot receive any messages until the connection is started. This is noted in the JMS Specification and Javadoc.

As an aside, if you use the JMS 2.0 "simplified" API and create a JMSContext object (an object which is essentially a combined Connection and Session) you do not need to call start to receive messages. A consumer crated from it can be used to receive messages without being explicitly started.