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(); }
private void waitReceiveAck() throws Exception { for (int i = 0; i < 100 && ackMessages.size() < MESSAGE_COUNT; i++) { Thread.sleep(100); } assertFalse(ackMessages.size() < MESSAGE_COUNT); } }
/** * 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(); } }
@SuppressWarnings("unchecked") protected List<String> assertReceivedObjectMessageWithListBody(Message message) throws JMSException { assertNotNull("Should have received a message!", message); assertEquals("foo header", "abc", message.getStringProperty("foo")); assertTrue("Should be an object message but was: " + message, message instanceof ObjectMessage); ObjectMessage objectMessage = (ObjectMessage)message; List<String> body = (List<String>)objectMessage.getObject(); LOG.info("Received body: " + body); assertEquals("Size of list should be 1", 1, body.size()); assertEquals("element 0 of list", "First", body.get(0)); return body; }
beginTx(); while (consumer.receive(1000) != null) { commitTx(); beginTx(); producer.send(outbound[0]); producer.send(outbound[1]); commitTx(); beginTx(); Message message = consumer.receive(1000); messages.add(message); assertEquals(outbound[0], message); commitTx(); beginTx(); message = consumer.receive(1000); assertNotNull(message); assertEquals(outbound[1], message); rollbackTx(); beginTx(); message = consumer.receive(5000); assertNotNull("Should have re-received the message again!", message); messages.add(message); commitTx(); assertTextMessagesEqual("Rollback did not work", outbound, inbound);
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(); }
/** * 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(); }
beginTx(); producer.send(outbound[0]); commitTx(); beginTx(); producer.send(session.createTextMessage("I'm going to get rolled back.")); rollbackTx(); beginTx(); producer.send(outbound[1]); commitTx(); beginTx(); ArrayList<Message> messages = new ArrayList<Message>(); LOG.info("About to consume message 1"); commitTx(); Message inbound[] = new Message[messages.size()]; messages.toArray(inbound); assertTextMessagesEqual("Rollback did not work.", outbound, inbound);
beginTx(); producer.send(outbound[0]); commitTx(); beginTx(); producer.send(session.createTextMessage("I'm going to get rolled back.")); consumer.close(); session.close(); reconnect(); beginTx(); producer.send(outbound[1]); commitTx(); beginTx(); Message message = consumer.receive(1000); messages.add(message); commitTx(); Message inbound[] = new Message[messages.size()]; messages.toArray(inbound); assertTextMessagesEqual("Rollback did not work.", outbound, inbound);
beginTx(); producer.send(outbound[0]); commitTx(); beginTx(); producer.send(session.createTextMessage("I'm going to get rolled back.")); consumer.close(); reconnectSession(); commitTx(); beginTx(); Message message = consumer.receive(1000); messages.add(message); commitTx(); Message inbound[] = new Message[messages.size()]; messages.toArray(inbound); assertTextMessagesEqual("Rollback did not work.", outbound, inbound);
/** * 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); }
beginTx(); while (consumer.receiveNoWait() != null) { commitTx(); beginTx(); producer.send(outbound[0]); producer.send(outbound[1]); commitTx(); LOG.info("Sent 0: " + outbound[0]); LOG.info("Sent 1: " + outbound[1]); beginTx(); TextMessage message = (TextMessage)consumer.receive(1000); assertEquals(outbound[0].getText(), message.getText()); commitTx(); LOG.info("Created consumer: " + consumer); beginTx(); message = (TextMessage)consumer.receive(1000); assertEquals(outbound[1].getText(), message.getText()); commitTx();
@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(); } } } }
private void waitReceiveUnack() throws Exception { for (int i = 0; i < 100 && !resendPhase; i++) { Thread.sleep(100); } assertTrue(resendPhase); }
beginTx(); while (consumer.receive(1000) != null) { commitTx(); beginTx(); producer.send(outbound[0]); producer.send(outbound[1]); commitTx(); beginTx(); Message message = consumer.receive(1000); assertEquals(outbound[0], message); assertNotNull(message); assertEquals(outbound[1], message); rollbackTx(); beginTx(); message = consumer.receive(5000); assertNotNull("Should have re-received the first message again!", message); messages.add(message); assertEquals(outbound[0], message); message = consumer.receive(5000); assertNotNull("Should have re-received the second message again!", message); messages.add(message); assertEquals(outbound[1], message); assertNull(consumer.receiveNoWait());