@Override public void onMessage(Message message) { if (!resendPhase) { unackMessages.add(message); if (unackMessages.size() == MESSAGE_COUNT) { try { rollbackTx(); resendPhase = true; } catch (Exception e) { e.printStackTrace(); } } } else { ackMessages.add(message); if (ackMessages.size() == MESSAGE_COUNT) { try { commitTx(); } catch (Exception e) { e.printStackTrace(); } } } }
/** * Sends a batch of messages and validates that the rollbacked message was * not consumed. * * @throws Exception */ public void testSendReceiveWithPrefetchOne() throws Exception { setPrefetchToOne(); Message[] outbound = new Message[] {session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message"), session.createTextMessage("Fourth Message")}; beginTx(); for (int i = 0; i < outbound.length; i++) { // sends a message producer.send(outbound[i]); } commitTx(); // receives the first message beginTx(); for (int i = 0; i < outbound.length; i++) { LOG.info("About to consume message 1"); Message message = consumer.receive(1000); assertNotNull(message); LOG.info("Received: " + message); } // validates that the rollbacked was not consumed commitTx(); }
/** * spec section 3.6 acking a message with automation acks has no effect. * @throws Exception */ public void testAckMessageInTx() throws Exception { Message[] outbound = new Message[] {session.createTextMessage("First Message")}; // sends a message beginTx(); producer.send(outbound[0]); outbound[0].acknowledge(); commitTx(); outbound[0].acknowledge(); // receives the first message beginTx(); ArrayList<Message> messages = new ArrayList<Message>(); LOG.info("About to consume message 1"); Message message = consumer.receive(1000); messages.add(message); LOG.info("Received: " + message); // validates that the rollbacked was not consumed commitTx(); Message inbound[] = new Message[messages.size()]; messages.toArray(inbound); assertTextMessagesEqual("Message not delivered.", outbound, inbound); }
/** * Sends a batch of messages and validates that the messages are received. * * @throws Exception */ public void testSendReceiveTransactedBatches() throws Exception { TextMessage message = session.createTextMessage("Batch Message"); for (int j = 0; j < batchCount; j++) { LOG.info("Producing bacth " + j + " of " + batchSize + " messages"); beginTx(); for (int i = 0; i < batchSize; i++) { producer.send(message); } messageSent(); commitTx(); LOG.info("Consuming bacth " + j + " of " + batchSize + " messages"); beginTx(); for (int i = 0; i < batchSize; i++) { message = (TextMessage)consumer.receive(1000 * 5); assertNotNull("Received only " + i + " messages in batch " + j, message); assertEquals("Batch Message", message.getText()); } commitTx(); } }
public void testMessageListener() throws Exception { // send messages for (int i = 0; i < MESSAGE_COUNT; i++) { producer.send(session.createTextMessage(MESSAGE_TEXT + i)); } commitTx(); consumer.setMessageListener(this); // wait receive waitReceiveUnack(); assertEquals(unackMessages.size(), MESSAGE_COUNT); // resend phase waitReceiveAck(); assertEquals(ackMessages.size(), MESSAGE_COUNT); // should no longer re-receive consumer.setMessageListener(null); assertNull(consumer.receive(500)); reconnect(); }
public void testChangeMutableObjectInObjectMessageThenRollback() throws Exception { ArrayList<String> list = new ArrayList<String>(); list.add("First"); Message outbound = session.createObjectMessage(list); outbound.setStringProperty("foo", "abc"); beginTx(); producer.send(outbound); commitTx(); LOG.info("About to consume message 1"); beginTx(); Message message = consumer.receive(5000); List<String> body = assertReceivedObjectMessageWithListBody(message); // now lets try mutate it try { message.setStringProperty("foo", "def"); fail("Cannot change properties of the object!"); } catch (JMSException e) { LOG.info("Caught expected exception: " + e, e); } body.clear(); body.add("This should never be seen!"); rollbackTx(); beginTx(); message = consumer.receive(5000); List<String> secondBody = assertReceivedObjectMessageWithListBody(message); assertNotSame("Second call should return a different body", secondBody, body); commitTx(); }