MessageConsumer.receive receiving messages even with server down
tyler_durden Apr 21, 2011 7:57 AMHello everyone. I'm a bit lost on this.
First I'll post the snippet, which is basically taken from the reattach-node example:
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = getContext(0);
// Step 2. Perform a lookup on the queue
Queue queue = (Queue)initialContext.lookup("/queue/ExampleQueue");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
// Step 4. Create a JMS Connection
Connection = cf.createConnection();
// Step 5. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
// Step 7. Create a Text Message
TextMessage message = session.createTextMessage("This is a text message");
System.out.println("Sent message: " + message.getText());
// Step 8. Send the Message
producer.send(message);
// Step 9. Create a JMS Message Consumer
MessageConsumer messageConsumer = session.createConsumer(queue);
// Step 10. Start the Connection
Connection.start();
// Step 11. To simulate a temporary problem on the network, we stop the remoting acceptor on the
// server which will close all connections
stopAcceptor(initialContext);
System.out.println("Acceptor now stopped, will wait for 5 seconds. This simulates the network connection failing for a while");
// Step 12. Wait a while then restart the acceptor
Thread.sleep(5000);
System.out.println("Acceptor now stopped, trying to read before restarting");
TextMessage messageReceived2 = (TextMessage)messageConsumer.receive(10000);
if(messageReceived2!=null){
System.out.println("Received message: " + messageReceived2.getText());
}
System.out.println("Re-starting acceptor");
startAcceptor(initialContext);
System.out.println("Restarted acceptor. The client will now reconnect in 10 sec.");
// Step 12.1 Wait a while then restart the acceptor
Thread.sleep(10000);
// Step 13. We receive the message
TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived.getText());
return true;
Contrary to what I was expecting, the receive() before the restart is reading and removing the message from the server, and ofc the following receive is timing out, resulting in a nullpointerexception on the System.out.
Why is the message received between the stop and startAcceptor?
Here is the log:
21-apr-2011 13.24.48 jmsproducertester.HornetQExample run
INFO: hornetq.example.runServer is true
Sent message: This is a text message
Acceptor now stopped, will wait for 5 seconds. This simulates the network connection failing for a while
Acceptor now stopped, trying to read before restarting
Received message: This is a text message
Re-starting acceptor
Restarted acceptor. The client will now reconnect in 10 sec.
java.lang.NullPointerException
example complete
at jmsproducertester.ReattachExample.runExample(ReattachExample.java:103)
at jmsproducertester.HornetQExample.run(HornetQExample.java:60)
at jmsproducertester.ReattachExample.main(ReattachExample.java:28)
#####################
### FAILURE! ###
#####################
Java Result: 1