use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsSessionRecoverTest method doTestAsynchRecover.
/**
* Test to make sure that an Async recover works.
*
* @throws JMSException
* @throws InterruptedException
*/
public void doTestAsynchRecover() throws JMSException, InterruptedException {
final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
final String[] errorMessage = new String[] { null };
final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
MessageConsumer consumer = session.createConsumer(dest);
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage("First"));
producer.send(session.createTextMessage("Second"));
consumer.setMessageListener(new MessageListener() {
int counter;
@Override
public void onMessage(Message msg) {
counter++;
try {
TextMessage message = (TextMessage) msg;
switch(counter) {
case 1:
assertEquals("First", message.getText());
assertFalse(message.getJMSRedelivered());
message.acknowledge();
break;
case 2:
assertEquals("Second", message.getText());
assertFalse(message.getJMSRedelivered());
session.recover();
break;
case 3:
assertEquals("Second", message.getText());
assertTrue(message.getJMSRedelivered());
message.acknowledge();
doneCountDownLatch.countDown();
break;
default:
errorMessage[0] = "Got too many messages: " + counter;
doneCountDownLatch.countDown();
}
} catch (Throwable e) {
e.printStackTrace();
errorMessage[0] = "Got exception: " + e;
doneCountDownLatch.countDown();
}
}
});
connection.start();
if (doneCountDownLatch.await(5, TimeUnit.SECONDS)) {
if (errorMessage[0] != null) {
fail(errorMessage[0]);
}
} else {
fail("Timeout waiting for async message delivery to complete.");
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsSessionRecoverTest method doTestAsynchRecoverWithAutoAck.
/**
* Test to make sure that an Async recover works when using AUTO_ACKNOWLEDGE.
*
* @throws JMSException
* @throws InterruptedException
*/
public void doTestAsynchRecoverWithAutoAck() throws JMSException, InterruptedException {
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final String[] errorMessage = new String[] { null };
final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
MessageConsumer consumer = session.createConsumer(dest);
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage("First"));
producer.send(session.createTextMessage("Second"));
consumer.setMessageListener(new MessageListener() {
int counter;
@Override
public void onMessage(Message msg) {
counter++;
try {
TextMessage message = (TextMessage) msg;
switch(counter) {
case 1:
assertEquals("First", message.getText());
assertFalse(message.getJMSRedelivered());
break;
case 2:
// This should rollback the delivery of this message..
// and re-deliver.
assertEquals("Second", message.getText());
assertFalse(message.getJMSRedelivered());
session.recover();
break;
case 3:
assertEquals("Second", message.getText());
assertTrue(message.getJMSRedelivered());
doneCountDownLatch.countDown();
break;
default:
errorMessage[0] = "Got too many messages: " + counter;
doneCountDownLatch.countDown();
}
} catch (Throwable e) {
e.printStackTrace();
errorMessage[0] = "Got exception: " + e;
doneCountDownLatch.countDown();
}
}
});
connection.start();
if (doneCountDownLatch.await(5000, TimeUnit.SECONDS)) {
if (errorMessage[0] != null) {
fail(errorMessage[0]);
}
} else {
fail("Timeout waiting for async message delivery to complete.");
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class AdvisoryTempDestinationTests method testNoSlowConsumerAdvisory.
public void testNoSlowConsumerAdvisory() throws Exception {
Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue queue = s.createTemporaryQueue();
MessageConsumer consumer = s.createConsumer(queue);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
}
});
Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
// start throwing messages at the consumer
MessageProducer producer = s.createProducer(queue);
for (int i = 0; i < MESSAGE_COUNT; i++) {
BytesMessage m = s.createBytesMessage();
m.writeBytes(new byte[1024]);
producer.send(m);
}
Message msg = advisoryConsumer.receive(1000);
assertNull(msg);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class AdvisoryTests method testNoSlowConsumerAdvisory.
public void testNoSlowConsumerAdvisory() throws Exception {
Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = s.createQueue(getClass().getName());
MessageConsumer consumer = s.createConsumer(queue);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
}
});
Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
// start throwing messages at the consumer
MessageProducer producer = s.createProducer(queue);
for (int i = 0; i < MESSAGE_COUNT; i++) {
BytesMessage m = s.createBytesMessage();
m.writeBytes(new byte[1024]);
producer.send(m);
}
Message msg = advisoryConsumer.receive(1000);
assertNull(msg);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class ConsumerListenerTest method createConsumer.
protected Session createConsumer() throws JMSException {
final String consumerText = "Consumer: " + (++consumerCounter);
LOG.info("Creating consumer: " + consumerText + " on destination: " + destination);
Session answer = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = answer.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
LOG.info("Received message by: " + consumerText + " message: " + message);
}
});
return answer;
}
Aggregations