Search in sources :

Example 41 with ActiveMQDestination

use of org.apache.activemq.command.ActiveMQDestination 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)

Example 42 with ActiveMQDestination

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

the class JmsTopicRequestReplyTest method onMessage.

/**
 * Use the asynchronous subscription mechanism
 */
@Override
public void onMessage(Message message) {
    try {
        TextMessage requestMessage = (TextMessage) message;
        System.out.println("Received request from " + requestDestination);
        System.out.println(requestMessage.toString());
        Destination replyDestination = requestMessage.getJMSReplyTo();
        // TODO
        // String value =
        // ActiveMQDestination.getClientId((ActiveMQDestination)
        // replyDestination);
        // assertEquals("clientID from the temporary destination must be the
        // same", clientSideClientID, value);
        TextMessage replyMessage = serverSession.createTextMessage("Hello: " + requestMessage.getText());
        replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
        if (dynamicallyCreateProducer) {
            replyProducer = serverSession.createProducer(replyDestination);
            replyProducer.send(replyMessage);
        } else {
            replyProducer.send(replyDestination, replyMessage);
        }
        System.out.println("Sent reply to " + replyDestination);
        System.out.println(replyMessage.toString());
    } catch (JMSException e) {
        onException(e);
    }
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

Example 43 with ActiveMQDestination

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

the class JMSConsumer12Test method testStartAfterSend.

@Test
public void testStartAfterSend() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, destinationType);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send the messages
    sendMessages(session, destination, 1);
    // Start the conncection after the message was sent.
    connection.start();
    // Make sure only 1 message was delivered.
    assertNotNull(consumer.receive(1000));
    assertNull(consumer.receiveNoWait());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 44 with ActiveMQDestination

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

the class JMSConsumer11Test method testPrefetch1MessageNotDispatched.

@Test
public void testPrefetch1MessageNotDispatched() throws Exception {
    // Set prefetch to 1
    connection.getPrefetchPolicy().setAll(1);
    connection.start();
    Session session = connection.createSession(true, 0);
    ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send 2 messages to the destination.
    sendMessages(session, destination, 2);
    session.commit();
    // The prefetch should fill up with 1 message.
    // Since prefetch is still full, the 2nd message should get dispatched
    // to another consumer.. lets create the 2nd consumer test that it does
    // make sure it does.
    ActiveMQConnection connection2 = (ActiveMQConnection) factory.createConnection();
    connection2.start();
    Session session2 = connection2.createSession(true, 0);
    MessageConsumer consumer2 = session2.createConsumer(destination);
    System.out.println("consumer receiving ...");
    // Pick up the first message.
    Message message1 = consumer.receive(1000);
    System.out.println("received1: " + message1);
    assertNotNull(message1);
    System.out.println("consumer 2 receiving...");
    // Pick up the 2nd messages.
    Message message2 = consumer2.receive(5000);
    System.out.println("received2: " + message2);
    assertNotNull(message2);
    System.out.println("committing sessions !! " + session.getClass().getName());
    session.commit();
    System.out.println("committed session, now 2");
    session2.commit();
    System.out.println("all committed");
    Message m = consumer.receiveNoWait();
    System.out.println("received 3: " + m);
    assertNull(m);
    try {
        connection2.close();
    } catch (Throwable e) {
        System.err.println("exception e: " + e);
        e.printStackTrace();
    }
    System.out.println("Test finished!!");
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 45 with ActiveMQDestination

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

the class JMSConsumer13Test method testReceiveMessageWithConsumer.

@Test
public void testReceiveMessageWithConsumer() throws Exception {
    // Receive a message with the JMS API
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, destinationType);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send the messages
    sendMessages(session, destination, 1);
    // Make sure only 1 message was delivered.
    Message m = consumer.receive(1000);
    assertNotNull(m);
    assertEquals("0", ((TextMessage) m).getText());
    assertNull(consumer.receiveNoWait());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) 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)

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