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();
}
}
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();
}
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();
}
}
Aggregations