Java Code Examples for javax.jms.TextMessage#acknowledge()

The following examples show how to use javax.jms.TextMessage#acknowledge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DivertAndACKClientTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientACK() throws Exception {
   Queue queueSource = createQueue("Source");
   Queue queueTarget = createQueue("Dest");

   Connection connection = cf.createConnection();
   Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

   final MessageProducer producer = session.createProducer(queueSource);

   final TextMessage message = session.createTextMessage("message text");
   producer.send(message);

   connection.start();

   final MessageConsumer consumer = session.createConsumer(queueTarget);
   TextMessage receivedMessage = (TextMessage) consumer.receive(1000);
   Assert.assertNotNull(receivedMessage);
   receivedMessage.acknowledge();

   connection.close();
}
 
Example 2
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionalSimple() throws Exception {
   try (Connection connection = factory.createConnection()) {

      Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
      Queue queue = session.createQueue(queueName);
      MessageProducer producer = session.createProducer(queue);
      MessageConsumer consumer = session.createConsumer(queue);
      producer.send(session.createTextMessage("test"));
      session.commit();

      Assert.assertNull(consumer.receive(100));
      connection.start();

      TextMessage message = (TextMessage) consumer.receive(5000);
      Assert.assertEquals("test", message.getText());

      Assert.assertNotNull(message);

      message.acknowledge();
   }
}
 
Example 3
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendEmpty() throws Exception {
   try (Connection connection = factory.createConnection()) {

      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = session.createQueue(queueName);
      MessageProducer producer = session.createProducer(queue);
      MessageConsumer consumer = session.createConsumer(queue);
      producer.send(session.createTextMessage());

      Assert.assertNull(consumer.receive(100));
      connection.start();

      TextMessage message = (TextMessage) consumer.receive(5000);

      Assert.assertNotNull(message);

      message.acknowledge();
   }
}
 
Example 4
Source File: GroupingTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private int flushMessages(MessageConsumer consumer) throws JMSException {
   int received = 0;
   while (true) {
      TextMessage msg = (TextMessage) consumer.receiveNoWait();
      if (msg == null) {
         break;
      }
      msg.acknowledge();
      received++;
   }
   return received;
}
 
Example 5
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientACK() throws Exception {
   try {

      Connection connection = factory.createConnection();

      Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      Queue queue = session.createQueue(queueName);
      MessageProducer producer = session.createProducer(queue);
      MessageConsumer consumer = session.createConsumer(queue);
      producer.send(session.createTextMessage("test"));

      Assert.assertNull(consumer.receive(100));
      connection.start();

      TextMessage message = (TextMessage) consumer.receive(5000);

      Assert.assertNotNull(message);

      message.acknowledge();

      connection.close();

      System.err.println("Done!!!");
   } catch (Throwable e) {
      e.printStackTrace();
   }
}
 
Example 6
Source File: QReceiverListenerClient.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onMessage(Message message) {
	try {
		TextMessage textMessage = (TextMessage) message;
		System.out.println("消息内容:" + textMessage.getText() + ",是否重发:" + textMessage.getJMSRedelivered());
		textMessage.acknowledge();
		throw new RuntimeException("test");
	} catch (JMSException e) {
		e.printStackTrace();
	}
}
 
Example 7
Source File: JMSTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testClientAcknowledge() throws Exception {
   conn = createConnection();

   Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
   MessageProducer p = session.createProducer(queue1);
   p.send(session.createTextMessage("CLACK"));

   MessageConsumer cons = session.createConsumer(queue1);

   conn.start();

   TextMessage m = (TextMessage) cons.receive(1000);

   ProxyAssertSupport.assertEquals("CLACK", m.getText());

   // make sure the message is still in "delivering" state
   assertRemainingMessages(1);

   m.acknowledge();

   assertRemainingMessages(0);
}
 
