Search in sources :

Example 46 with ActiveMQDestination

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

the class JMSConsumer2Test method testRedispatchOfRolledbackTx.

@Test
public void testRedispatchOfRolledbackTx() throws Exception {
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    sendMessages(connection, destination, 2);
    MessageConsumer consumer = session.createConsumer(destination);
    assertNotNull(consumer.receive(1000));
    assertNotNull(consumer.receive(1000));
    // install another consumer while message dispatch is unacked/uncommitted
    Session redispatchSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    MessageConsumer redispatchConsumer = redispatchSession.createConsumer(destination);
    session.rollback();
    session.close();
    Message msg = redispatchConsumer.receive(1000);
    assertNotNull(msg);
    assertTrue(msg.getJMSRedelivered());
    assertEquals(2, msg.getLongProperty("JMSXDeliveryCount"));
    msg = redispatchConsumer.receive(1000);
    assertNotNull(msg);
    assertTrue(msg.getJMSRedelivered());
    assertEquals(2, msg.getLongProperty("JMSXDeliveryCount"));
    redispatchSession.commit();
    assertNull(redispatchConsumer.receive(500));
    redispatchSession.close();
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 47 with ActiveMQDestination

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

the class JMSConsumer2Test method testMessageListenerWithConsumerCanBeStoppedConcurently.

@Test
public void testMessageListenerWithConsumerCanBeStoppedConcurently() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch closeDone = new CountDownLatch(1);
    connection.start();
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    // preload the queue
    sendMessages(session, destination, 2000);
    final ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
    final Map<Thread, Throwable> exceptions = Collections.synchronizedMap(new HashMap<Thread, Throwable>());
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            exceptions.put(t, e);
        }
    });
    final class AckAndClose implements Runnable {

        private final Message message;

        AckAndClose(Message m) {
            this.message = m;
        }

        @Override
        public void run() {
            try {
                int count = counter.incrementAndGet();
                if (count == 590) {
                    // close in a separate thread is ok by jms
                    consumer.close();
                    closeDone.countDown();
                }
                if (count % 200 == 0) {
                    // ensure there are some outstanding messages
                    // ack every 200
                    message.acknowledge();
                }
            } catch (Exception e) {
                exceptions.put(Thread.currentThread(), e);
            }
        }
    }
    final ExecutorService executor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            // ack and close eventually in separate thread
            executor.execute(new AckAndClose(m));
        }
    });
    assertTrue(closeDone.await(20, TimeUnit.SECONDS));
    // await possible exceptions
    Thread.sleep(1000);
    assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
}
Also used : Message(javax.jms.Message) ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 48 with ActiveMQDestination

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

the class JMSConsumer4Test method testDurableConsumerSelectorChange.

@Test
public void testDurableConsumerSelectorChange() throws Exception {
    // Receive a message with the JMS API
    connection.setClientID("test");
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, destinationType);
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(deliveryMode);
    MessageConsumer consumer = session.createDurableSubscriber((Topic) destination, "test", "color='red'", false);
    // Send the messages
    TextMessage message = session.createTextMessage("1st");
    message.setStringProperty("color", "red");
    producer.send(message);
    Message m = consumer.receive(1000);
    assertNotNull(m);
    assertEquals("1st", ((TextMessage) m).getText());
    // Change the subscription.
    consumer.close();
    consumer = session.createDurableSubscriber((Topic) destination, "test", "color='blue'", false);
    message = session.createTextMessage("2nd");
    message.setStringProperty("color", "red");
    producer.send(message);
    message = session.createTextMessage("3rd");
    message.setStringProperty("color", "blue");
    producer.send(message);
    // Selector should skip the 2nd message.
    m = consumer.receive(1000);
    assertNotNull(m);
    assertEquals("3rd", ((TextMessage) m).getText());
    assertNull(consumer.receiveNoWait());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 49 with ActiveMQDestination

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

the class JMSUsecase1Test method testSendReceiveTransacted.

@Test
public void testSendReceiveTransacted() throws Exception {
    // Send a message to the broker.
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    ActiveMQDestination destination = createDestination(session, destinationType);
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(this.deliveryMode);
    MessageConsumer consumer = session.createConsumer(destination);
    producer.send(session.createTextMessage("test"));
    // Message should not be delivered until commit.
    assertNull(consumer.receiveNoWait());
    session.commit();
    // Make sure only 1 message was delivered.
    Message message = consumer.receive(1000);
    assertNotNull(message);
    assertFalse(message.getJMSRedelivered());
    assertNull(consumer.receiveNoWait());
    // Message should be redelivered is rollback is used.
    session.rollback();
    // Make sure only 1 message was delivered.
    message = consumer.receive(2000);
    assertNotNull(message);
    assertTrue(message.getJMSRedelivered());
    assertNull(consumer.receiveNoWait());
    // If we commit now, the message should not be redelivered.
    session.commit();
    assertNull(consumer.receiveNoWait());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 50 with ActiveMQDestination

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

the class JMSUsecaseTest method testQueueBrowser.

@Test
public void testQueueBrowser() throws Exception {
    // Send a message to the broker.
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, destinationType);
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(this.deliveryMode);
    sendMessages(session, producer, 5);
    producer.close();
    QueueBrowser browser = session.createBrowser((Queue) destination);
    Enumeration<?> enumeration = browser.getEnumeration();
    for (int i = 0; i < 5; i++) {
        Thread.sleep(100);
        assertTrue(enumeration.hasMoreElements());
        Message m = (Message) enumeration.nextElement();
        assertNotNull(m);
        assertEquals("" + i, ((TextMessage) m).getText());
    }
    assertFalse(enumeration.hasMoreElements());
}
Also used : TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Aggregations

ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)165 Session (javax.jms.Session)62 MessageConsumer (javax.jms.MessageConsumer)49 Message (org.apache.activemq.command.Message)46 ConsumerInfo (org.apache.activemq.command.ConsumerInfo)45 ConnectionInfo (org.apache.activemq.command.ConnectionInfo)44 SessionInfo (org.apache.activemq.command.SessionInfo)44 ProducerInfo (org.apache.activemq.command.ProducerInfo)42 Test (org.junit.Test)41 Message (javax.jms.Message)40 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)40 TextMessage (javax.jms.TextMessage)31 BasicOpenWireTest (org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest)24 ActiveMQTopic (org.apache.activemq.command.ActiveMQTopic)22 MessageProducer (javax.jms.MessageProducer)18 XATransactionId (org.apache.activemq.command.XATransactionId)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 ActiveMQMessageProducer (org.apache.activemq.ActiveMQMessageProducer)12 MessageAck (org.apache.activemq.command.MessageAck)12 ActiveMQMessageConsumer (org.apache.activemq.ActiveMQMessageConsumer)11