Search in sources :

Example 61 with ActiveMQQueue

use of org.apache.activemq.command.ActiveMQQueue in project activemq-artemis by apache.

the class RedeliveryPolicyTest method testMaximumRedeliveryDelay.

/**
 * @throws Exception
 */
@Test
public void testMaximumRedeliveryDelay() throws Exception {
    // Receive a message with the JMS API
    RedeliveryPolicy policy = connection.getRedeliveryPolicy();
    policy.setInitialRedeliveryDelay(10);
    policy.setUseExponentialBackOff(true);
    policy.setMaximumRedeliveries(-1);
    policy.setRedeliveryDelay(50);
    policy.setMaximumRedeliveryDelay(1000);
    policy.setBackOffMultiplier((short) 2);
    policy.setUseExponentialBackOff(true);
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    this.makeSureCoreQueueExist("TEST");
    MessageProducer producer = session.createProducer(destination);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send the messages
    producer.send(session.createTextMessage("1st"));
    producer.send(session.createTextMessage("2nd"));
    session.commit();
    TextMessage m;
    for (int i = 0; i < 10; ++i) {
        // we should be able to get the 1st message redelivered until a
        // session.commit is called
        m = (TextMessage) consumer.receive(2000);
        assertNotNull(m);
        assertEquals("1st", m.getText());
        session.rollback();
    }
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.commit();
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.commit();
    assertTrue(policy.getNextRedeliveryDelay(Long.MAX_VALUE) == 1000);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) RedeliveryPolicy(org.apache.activemq.RedeliveryPolicy) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 62 with ActiveMQQueue

use of org.apache.activemq.command.ActiveMQQueue in project activemq-artemis by apache.

the class RedeliveryPolicyTest method testZeroMaximumNumberOfRedeliveries.

/**
 * @throws Exception
 */
@Test
public void testZeroMaximumNumberOfRedeliveries() throws Exception {
    // Receive a message with the JMS API
    RedeliveryPolicy policy = connection.getRedeliveryPolicy();
    policy.setInitialRedeliveryDelay(100);
    policy.setUseExponentialBackOff(false);
    // let's set the maximum redeliveries to 0
    policy.setMaximumRedeliveries(0);
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    this.makeSureCoreQueueExist("TEST");
    MessageProducer producer = session.createProducer(destination);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send the messages
    producer.send(session.createTextMessage("1st"));
    producer.send(session.createTextMessage("2nd"));
    session.commit();
    TextMessage m;
    m = (TextMessage) consumer.receive(1000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    // the 1st message should not be redelivered since maximumRedeliveries is
    // set to 0
    m = (TextMessage) consumer.receive(1000);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) RedeliveryPolicy(org.apache.activemq.RedeliveryPolicy) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 63 with ActiveMQQueue

use of org.apache.activemq.command.ActiveMQQueue in project activemq-artemis by apache.

the class RedeliveryPolicyTest method testRedeliveryPolicyPerDestination.

@Test
public void testRedeliveryPolicyPerDestination() throws Exception {
    RedeliveryPolicy queuePolicy = new RedeliveryPolicy();
    queuePolicy.setInitialRedeliveryDelay(0);
    queuePolicy.setRedeliveryDelay(1000);
    queuePolicy.setUseExponentialBackOff(false);
    queuePolicy.setMaximumRedeliveries(2);
    RedeliveryPolicy topicPolicy = new RedeliveryPolicy();
    topicPolicy.setInitialRedeliveryDelay(0);
    topicPolicy.setRedeliveryDelay(1000);
    topicPolicy.setUseExponentialBackOff(false);
    topicPolicy.setMaximumRedeliveries(3);
    // Receive a message with the JMS API
    RedeliveryPolicyMap map = connection.getRedeliveryPolicyMap();
    map.put(new ActiveMQTopic(">"), topicPolicy);
    map.put(new ActiveMQQueue(">"), queuePolicy);
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue queue = new ActiveMQQueue("TEST");
    ActiveMQTopic topic = new ActiveMQTopic("TEST");
    this.makeSureCoreQueueExist("TEST");
    MessageProducer producer = session.createProducer(null);
    MessageConsumer queueConsumer = session.createConsumer(queue);
    MessageConsumer topicConsumer = session.createConsumer(topic);
    // Send the messages
    producer.send(queue, session.createTextMessage("1st"));
    producer.send(queue, session.createTextMessage("2nd"));
    producer.send(topic, session.createTextMessage("1st"));
    producer.send(topic, session.createTextMessage("2nd"));
    session.commit();
    TextMessage m;
    m = (TextMessage) queueConsumer.receive(100);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) topicConsumer.receive(100);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) queueConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    m = (TextMessage) topicConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.rollback();
    m = (TextMessage) queueConsumer.receive(100);
    assertNotNull("first immediate redelivery", m);
    m = (TextMessage) topicConsumer.receive(100);
    assertNotNull("first immediate redelivery", m);
    session.rollback();
    m = (TextMessage) queueConsumer.receive(100);
    assertNull("second delivery delayed: " + m, m);
    m = (TextMessage) topicConsumer.receive(100);
    assertNull("second delivery delayed: " + m, m);
    m = (TextMessage) queueConsumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) topicConsumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) queueConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    m = (TextMessage) topicConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.rollback();
    m = (TextMessage) queueConsumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) topicConsumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) queueConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    m = (TextMessage) topicConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.rollback();
    // No third attempt for the Queue consumer
    m = (TextMessage) queueConsumer.receive(2000);
    assertNull(m);
    m = (TextMessage) topicConsumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) queueConsumer.receive(100);
    assertNull(m);
    m = (TextMessage) topicConsumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) RedeliveryPolicy(org.apache.activemq.RedeliveryPolicy) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) RedeliveryPolicyMap(org.apache.activemq.broker.region.policy.RedeliveryPolicyMap) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 64 with ActiveMQQueue

