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