use of javax.jms.MessageListener in project cxf by apache.
the class MessageListenerTest method testLocalTransaction.
@Test
public void testLocalTransaction() throws JMSException, XAException, InterruptedException {
Connection connection = createConnection("brokerLocalTransaction");
Queue dest = JMSUtil.createQueue(connection, "test");
MessageListener listenerHandler = new TestMessageListener();
AbstractMessageListenerContainer container = new MessageListenerContainer(connection, dest, listenerHandler);
container.setTransacted(true);
container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
container.start();
testTransactionalBehaviour(connection, dest);
container.stop();
connection.close();
}
use of javax.jms.MessageListener in project cxf by apache.
the class MessageListenerTest method testConnectionProblemXA.
@Test
public void testConnectionProblemXA() 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 = createMock(ExceptionListener.class);
Capture<JMSException> captured = newCapture();
exListener.onException(capture(captured));
expectLastCall();
replay(exListener);
//
PollingMessageListenerContainer container = new PollingMessageListenerContainer(connection, dest, listenerHandler, exListener);
container.setTransacted(false);
container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
container.setTransactionManager(transactionManager);
// Simulate connection problem
connection.close();
container.start();
Awaitility.await().until(() -> !container.isRunning());
verify(exListener);
JMSException ex = captured.getValue();
// Closing the pooled connection will result in a NPE when using it
Assert.assertEquals("Wrapped exception. null", ex.getMessage());
}
use of javax.jms.MessageListener in project cxf by apache.
the class MessageListenerTest method testNoTransaction.
@Test
public void testNoTransaction() throws JMSException, XAException, InterruptedException {
Connection connection = createConnection("brokerNoTransaction");
Queue dest = JMSUtil.createQueue(connection, "test");
MessageListener listenerHandler = new TestMessageListener();
AbstractMessageListenerContainer container = new MessageListenerContainer(connection, dest, listenerHandler);
container.setTransacted(false);
container.setAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
container.start();
assertNumMessagesInQueue("At the start the queue should be empty", connection, dest, 0, 0);
sendMessage(connection, dest, OK);
assertNumMessagesInQueue("This message should be committed", connection, dest, 0, 1000);
sendMessage(connection, dest, FAIL);
assertNumMessagesInQueue("Even when an exception occurs the message should be committed", connection, dest, 0, 1000);
container.stop();
connection.close();
}
use of javax.jms.MessageListener in project rabbitmq-jms-client by rabbitmq.
the class RecoverMessagesIT method testRecoverTextMessageAsyncSync.
@Test
public void testRecoverTextMessageAsyncSync() throws Exception {
final ArrayList<Message> messages = new ArrayList<Message>();
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = queueSession.createTextMessage(MESSAGE);
queueSender.send(message);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
final CountDownLatch latch = new CountDownLatch(2);
queueReceiver.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
messages.add(message);
latch.countDown();
}
});
// allow subscription to take place
Thread.sleep(100);
// we should have received one message
assertEquals(1, messages.size());
TextMessage tmsg1 = (TextMessage) messages.get(0);
assertFalse(tmsg1.getJMSRedelivered());
queueSession.recover();
latch.await(1000, TimeUnit.MILLISECONDS);
// we should have received two messages
// There is no synchronisation so no guarantee we see the latest messages!
assertEquals(2, messages.size());
TextMessage tmsg2 = (TextMessage) messages.get(1);
assertEquals(tmsg1, tmsg2);
assertTrue(tmsg2.getJMSRedelivered());
tmsg2.acknowledge();
queueReceiver.setMessageListener(null);
TextMessage tmsg3 = (TextMessage) queueReceiver.receiveNoWait();
assertNull(tmsg3);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsBenchmark method testConcurrentSendReceive.
/**
* @throws Throwable
*/
public void testConcurrentSendReceive() throws Throwable {
final Semaphore connectionsEstablished = new Semaphore(1 - (CONSUMER_COUNT + PRODUCER_COUNT));
final Semaphore workerDone = new Semaphore(1 - (CONSUMER_COUNT + PRODUCER_COUNT));
final CountDownLatch sampleTimeDone = new CountDownLatch(1);
final AtomicInteger producedMessages = new AtomicInteger(0);
final AtomicInteger receivedMessages = new AtomicInteger(0);
final Callable<Object> producer = new Callable<Object>() {
@Override
public Object call() throws JMSException, InterruptedException {
Connection connection = factory.createConnection();
connections.add(connection);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
BytesMessage message = session.createBytesMessage();
message.writeBytes(new byte[1024]);
connection.start();
connectionsEstablished.release();
while (!sampleTimeDone.await(0, TimeUnit.MILLISECONDS)) {
producer.send(message);
producedMessages.incrementAndGet();
}
connection.close();
workerDone.release();
return null;
}
};
final Callable<Object> consumer = new Callable<Object>() {
@Override
public Object call() throws JMSException, InterruptedException {
Connection connection = factory.createConnection();
connections.add(connection);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message msg) {
receivedMessages.incrementAndGet();
}
});
connection.start();
connectionsEstablished.release();
sampleTimeDone.await();
connection.close();
workerDone.release();
return null;
}
};
final Throwable[] workerError = new Throwable[1];
for (int i = 0; i < PRODUCER_COUNT; i++) {
new Thread("Producer:" + i) {
@Override
public void run() {
try {
producer.call();
} catch (Throwable e) {
e.printStackTrace();
workerError[0] = e;
}
}
}.start();
}
for (int i = 0; i < CONSUMER_COUNT; i++) {
new Thread("Consumer:" + i) {
@Override
public void run() {
try {
consumer.call();
} catch (Throwable e) {
e.printStackTrace();
workerError[0] = e;
}
}
}.start();
}
LOG.info(getName() + ": Waiting for Producers and Consumers to startup.");
connectionsEstablished.acquire();
LOG.info("Producers and Consumers are now running. Waiting for system to reach steady state: " + (SAMPLE_DELAY / 1000.0f) + " seconds");
Thread.sleep(1000 * 10);
LOG.info("Starting sample: " + SAMPLES + " each lasting " + (SAMPLE_DURATION / 1000.0f) + " seconds");
for (int i = 0; i < SAMPLES; i++) {
long start = System.currentTimeMillis();
producedMessages.set(0);
receivedMessages.set(0);
Thread.sleep(SAMPLE_DURATION);
long end = System.currentTimeMillis();
int r = receivedMessages.get();
int p = producedMessages.get();
LOG.info("published: " + p + " msgs at " + (p * 1000f / (end - start)) + " msgs/sec, " + "consumed: " + r + " msgs at " + (r * 1000f / (end - start)) + " msgs/sec");
}
LOG.info("Sample done.");
sampleTimeDone.countDown();
workerDone.acquire();
if (workerError[0] != null) {
throw workerError[0];
}
}
Aggregations