Example 8
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private void doAcknowledgePreviouslyRecoveredClientAckMessagesTestImpl(boolean closeConsumer, boolean closeSession, boolean consumeAllRecovered) throws JMSException, Exception, IOException {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer, false, "?jms.clientID=myClientId", null, null, false);
        connection.start();

        int msgCount = 7;
        int deliverBeforeRecoverCount = 4;
        int acknowledgeAfterRecoverCount = consumeAllRecovered ? 5 : 2;

        testPeer.expectBegin();

        Session session = connection.createSession(Session.CLIENT_ACKNOWLEDGE);

        String topicName = "myTopic";
        Topic topic = session.createTopic(topicName);

        final CountDownLatch incoming = new CountDownLatch(msgCount);
        ((JmsConnection) connection).addConnectionListener(new JmsDefaultConnectionListener() {

            @Override
            public void onInboundMessage(JmsInboundMessageDispatch envelope) {
                incoming.countDown();
            }
        });

        testPeer.expectReceiverAttach();

        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, new AmqpValueDescribedType("content"), msgCount, false, false,
                equalTo(UnsignedInteger.valueOf(JmsDefaultPrefetchPolicy.DEFAULT_QUEUE_PREFETCH)), 1, false, true);

        MessageConsumer consumer = session.createConsumer(topic);

        TextMessage receivedTextMessage = null;
        for (int i = 1; i <= deliverBeforeRecoverCount; i++) {
            assertNotNull("Expected message did not arrive: " + i, receivedTextMessage = (TextMessage) consumer.receive(3000));
            assertEquals("Unexpected delivery number", i,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);
        }

        // Await all incoming messages to arrive at consumer before we recover, ensure deterministic test behaviour.
        assertTrue("Messages did not arrive in a timely fashion", incoming.await(3, TimeUnit.SECONDS));

        session.recover();

        testPeer.waitForAllHandlersToComplete(1000);

        for (int i = 1; i <= acknowledgeAfterRecoverCount; i++) {
            assertNotNull("Expected message did not arrive after recover: " + i, receivedTextMessage = (TextMessage) consumer.receive(3000));
            assertEquals("Unexpected delivery number after recover", i,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);
            testPeer.expectDisposition(true, new AcceptedMatcher(), i, i);
            receivedTextMessage.acknowledge();
        }

        testPeer.waitForAllHandlersToComplete(1000);

        if(!consumeAllRecovered) {
            // Any message delivered+recovered before but not then delivered and acknowledged afterwards, will have
            // disposition sent as consumer/session/connection is closed.
            for (int i = acknowledgeAfterRecoverCount + 1; i <= deliverBeforeRecoverCount; i++) {
                testPeer.expectDisposition(true, new ModifiedMatcher().withDeliveryFailed(equalTo(true)), i, i);
            }
        }

        if(closeConsumer) {
            testPeer.expectDetach(true,  true,  true);

            // Dispositions sent by proton when the link is freed
            for (int i = Math.max(deliverBeforeRecoverCount, acknowledgeAfterRecoverCount) + 1; i <= msgCount; i++) {
                testPeer.expectDisposition(true, new ReleasedMatcher(), i, i);
            }

            consumer.close();
        }

        if(closeSession) {
            testPeer.expectEnd();

            session.close();
        }

        testPeer.expectClose();

        connection.close();
    }
}
 
Example 9
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testRecoveredClientAckSessionWithDurableSubscriber() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer, false, "?jms.clientID=myClientId", null, null, false);
        connection.start();

        testPeer.expectBegin();

        Session session = connection.createSession(Session.CLIENT_ACKNOWLEDGE);

        String subscriptionName = "mySubName";
        String topicName = "myTopic";
        Topic topic = session.createTopic(topicName);

        int msgCount = 3;
        testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, new AmqpValueDescribedType("content"), msgCount, false, false,
                Matchers.greaterThanOrEqualTo(UnsignedInteger.valueOf(msgCount)), 1, false, true);

        MessageConsumer subscriber = session.createDurableConsumer(topic, subscriptionName);

        TextMessage receivedTextMessage = null;
        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        assertEquals("Unexpected delivery number", 1,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);
        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        assertEquals("Unexpected delivery number", 2,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);

        session.recover();

        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        int deliveryNumber = receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1;
        assertEquals("Unexpected delivery number", 1,  deliveryNumber);

        testPeer.expectDisposition(true, new AcceptedMatcher(), 1, 1);

        receivedTextMessage.acknowledge();

        testPeer.expectDisposition(true, new ModifiedMatcher().withDeliveryFailed(equalTo(true)), 2, 2);
        testPeer.expectDetach(false, true, false);
        testPeer.expectDisposition(true, new ReleasedMatcher(), 3, 3);

        subscriber.close();

        testPeer.waitForAllHandlersToComplete(1000);

        testPeer.expectDurableSubUnsubscribeNullSourceLookup(false, false, subscriptionName, topicName, true);
        testPeer.expectDetach(true, true, true);

        session.unsubscribe(subscriptionName);

        testPeer.expectClose();

        connection.close();
    }
}
 
