Search in sources :

Example 46 with MessageConsumer

use of javax.jms.MessageConsumer in project tech by ffyyhh995511.

the class ActiveMqService method receiveTopic.

/**
 * 接受active mq的topic(订阅)
 * 通过一个消费者的监听接受推送的消息
 * @param topicStr
 * @throws Exception
 */
public void receiveTopic(String topicStr) throws Exception {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
    Connection connection = connectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicStr);
    MessageConsumer consumer = session.createConsumer(topic);
    consumer.setMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            TextMessage tm = (TextMessage) message;
            System.out.println(tm);
            try {
                System.out.println(tm.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 47 with MessageConsumer

use of javax.jms.MessageConsumer in project rocketmq-externals by apache.

the class JmsClientIT method testProducerAndConsume_TwoConsumer.

@Test
public void testProducerAndConsume_TwoConsumer() throws Exception {
    Connection connection = createConnection(producerId, consumerId);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destinationA = session.createTopic("TopicA");
    Destination destinationB = session.createTopic("TopicB");
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    JmsTestListener listenerA = new JmsTestListener(10, countDownLatch);
    JmsTestListener listenerB = new JmsTestListener(10, countDownLatch);
    try {
        // two consumers
        MessageConsumer messageConsumerA = session.createConsumer(destinationA);
        messageConsumerA.setMessageListener(listenerA);
        MessageConsumer messageConsumerB = session.createConsumer(destinationB);
        messageConsumerB.setMessageListener(listenerB);
        // producer
        MessageProducer messageProducer = session.createProducer(destinationA);
        connection.start();
        for (int i = 0; i < 10; i++) {
            TextMessage message = session.createTextMessage(text + i);
            Assert.assertNull(message.getJMSMessageID());
            messageProducer.send(message);
            Assert.assertNotNull(message.getJMSMessageID());
        }
        for (int i = 0; i < 10; i++) {
            TextMessage message = session.createTextMessage(text + i);
            Assert.assertNull(message.getJMSMessageID());
            messageProducer.send(destinationB, message);
            Assert.assertNotNull(message.getJMSMessageID());
        }
        if (countDownLatch.await(30, TimeUnit.SECONDS)) {
            Thread.sleep(2000);
        }
        Assert.assertEquals(10, listenerA.getConsumedNum());
        Assert.assertEquals(10, listenerB.getConsumedNum());
    } finally {
        // Close the connection
        connection.close();
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) JmsTestListener(org.apache.rocketmq.jms.JmsTestListener) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 48 with MessageConsumer

use of javax.jms.MessageConsumer in project rocketmq-externals by apache.

the class JmsConsumerIT method testStartIdempotency.

@Test
public void testStartIdempotency() throws Exception {
    JmsBaseConnectionFactory connectionFactory = new JmsBaseConnectionFactory(new URI("rocketmq://xxx?consumerId=" + consumerId + "&nameServer=" + nameServer));
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    checkConsumerState(consumerId, true, false);
    try {
        Destination destination = session.createTopic(topic + ":" + messageType);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(listener);
        checkConsumerState(consumerId, false, false);
        ((JmsBaseMessageConsumer) consumer).startConsumer();
        checkConsumerState(consumerId, false, true);
        Destination destination1 = session.createTopic(topic2 + ":" + messageType);
        MessageConsumer consumer1 = session.createConsumer(destination1);
        consumer1.setMessageListener(listener);
        ((JmsBaseMessageConsumer) consumer1).startConsumer();
        checkConsumerState(consumerId, false, true);
        // the start is idempotent
        connection.start();
        connection.start();
        Thread.sleep(5000);
    } finally {
        connection.close();
    }
}
Also used : Destination(javax.jms.Destination) JmsBaseMessageConsumer(org.apache.rocketmq.jms.domain.JmsBaseMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) JmsBaseMessageConsumer(org.apache.rocketmq.jms.domain.JmsBaseMessageConsumer) JmsBaseConnectionFactory(org.apache.rocketmq.jms.domain.JmsBaseConnectionFactory) Connection(javax.jms.Connection) URI(java.net.URI) Session(javax.jms.Session) Test(org.junit.Test)

Example 49 with MessageConsumer

use of javax.jms.MessageConsumer in project rocketmq-externals by apache.

the class JmsConsumerIT method testReferenceCount.

@Test
public void testReferenceCount() throws Exception {
    JmsBaseConnectionFactory connectionFactory = new JmsBaseConnectionFactory(new URI("rocketmq://xxx?consumerId=" + consumerId + "&nameServer=" + nameServer));
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    connection.start();
    try {
        Destination destination = session.createTopic(topic + ":" + messageType);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(listener);
        RMQPushConsumerExt rmqPushConsumerExt = getRMQPushConsumerExt(consumerId);
        Assert.assertNotNull(rmqPushConsumerExt);
        Assert.assertEquals(1, rmqPushConsumerExt.getReferenceCount());
        MessageConsumer consumer2 = session.createConsumer(destination);
        Assert.assertEquals(2, rmqPushConsumerExt.getReferenceCount());
        MessageConsumer consumer3 = session.createConsumer(session.createTopic(topic + ":" + messageType));
        Assert.assertEquals(3, rmqPushConsumerExt.getReferenceCount());
        session.close();
        Assert.assertEquals(0, rmqPushConsumerExt.getReferenceCount());
        Assert.assertEquals(false, rmqPushConsumerExt.isStarted());
        Assert.assertNull(getRMQPushConsumerExt(consumerId));
        Thread.sleep(5000);
    } finally {
        connection.close();
    }
}
Also used : Destination(javax.jms.Destination) JmsBaseMessageConsumer(org.apache.rocketmq.jms.domain.JmsBaseMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) JmsTestUtil.getRMQPushConsumerExt(org.apache.rocketmq.jms.JmsTestUtil.getRMQPushConsumerExt) RMQPushConsumerExt(org.apache.rocketmq.jms.domain.RMQPushConsumerExt) JmsBaseConnectionFactory(org.apache.rocketmq.jms.domain.JmsBaseConnectionFactory) Connection(javax.jms.Connection) URI(java.net.URI) Session(javax.jms.Session) Test(org.junit.Test)

Example 50 with MessageConsumer

use of javax.jms.MessageConsumer in project rocketmq-externals by apache.

the class SimpleExMessageListenerContainer method createListenerConsumer.

/**
 * Create a MessageConsumer for the given JMS Session, registering a
 * MessageListener for the specified listener.
 *
 * @param session
 *         the JMS Session to work on
 *
 * @return the MessageConsumer
 *
 * @throws JMSException
 *         if thrown by JMS methods
 * @see #executeListener
 */
protected MessageConsumer createListenerConsumer(final Session session) throws JMSException {
    Destination destination = getDestination();
    if (destination == null) {
        destination = resolveDestinationName(session, getDestinationName());
    }
    MessageConsumer consumer = createConsumer(session, destination);
    consumer.setMessageListener((MessageListener) super.getMessageListener());
    return consumer;
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3