use of javax.jms.MessageListener in project cxf by apache.
the class MessageListenerTest method testConnectionProblem.
@Test
public void testConnectionProblem() throws JMSException {
Connection connection = createConnection("broker");
Queue dest = JMSUtil.createQueue(connection, "test");
MessageListener listenerHandler = new TestMessageListener();
ExceptionListener exListener = createMock(ExceptionListener.class);
Capture<JMSException> captured = newCapture();
exListener.onException(capture(captured));
expectLastCall();
replay(exListener);
//
PollingMessageListenerContainer container = new PollingMessageListenerContainer(connection, dest, listenerHandler, exListener);
// Simulate connection problem
connection.close();
container.start();
Awaitility.await().until(() -> !container.isRunning());
verify(exListener);
JMSException ex = captured.getValue();
Assert.assertEquals("The connection is already closed", ex.getMessage());
}
use of javax.jms.MessageListener in project cxf by apache.
the class MessageListenerTest method testWithJTA.
@Test
public void testWithJTA() throws JMSException, XAException, InterruptedException {
TransactionManager transactionManager = new GeronimoTransactionManager();
Connection connection = createXAConnection("brokerJTA", transactionManager);
Queue dest = JMSUtil.createQueue(connection, "test");
MessageListener listenerHandler = new TestMessageListener();
ExceptionListener exListener = new ExceptionListener() {
@Override
public void onException(JMSException exception) {
}
};
PollingMessageListenerContainer container = new PollingMessageListenerContainer(connection, dest, listenerHandler, exListener);
container.setTransacted(false);
container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
container.setTransactionManager(transactionManager);
container.start();
testTransactionalBehaviour(connection, dest);
container.stop();
connection.close();
}
use of javax.jms.MessageListener in project cxf by apache.
the class MessageListenerContainer method start.
@Override
public void start() {
try {
session = connection.createSession(transacted, acknowledgeMode);
if (durableSubscriptionName != null && destination instanceof Topic) {
consumer = session.createDurableSubscriber((Topic) destination, durableSubscriptionName, messageSelector, pubSubNoLocal);
} else {
consumer = session.createConsumer(destination, messageSelector);
}
MessageListener intListener = new LocalTransactionalMessageListener(session, listenerHandler);
// new DispachingListener(getExecutor(), listenerHandler);
consumer.setMessageListener(intListener);
running = true;
} catch (JMSException e) {
throw JMSUtil.convertJmsException(e);
}
}
use of javax.jms.MessageListener in project JFramework by gugumall.
the class Receiver method listen.
/**
* 初始化并开始监听消息
* @throws Exception
*/
private void listen() throws Exception {
try {
MessageListener listener = (MessageListener) Class.forName(this.getProperty("LISTENER")).newInstance();
if (type == 0) {
initP2P(DeliveryMode.PERSISTENT);
queueReceiver.setMessageListener(listener);
} else if (type == 1) {
initP2S(DeliveryMode.PERSISTENT);
subscriber.setMessageListener(listener);
} else if (type == 2) {
initP2C(DeliveryMode.PERSISTENT);
if (this.durable == Parameters.PERSISTENT) {
subscriber.setMessageListener(listener);
} else {
consumer.setMessageListener(listener);
}
} else {
throw new Exception("type illegal!");
}
System.out.println("message listener is to listen......\r\nlistener name is: " + this.getProperty("LISTENER"));
} catch (Exception e) {
log.log(e, Logger.LEVEL_ERROR);
this.close();
}
}
use of javax.jms.MessageListener in project wildfly-camel by wildfly-extras.
the class JMSIntegrationTest method testMessageProviderRouteWithClientAck.
@Test
public void testMessageProviderRouteWithClientAck() throws Exception {
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").transform(simple("Hello ${body}")).toF("jms:queue:%s?connectionFactory=ConnectionFactory", QUEUE_NAME);
}
});
final List<String> result = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(4);
camelctx.start();
try {
// Get the message from the queue
ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
Connection connection = cfactory.createConnection();
final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination destination = (Destination) initialctx.lookup(QUEUE_JNDI_NAME);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public synchronized void onMessage(Message message) {
TextMessage text = (TextMessage) message;
long count = latch.getCount();
try {
// always append the message text
result.add(text.getText() + " " + (5 - count));
if (count == 4) {
// do nothing on first
} else if (count == 3) {
// recover causing a redelivery
session.recover();
} else {
// ackknowledge
message.acknowledge();
}
} catch (JMSException ex) {
result.add(ex.getMessage());
}
latch.countDown();
}
});
connection.start();
try {
ProducerTemplate producer = camelctx.createProducerTemplate();
producer.asyncSendBody("direct:start", "Message");
producer.asyncSendBody("direct:start", "Message");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertEquals("Four messages", 4, result.size());
Assert.assertEquals("Hello Message 1", result.get(0));
Assert.assertEquals("Hello Message 2", result.get(1));
Assert.assertEquals("Hello Message 3", result.get(2));
Assert.assertEquals("Hello Message 4", result.get(3));
} finally {
connection.close();
}
} finally {
camelctx.stop();
}
}
Aggregations