use of org.apache.activemq.command.ActiveMQQueue in project activemq-artemis by apache.

the class RedeliveryPolicyTest method testInfiniteMaximumNumberOfRedeliveries.

/**
 * @throws Exception
 */
@Test
public void testInfiniteMaximumNumberOfRedeliveries() throws Exception {
    // Receive a message with the JMS API
    RedeliveryPolicy policy = connection.getRedeliveryPolicy();
    policy.setInitialRedeliveryDelay(100);
    policy.setUseExponentialBackOff(false);
    // let's set the maximum redeliveries to no maximum (ie. infinite)
    policy.setMaximumRedeliveries(-1);
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    this.makeSureCoreQueueExist("TEST");
    MessageProducer producer = session.createProducer(destination);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send the messages
    producer.send(session.createTextMessage("1st"));
    producer.send(session.createTextMessage("2nd"));
    session.commit();
    TextMessage m;
    m = (TextMessage) consumer.receive(1000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    // we should be able to get the 1st message redelivered until a
    // session.commit is called
    m = (TextMessage) consumer.receive(1000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.commit();
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) RedeliveryPolicy(org.apache.activemq.RedeliveryPolicy) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 65 with ActiveMQQueue

use of org.apache.activemq.command.ActiveMQQueue in project activemq-artemis by apache.

the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionDlq.

@Test
public void testQueueSessionListenerExceptionDlq() throws Exception {
    redeliverConnection.start();
    Session session = redeliverConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    String qname = "queue-testQueueSessionListenerExceptionDlq";
    Queue queue = session.createQueue(qname);
    this.makeSureCoreQueueExist(qname);
    MessageProducer producer = createProducer(session, queue);
    Message message = createTextMessage(session);
    producer.send(message);
    final Message[] dlqMessage = new Message[1];
    ActiveMQDestination dlqDestination = new ActiveMQQueue("ActiveMQ.DLQ");
    this.makeSureCoreQueueExist("ActiveMQ.DLQ");
    MessageConsumer dlqConsumer = session.createConsumer(dlqDestination);
    final CountDownLatch gotDlqMessage = new CountDownLatch(1);
    dlqConsumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            System.out.println("DLQ Message Received: " + message);
            dlqMessage[0] = message;
            gotDlqMessage.countDown();
        }
    });
    MessageConsumer consumer = session.createConsumer(queue);
    final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
    System.out.println("max redlivery: " + maxDeliveries);
    final CountDownLatch gotMessage = new CountDownLatch(maxDeliveries);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            System.out.println("Message Received: " + message);
            gotMessage.countDown();
            throw new RuntimeException(getName() + " force a redelivery");
        }
    });
    assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
    // check DLQ
    assertTrue("got dlq message", gotDlqMessage.await(20, TimeUnit.SECONDS));
    // check DLQ message cause is captured
    message = dlqMessage[0];
    assertNotNull("dlq message captured", message);
    String cause = message.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    System.out.println("DLQ'd message cause reported as: " + cause);
    assertTrue("cause 'cause' exception is remembered", cause.contains("RuntimeException"));
    assertTrue("is correct exception", cause.contains(getName()));
    assertTrue("cause exception is remembered", cause.contains("Throwable"));
    assertTrue("cause policy is remembered", cause.contains("RedeliveryPolicy"));
    session.close();
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Aggregations

ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)239 Session (javax.jms.Session)81 MessageProducer (javax.jms.MessageProducer)78 MessageConsumer (javax.jms.MessageConsumer)76 TextMessage (javax.jms.TextMessage)73 Test (org.junit.Test)66 ActiveMQTopic (org.apache.activemq.command.ActiveMQTopic)54 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)44 Message (javax.jms.Message)36 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)33 Connection (javax.jms.Connection)32 Destination (javax.jms.Destination)27 CountDownLatch (java.util.concurrent.CountDownLatch)20 ActiveMQTextMessage (org.apache.activemq.command.ActiveMQTextMessage)18 ConnectionInfo (org.apache.activemq.command.ConnectionInfo)18 ConsumerInfo (org.apache.activemq.command.ConsumerInfo)18 SessionInfo (org.apache.activemq.command.SessionInfo)18 Message (org.apache.activemq.command.Message)17 BasicOpenWireTest (org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest)16 ProducerInfo (org.apache.activemq.command.ProducerInfo)16