use of javax.jms.MessageListener in project wildfly by wildfly.
the class LegacyJMSTestCase method doSendAndReceive.
private void doSendAndReceive(String connectionFactoryLookup, String destinationLoookup) throws Exception {
Destination destination = (Destination) remoteContext.lookup(destinationLoookup);
assertNotNull(destination);
ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
assertNotNull(cf);
try (JMSContext producerContext = cf.createContext("guest", "guest");
JMSContext consumerContext = cf.createContext("guest", "guest")) {
final CountDownLatch latch = new CountDownLatch(10);
final List<String> result = new ArrayList<String>();
JMSConsumer consumer = consumerContext.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
result.add(msg.getText());
latch.countDown();
} catch (JMSException e) {
e.printStackTrace();
}
}
});
JMSProducer producer = producerContext.createProducer();
for (int i = 0; i < 10; i++) {
String text = "Test" + i;
producer.send(destination, text);
}
assertTrue(latch.await(3, SECONDS));
assertEquals(10, result.size());
for (int i = 0; i < result.size(); i++) {
assertEquals("Test" + i, result.get(i));
}
}
}
use of javax.jms.MessageListener in project ats-framework by Axway.
the class JmsClient method startListeningToTopic.
/**
* Start listening for messages on topic
*
* @param topicName the topic name
*/
@PublicAtsApi
public void startListeningToTopic(final String topicName) {
final TopicInfo topicInfo = getTopicInfo(topicName);
if (topicInfo.isListening()) {
throw new JmsMessageException("We are already listening for messages on topic " + topicName);
}
try {
final Session session = loadSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = session.createTopic(topicName);
topicInfo.topicConsumer = session.createConsumer(topic);
topicInfo.topicConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
topicInfo.addMessage(message);
}
});
} catch (JMSException e) {
throw new JmsMessageException("Could not start listening for messages on topic " + topicName, e);
}
}
use of javax.jms.MessageListener 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.MessageListener in project spring-framework by spring-projects.
the class AbstractJmsListenerEndpoint method setupMessageListener.
private void setupMessageListener(MessageListenerContainer container) {
MessageListener messageListener = createMessageListener(container);
if (messageListener == null) {
throw new IllegalStateException("Endpoint [" + this + "] must provide a non-null message listener");
}
container.setupMessageListener(messageListener);
}
use of javax.jms.MessageListener in project qpid-broker-j by apache.
the class ClientJmsDelegate method setInstructionListener.
public void setInstructionListener(final Client client) {
try {
_instructionQueue = _instructionListenerSession.createTemporaryQueue();
final MessageConsumer instructionConsumer = _instructionListenerSession.createConsumer(_instructionQueue);
instructionConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(final Message message) {
client.processInstruction(JmsMessageAdaptor.messageToCommand(message));
}
});
} catch (final JMSException jmse) {
throw new DistributedTestException("Unable to setup instruction listener", jmse);
}
}
Aggregations