Search in sources :

Example 36 with ActiveMQQueue

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

the class RedeliveryPolicyTest method testInitialRedeliveryDelayOne.

public void testInitialRedeliveryDelayOne() throws Exception {
    // Receive a message with the JMS API
    RedeliveryPolicy policy = connection.getRedeliveryPolicy();
    policy.setInitialRedeliveryDelay(1000);
    policy.setUseExponentialBackOff(false);
    policy.setMaximumRedeliveries(1);
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("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(100);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    m = (TextMessage) consumer.receive(100);
    assertNull(m);
    m = (TextMessage) consumer.receive(2000);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    m = (TextMessage) consumer.receive(100);
    assertNotNull(m);
    assertEquals("2nd", m.getText());
    session.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession)

Example 37 with ActiveMQQueue

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

the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionDlq.

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

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

        @Override
        public void onMessage(Message message) {
            LOG.info("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);
    LOG.info("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 : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) 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)

Example 38 with ActiveMQQueue

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

the class QueueConsumerPriorityTest method testQueueConsumerPriority.

public void testQueueConsumerPriority() throws JMSException, InterruptedException {
    Connection conn = createConnection(true);
    Session consumerLowPriority = null;
    Session consumerHighPriority = null;
    Session senderSession = null;
    try {
        consumerLowPriority = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        consumerHighPriority = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertNotNull(consumerHighPriority);
        senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        String queueName = getClass().getName();
        ActiveMQQueue low = new ActiveMQQueue(queueName + "?consumer.priority=1");
        MessageConsumer lowConsumer = consumerLowPriority.createConsumer(low);
        ActiveMQQueue high = new ActiveMQQueue(queueName + "?consumer.priority=2");
        MessageConsumer highConsumer = consumerLowPriority.createConsumer(high);
        ActiveMQQueue senderQueue = new ActiveMQQueue(queueName);
        MessageProducer producer = senderSession.createProducer(senderQueue);
        Message msg = senderSession.createTextMessage("test");
        for (int i = 0; i < 10000; i++) {
            producer.send(msg);
            assertNotNull("null on iteration: " + i, highConsumer.receive(500));
        }
        assertNull(lowConsumer.receive(2000));
    } finally {
        conn.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 39 with ActiveMQQueue

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

the class RedeliveryPolicyTest method testNornalRedeliveryPolicyDelaysDeliveryOnRollback.

/**
 * @throws Exception
 */
public void testNornalRedeliveryPolicyDelaysDeliveryOnRollback() throws Exception {
    // Receive a message with the JMS API
    RedeliveryPolicy policy = connection.getRedeliveryPolicy();
    policy.setInitialRedeliveryDelay(0);
    policy.setRedeliveryDelay(500);
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue(getName());
    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();
    // No delay on first rollback..
    m = (TextMessage) consumer.receive(100);
    assertNotNull(m);
    session.rollback();
    // Show subsequent re-delivery delay is incrementing.
    m = (TextMessage) consumer.receive(100);
    assertNull(m);
    m = (TextMessage) consumer.receive(700);
    assertNotNull(m);
    assertEquals("1st", m.getText());
    session.rollback();
    // The message gets redelivered after 500 ms every time since
    // we are not using exponential backoff.
    m = (TextMessage) consumer.receive(100);
    assertNull(m);
    m = (TextMessage) consumer.receive(700);
    assertNotNull(m);
    assertEquals("1st", m.getText());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession)

Example 40 with ActiveMQQueue

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

the class Main method main.

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        BrokerService broker = new BrokerService();
        broker.setPersistent(false);
        // String brokerDir = "xbean:...;
        // System.setProperty("activemq.base", brokerDir);
        // BrokerService broker = BrokerFactory.createBroker(new URI(brokerDir + "/activemq.xml"));
        // for running on Java 5 without mx4j
        ManagementContext managementContext = broker.getManagementContext();
        managementContext.setFindTigerMbeanServer(true);
        managementContext.setUseMBeanServer(true);
        managementContext.setCreateConnector(false);
        broker.setUseJmx(true);
        // broker.setPlugins(new BrokerPlugin[] { new
        // ConnectionDotFilePlugin(), new UDPTraceBrokerPlugin() });
        broker.addConnector("tcp://localhost:61616");
        broker.addConnector("stomp://localhost:61613");
        broker.start();
        // lets publish some messages so that there is some stuff to browse
        DefaultQueueSender.main(new String[] { "Prices.Equity.IBM" });
        DefaultQueueSender.main(new String[] { "Prices.Equity.MSFT" });
        // lets create a dummy couple of consumers
        if (createConsumers) {
            Connection connection = new ActiveMQConnectionFactory().createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            session.createConsumer(new ActiveMQQueue("Orders.IBM"));
            session.createConsumer(new ActiveMQQueue("Orders.MSFT"), "price > 100");
            Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            session2.createConsumer(new ActiveMQQueue("Orders.MSFT"), "price > 200");
        } else {
            // Lets wait for the broker
            broker.waitUntilStopped();
        }
    } catch (Exception e) {
        System.out.println("Failed: " + e);
        e.printStackTrace();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Connection(javax.jms.Connection) ManagementContext(org.apache.activemq.broker.jmx.ManagementContext) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Session(javax.jms.Session)

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