Search in sources :

Example 1 with JmsTestListener

use of org.apache.rocketmq.jms.JmsTestListener 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 2 with JmsTestListener

use of org.apache.rocketmq.jms.JmsTestListener in project rocketmq-externals by apache.

the class JmsConsumeIT method testConsume.

@Test
public void testConsume() throws Exception {
    final Topic topic = (Topic) consumeContext.getBean("baseTopic");
    JmsTestListener messageListener = (JmsTestListener) consumeContext.getBean("messageListener");
    CountDownLatch countDownLatch = new CountDownLatch(1);
    messageListener.setLatch(countDownLatch);
    messageListener.setExpectd(30);
    consumeContext.start();
    for (int i = 0; i < 30; i++) {
        jmsTemplate.execute(topic, new ProducerCallback() {

            @Override
            public Object doInJms(Session session, MessageProducer producer) throws JMSException {
                JmsTextMessage message = (JmsTextMessage) session.createTextMessage("hello world,kafka, haha");
                producer.send(topic, message);
                Assert.assertNotNull(message.getJMSMessageID());
                return message;
            }
        });
    }
    if (countDownLatch.await(30, TimeUnit.SECONDS)) {
        Thread.sleep(2000);
    }
    Assert.assertEquals(30, messageListener.getConsumedNum());
    consumeContext.close();
}
Also used : JmsTestListener(org.apache.rocketmq.jms.JmsTestListener) ProducerCallback(org.springframework.jms.core.ProducerCallback) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) CountDownLatch(java.util.concurrent.CountDownLatch) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) Session(javax.jms.Session) Test(org.testng.annotations.Test)

Example 3 with JmsTestListener

use of org.apache.rocketmq.jms.JmsTestListener in project rocketmq-externals by apache.

the class JmsClientIT method testProducerAndConsume_TagFilter.

@Test
public void testProducerAndConsume_TagFilter() throws Exception {
    Connection connection = createConnection(producerId, consumerId);
    Connection anotherConnection = createConnection(producerId, consumerId + "other");
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session anotherSession = anotherConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destinationA = session.createTopic("topic:tagA");
    Destination destinationB = session.createTopic("topic:tagB");
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    JmsTestListener listenerForTagA = new JmsTestListener(10, countDownLatch);
    JmsTestListener listenerForAll = new JmsTestListener(40, countDownLatch);
    try {
        session.createConsumer(destinationA).setMessageListener(listenerForTagA);
        anotherSession.createConsumer(session.createTopic("topic")).setMessageListener(listenerForAll);
        // producer
        MessageProducer messageProducer = session.createProducer(destinationA);
        connection.start();
        anotherConnection.start();
        for (int i = 0; i < 20; i++) {
            TextMessage message = session.createTextMessage(text + i);
            Assert.assertNull(message.getJMSMessageID());
            messageProducer.send(message);
            Assert.assertNotNull(message.getJMSMessageID());
        }
        for (int i = 0; i < 20; 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(20, listenerForTagA.getConsumedNum());
        Assert.assertEquals(40, listenerForAll.getConsumedNum());
    } finally {
        // Close the connection
        connection.close();
        anotherConnection.close();
    }
}
Also used : Destination(javax.jms.Destination) 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)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)3 MessageProducer (javax.jms.MessageProducer)3 Session (javax.jms.Session)3 JmsTestListener (org.apache.rocketmq.jms.JmsTestListener)3 Connection (javax.jms.Connection)2 Destination (javax.jms.Destination)2 TextMessage (javax.jms.TextMessage)2 Test (org.junit.Test)2 JMSException (javax.jms.JMSException)1 MessageConsumer (javax.jms.MessageConsumer)1 Topic (javax.jms.Topic)1 JmsTextMessage (org.apache.rocketmq.jms.domain.message.JmsTextMessage)1 ProducerCallback (org.springframework.jms.core.ProducerCallback)1 Test (org.testng.annotations.Test)1