/** * test messages are acknowledged and recovered properly * * @throws Exception */ public void testClientAcknowledge() throws Exception { Destination destination = createDestination(getClass().getName()); Connection connection = createConnection(); connection.setClientID(idGen.generateId()); connection.start(); Session consumerSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consumer = consumerSession.createConsumer(destination); Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = producerSession.createProducer(destination); producer.setDeliveryMode(deliveryMode); // send some messages TextMessage sent1 = producerSession.createTextMessage(); sent1.setText("msg1"); sent1.setStringProperty("str", "1"); producer.send(sent1); TextMessage sent2 = producerSession.createTextMessage(); sent2.setText("msg2"); sent2.setStringProperty("str", "2"); producer.send(sent2); TextMessage sent3 = producerSession.createTextMessage(); sent2.setText("msg3"); sent2.setStringProperty("str", "3"); producer.send(sent3); TextMessage msgTest = (TextMessage) consumer.receive(RECEIVE_TIMEOUT); System.out.println("msgTest::" + msgTest + " // " + msgTest.getText()); TextMessage rec2 = (TextMessage) consumer.receive(RECEIVE_TIMEOUT); System.out.println("msgTest::" + rec2 + " // " + rec2.getText()); assertNull(consumer.receiveNoWait()); // ack rec2 rec2.acknowledge(); TextMessage sent4 = producerSession.createTextMessage(); sent4.setText("msg4"); producer.send(sent4); TextMessage rec4 = (TextMessage) consumer.receive(RECEIVE_TIMEOUT); assertTrue(rec4.equals(sent4)); consumerSession.recover(); rec4 = (TextMessage) consumer.receive(RECEIVE_TIMEOUT); assertTrue(rec4.equals(sent4)); assertTrue(rec4.getJMSRedelivered()); rec4.acknowledge(); connection.close(); }
/** * Receive a message from destination with timeout. * * @param destinationName destinationName * @param timeout timeout * @return message */ public String receiveTextMessageFromDestinationWithTimeout( final String destinationName, final int timeout) { if (!this.isConnected()) { throw new JmsNotConnectedException("Not connected"); } MessageConsumer consumer = getConsumer(destinationName); TextMessage message; try { if (timeout == 0) { message = (TextMessage) consumer.receiveNoWait(); } else { message = (TextMessage) consumer.receive(timeout); } if (message != null) { if (acknowledgeMode == Session.CLIENT_ACKNOWLEDGE) { message.acknowledge(); } return message.getText(); } else { return null; } } catch (JMSException e) { throw new IllegalStateException("Unable to receive message from " + destinationName, e); } }
public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtMessage = (TextMessage) message; String[] results = txtMessage.getText().split("\n"); System.out.println("\nMessage received: string 1 = " + results[0]); System.out.println("Message received: string 2 = " + results[1]); txtMessage.acknowledge(); } else { System.out.println("Invalid message received."); } } catch (JMSException e) { System.out.println("Caught:" + e); e.printStackTrace(); } }
/** manually say OK I'm ready to take the message on the client */ public void doReceiveAck() { if (doSetup()) { try { queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE); QueueReceiver queueReceiver = queueSession.createReceiver(queue); queueConnection.start(); String info = null; while (true) { Message m = queueReceiver.receive(1); if (m != null) { if (m instanceof TextMessage) { TextMessage message = (TextMessage) m; info = message.getText(); if (info.indexOf("12") > -1) { log.debug("Skipping message:==> " + message.getText()); } else { log.debug("Reading message:==> " + message.getText()); message.acknowledge(); } } else { break; } } } } catch (JMSException e) { log.error("Listen Exception occurred: " + e.toString()); } finally { doCleanup(); } } }
@Override public boolean runExample() throws Exception { final int numMessages = 30; Connection connection = null; InitialContext initialContext = null; try { // Step 1. Get an initial context for looking up JNDI from the server #1 initialContext = new InitialContext(); // Step 2. Look up the JMS resources from JNDI Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); // Step 3. Create a JMS Connection connection = connectionFactory.createConnection(); // Step 4. Create a *non-transacted* JMS Session with client acknowledgement Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); // Step 5. Start the connection to ensure delivery occurs connection.start(); // Step 6. Create a JMS MessageProducer and a MessageConsumer MessageProducer producer = session.createProducer(queue); MessageConsumer consumer = session.createConsumer(queue); // Step 7. Send some messages to server #1, the live server for (int i = 0; i < numMessages; i++) { TextMessage message = session.createTextMessage("This is text message " + i); producer.send(message); System.out.println("Sent message: " + message.getText()); } // Step 8. Receive and acknowledge a third of the sent messages TextMessage message0 = null; for (int i = 0; i < numMessages / 3; i++) { message0 = (TextMessage) consumer.receive(5000); System.out.println("Got message: " + message0.getText()); } message0.acknowledge(); // Step 9. Receive the rest third of the sent messages but *do not* acknowledge them yet for (int i = numMessages / 3; i < numMessages; i++) { message0 = (TextMessage) consumer.receive(5000); System.out.println("Got message: " + message0.getText()); } // Step 10. Crash server #1, the live server, and wait a little while to make sure // it has really crashed Thread.sleep(5000); killServer(0); // Step 11. Acknowledging the 2nd half of the sent messages will fail as failover to the // backup server has occurred try { message0.acknowledge(); } catch (JMSException e) { System.out.println( "Got (the expected) exception while acknowledging message: " + e.getMessage()); } // Step 12. Consume again the 2nd third of the messages again. Note that they are not // considered as redelivered. for (int i = numMessages / 3; i < (numMessages / 3) * 2; i++) { message0 = (TextMessage) consumer.receive(5000); System.out.printf( "Got message: %s (redelivered?: %s)\n", message0.getText(), message0.getJMSRedelivered()); } message0.acknowledge(); reStartServer(0, 10000); Thread.sleep(10000); // Step 11. Acknowledging the 2nd half of the sent messages will fail as failover to the // backup server has occurred try { message0.acknowledge(); } catch (JMSException e) { System.err.println("Got exception while acknowledging message: " + e.getMessage()); } // Step 12. Consume again the 2nd third of the messages again. Note that they are not // considered as redelivered. for (int i = (numMessages / 3) * 2; i < numMessages; i++) { message0 = (TextMessage) consumer.receive(5000); System.out.printf( "Got message: %s (redelivered?: %s)\n", message0.getText(), message0.getJMSRedelivered()); } message0.acknowledge(); return true; } finally { // Step 13. Be sure to close our resources! if (connection != null) { connection.close(); } if (initialContext != null) { initialContext.close(); } } }