Example 10
Source File: AcknowledgementTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(final Message m) {
   try {
      count++;

      TextMessage tm = (TextMessage) m;

      if (count == 1) {
         assertRemainingMessages(3);
         if (!"a".equals(tm.getText())) {
            log.trace("Expected a but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
      }
      if (count == 2) {
         assertRemainingMessages(3);
         if (!"b".equals(tm.getText())) {
            log.trace("Expected b but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
      }
      if (count == 3) {
         assertRemainingMessages(3);
         if (!"c".equals(tm.getText())) {
            log.trace("Expected c but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
         log.trace("calling recover");
         sess.recover();
      }
      if (count == 4) {
         assertRemainingMessages(3);
         if (!"a".equals(tm.getText())) {
            log.trace("Expected a but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
         log.trace("*** calling acknowledge");
         tm.acknowledge();
         assertRemainingMessages(2);
         log.trace("calling recover");
         sess.recover();
      }
      if (count == 5) {
         assertRemainingMessages(2);
         if (!"b".equals(tm.getText())) {
            log.trace("Expected b but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
         log.trace("calling recover");
         sess.recover();
      }
      if (count == 6) {
         assertRemainingMessages(2);
         if (!"b".equals(tm.getText())) {
            log.trace("Expected b but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
      }
      if (count == 7) {
         assertRemainingMessages(2);
         if (!"c".equals(tm.getText())) {
            log.trace("Expected c but got " + tm.getText());
            failed = true;
            latch.countDown();
         }
         tm.acknowledge();
         assertRemainingMessages(0);
         latch.countDown();
      }

   } catch (Exception e) {
      log.error("Caught exception", e);
      failed = true;
      latch.countDown();
   }
}
 
Example 11
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(final Message m) {
   try {
      TextMessage tm = (TextMessage) m;

      messageOrder += tm.getText() + " ";
      if (count == 0) {
         if (!"a".equals(tm.getText())) {
            failed = true;
            latch.countDown();
         }
         if (transacted) {
            sess.rollback();
            messageOrder += "RB ";
         } else {
            messageOrder += "RC ";
            sess.recover();
         }
      }

      if (count == 1) {
         if (!"a".equals(tm.getText())) {
            failed = true;
            latch.countDown();
         }
         if (!tm.getJMSRedelivered()) {
            failed = true;
            latch.countDown();
         }
      }
      if (count == 2) {
         if (!"b".equals(tm.getText())) {
            failed = true;
            latch.countDown();
         }
      }
      if (count == 3) {
         if (!"c".equals(tm.getText())) {
            failed = true;
            latch.countDown();
         }
         if (transacted) {
            sess.commit();
         } else {
            tm.acknowledge();
         }
         latch.countDown();
      }
      count++;
   } catch (JMSException e) {
      failed = true;
      latch.countDown();
   }
}
 
Example 12
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRedel6() throws Exception {
   Connection conn = null;

   try {
      conn = createConnection();
      conn.start();

      Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      MessageProducer prod = sess.createProducer(queue1);
      TextMessage tm1 = sess.createTextMessage("hello1");
      TextMessage tm2 = sess.createTextMessage("hello2");
      TextMessage tm3 = sess.createTextMessage("hello3");
      prod.send(tm1);
      prod.send(tm2);
      prod.send(tm3);

      MessageConsumer cons1 = sess.createConsumer(queue1);

      TextMessage rm1 = (TextMessage) cons1.receive(1500);
      ProxyAssertSupport.assertNotNull(rm1);
      ProxyAssertSupport.assertEquals("hello1", rm1.getText());

      cons1.close();

      log.debug("sess.recover()");

      // redeliver
      sess.recover();

      MessageConsumer cons2 = sess.createConsumer(queue1);

      log.debug("receiving ...");

      TextMessage rm2 = (TextMessage) cons2.receive(1500);
      ProxyAssertSupport.assertNotNull(rm2);
      ProxyAssertSupport.assertEquals("hello1", rm2.getText());

      TextMessage rm3 = (TextMessage) cons2.receive(1500);
      ProxyAssertSupport.assertNotNull(rm3);
      ProxyAssertSupport.assertEquals("hello2", rm3.getText());

      TextMessage rm4 = (TextMessage) cons2.receive(1500);
      ProxyAssertSupport.assertNotNull(rm4);
      ProxyAssertSupport.assertEquals("hello3", rm4.getText());

      rm4.acknowledge();
   } finally {
      if (conn != null) {
         conn.close();
      }

      // This last step is important - there shouldn't be any more messages to receive
      checkEmpty(queue1);

      removeAllMessages(queue1.getQueueName(), true);
   }
}
 
Example 13
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testRedel5() throws Exception {
   Connection conn = null;

   try {
      conn = createConnection();
      conn.start();

      Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      MessageProducer prod = sess.createProducer(queue1);
      TextMessage tm1 = sess.createTextMessage("hello1");
      TextMessage tm2 = sess.createTextMessage("hello2");
      TextMessage tm3 = sess.createTextMessage("hello3");
      prod.send(tm1);
      prod.send(tm2);
      prod.send(tm3);

      MessageConsumer cons1 = sess.createConsumer(queue1);

      TextMessage rm1 = (TextMessage) cons1.receive(1500);
      ProxyAssertSupport.assertNotNull(rm1);
      ProxyAssertSupport.assertEquals("hello1", rm1.getText());

      // redeliver
      sess.recover();

      TextMessage rm2 = (TextMessage) cons1.receive(1500);
      ProxyAssertSupport.assertNotNull(rm2);
      ProxyAssertSupport.assertEquals("hello1", rm2.getText());

      TextMessage rm3 = (TextMessage) cons1.receive(1500);
      ProxyAssertSupport.assertNotNull(rm3);
      ProxyAssertSupport.assertEquals("hello2", rm3.getText());

      TextMessage rm4 = (TextMessage) cons1.receive(1500);
      ProxyAssertSupport.assertNotNull(rm4);
      ProxyAssertSupport.assertEquals("hello3", rm4.getText());

      rm4.acknowledge();
   } finally {
      if (conn != null) {
         conn.close();
      }

      // This last step is important - there shouldn't be any more messages to receive
      checkEmpty(queue1);

      removeAllMessages(queue1.getQueueName(), true);
   }
}
 
Example 14
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testClientAcknowledgmentOnClosedConsumer() throws Exception {
   Connection producerConnection = null;

   Connection consumerConnection = null;

   try {
      producerConnection = createConnection();

      consumerConnection = createConnection();

      Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      MessageProducer queueProducer = producerSession.createProducer(queue1);

      MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);

      TextMessage tm = producerSession.createTextMessage();

      tm.setText("One");

      queueProducer.send(tm);

      consumerConnection.start();

      TextMessage m = (TextMessage) queueConsumer.receive(1500);

      ProxyAssertSupport.assertEquals(m.getText(), "One");

      queueConsumer.close();

      m.acknowledge();

      try {
         queueConsumer.receive(2000);
         ProxyAssertSupport.fail("should throw exception");
      } catch (javax.jms.IllegalStateException e) {
         // OK
      }
   } finally {
      if (producerConnection != null) {
         producerConnection.close();
      }
      if (consumerConnection != null) {
         consumerConnection.close();
      }
   }
}
 
Example 15
Source File: ExceptionListenerForConnectionTimedOutExceptionTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testOnAcknowledge(boolean blockOnFailover) throws Exception {
   mayBlock.set(blockOnFailover);
   Connection sendConnection = null;
   Connection connection = null;
   AtomicReference<JMSException> exceptionOnConnection = new AtomicReference<>();

   try {
      ((ActiveMQConnectionFactory) cf).setOutgoingInterceptorList(OutBoundPacketCapture.class.getName());
      ((ActiveMQConnectionFactory) cf).setIncomingInterceptorList(SessAcknowledgeCauseResponseTimeout.class.getName());
      ((ActiveMQConnectionFactory) cf).setBlockOnAcknowledge(true);
      ((ActiveMQConnectionFactory) cf).setCallTimeout(500);

      sendConnection = cf.createConnection();

      final Session sendSession = sendConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      final MessageProducer producer = sendSession.createProducer(queue);

      TextMessage message = sendSession.createTextMessage();

      message.setText("Message");

      producer.send(message);

      connection = cf.createConnection();
      connection.start();
      connection.setExceptionListener(exceptionOnConnection::set);
      final Session consumerSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      final MessageConsumer messageConsumer = consumerSession.createConsumer(queue);

      TextMessage message1 = (TextMessage) messageConsumer.receive(1000);

      assertEquals("Message", message1.getText());

      message1.acknowledge();

      fail("JMSException expected");

   } catch (JMSException e) {
      if (blockOnFailover) {
         Wait.assertTrue(blocked::get);
         unblock();
      }
      assertTrue(e.getCause() instanceof ActiveMQConnectionTimedOutException);
      //Ensure JMS Connection ExceptionListener was also invoked
      assertTrue(Wait.waitFor(() -> exceptionOnConnection.get() != null, 2000, 100));
      assertTrue(exceptionOnConnection.get().getCause() instanceof ActiveMQConnectionTimedOutException);
   } finally {
      if (connection != null) {
         connection.close();
      }
      if (sendConnection != null) {
         sendConnection.close();
      }
   }
}
 
Example 16
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndividualACKMessageConsumer() throws Exception {
   Connection conn = cf.createConnection();
   Session session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
   jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
   MessageProducer producer = session.createProducer(jBossQueue);
   MessageConsumer consumer = session.createConsumer(jBossQueue);
   int noOfMessages = 100;
   for (int i = 0; i < noOfMessages; i++) {
      producer.setPriority(2);
      producer.send(session.createTextMessage("m" + i));
   }

   conn.start();

   final AtomicInteger errors = new AtomicInteger(0);
   final ReusableLatch latch = new ReusableLatch();
   latch.setCount(noOfMessages);

   class MessageAckEven implements MessageListener {

      int count = 0;

      @Override
      public void onMessage(Message msg) {
         try {
            TextMessage txtmsg = (TextMessage) msg;
            if (!txtmsg.getText().equals("m" + count)) {

               errors.incrementAndGet();
            }

            if (count % 2 == 0) {
               msg.acknowledge();
            }

            count++;
         } catch (Exception e) {
            errors.incrementAndGet();
         } finally {
            latch.countDown();
         }
      }

   }

   consumer.setMessageListener(new MessageAckEven());

   Assert.assertTrue(latch.await(5000));

   session.close();

   session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);

   consumer = session.createConsumer(jBossQueue);

   // Consume odd numbers first
   for (int i = 0; i < noOfMessages; i++) {
      if (i % 2 == 0) {
         continue;
      }

      TextMessage m = (TextMessage) consumer.receive(1000);
      Assert.assertNotNull(m);
      m.acknowledge();
      Assert.assertEquals("m" + i, m.getText());
   }

   SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
   conn.close();

   Queue queue = server.locateQueue(queueName);
   Wait.assertEquals(0, queue::getDeliveringCount);
   Wait.assertEquals(0, queue::getMessageCount);
}
 
Example 17
Source File: RecoverTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAcknowledgePerConsumer() throws Exception
{
    Queue queue1 = createQueue("Q1");
    Queue queue2 = createQueue("Q2");

    Connection consumerConnection = getConnection();
    try
    {
        Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageConsumer consumer1 = consumerSession.createConsumer(queue1);
        MessageConsumer consumer2 = consumerSession.createConsumer(queue2);

        Connection producerConnection = getConnection();
        try
        {
            Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer1 = producerSession.createProducer(queue1);
            MessageProducer producer2 = producerSession.createProducer(queue2);

            producer1.send(producerSession.createTextMessage("msg1"));
            producer2.send(producerSession.createTextMessage("msg2"));
        }
        finally
        {
            producerConnection.close();
        }
        consumerConnection.start();

        TextMessage message2 = (TextMessage) consumer2.receive(getReceiveTimeout());
        assertNotNull(message2);
        assertEquals("msg2", message2.getText());

        message2.acknowledge();
        consumerSession.recover();

        TextMessage message1 = (TextMessage) consumer1.receive(getReceiveTimeout());
        assertNotNull(message1);
        assertEquals("msg1", message1.getText());
    }
    finally
    {
        consumerConnection.close();
    }
}
 
Example 18
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testRedeliveryToCompetingConsumerOnQueue() throws Exception {
   Connection conn = null;

   try {
      conn = createConnection();

      Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageProducer prod = sessSend.createProducer(queue1);

      conn.start();

      Session sessConsume1 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      MessageConsumer cons1 = sessConsume1.createConsumer(queue1);

      TextMessage tm = sessSend.createTextMessage();

      tm.setText("Your mum");

      prod.send(tm);

      TextMessage tm2 = (TextMessage) cons1.receive();

      ProxyAssertSupport.assertNotNull(tm2);

      ProxyAssertSupport.assertEquals("Your mum", tm2.getText());

      // Don't ack

      // Create another consumer

      Session sessConsume2 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      MessageConsumer cons2 = sessConsume2.createConsumer(queue1);

      // this should cancel message and cause delivery to other consumer
      sessConsume1.close();

      TextMessage tm3 = (TextMessage) cons2.receive(1000);

      ProxyAssertSupport.assertNotNull(tm3);

      ProxyAssertSupport.assertEquals("Your mum", tm3.getText());

      tm3.acknowledge();
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}
 
Example 19
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
/**
 * http://www.jboss.org/index.html?module=bb&op=viewtopic&t=71350
 */
@Test
public void testRedel7() throws Exception {
   Connection conn = null;

   try {
      conn = createConnection();
      conn.start();

      Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      MessageProducer prod = sess.createProducer(queue1);

      TextMessage tm1 = sess.createTextMessage("1");

      TextMessage tm2 = sess.createTextMessage("2");

      TextMessage tm3 = sess.createTextMessage("3");

      prod.send(tm1);
      prod.send(tm2);
      prod.send(tm3);

      MessageConsumer cons1 = sess.createConsumer(queue1);

      TextMessage r1 = (TextMessage) cons1.receive();

      ProxyAssertSupport.assertEquals(tm1.getText(), r1.getText());

      cons1.close();

      MessageConsumer cons2 = sess.createConsumer(queue1);

      TextMessage r2 = (TextMessage) cons2.receive();

      ProxyAssertSupport.assertEquals(tm2.getText(), r2.getText());

      TextMessage r3 = (TextMessage) cons2.receive();

      ProxyAssertSupport.assertEquals(tm3.getText(), r3.getText());

      r1.acknowledge();
      r2.acknowledge();
      r3.acknowledge();
   } finally {
      if (conn != null) {
         conn.close();
      }
      removeAllMessages(queue1.getQueueName(), true);
   }
}
 
Example 20
Source File: ReceiveNoWaitTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testReceiveNoWait() throws Exception {
   assertNotNull(queue);

   for (int i = 0; i < 1000; i++) {
      Connection connection = cf.createConnection();

      Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      MessageProducer producer = session.createProducer(queue);

      producer.setDeliveryMode(DeliveryMode.PERSISTENT);

      for (int j = 0; j < 100; j++) {
         String text = "Message" + j;

         TextMessage message = session.createTextMessage();

         message.setText(text);

         producer.send(message);
      }

      connection.start();

      MessageConsumer consumer = session.createConsumer(queue);

      for (int j = 0; j < 100; j++) {
         TextMessage m = (TextMessage) consumer.receiveNoWait();

         if (m == null) {
            throw new IllegalStateException("msg null");
         }

         assertEquals("Message" + j, m.getText());

         m.acknowledge();
      }

      connection.close();
   }
}