use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsSchedulerTest method testScheduleRepeated.
@Test
public void testScheduleRepeated() throws Exception {
final int NUMBER = 10;
final AtomicInteger count = new AtomicInteger();
Connection connection = createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch latch = new CountDownLatch(NUMBER);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
count.incrementAndGet();
}
});
connection.start();
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("test msg");
long time = 1000;
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 500);
message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, NUMBER - 1);
producer.send(message);
producer.close();
assertEquals(latch.getCount(), NUMBER);
latch.await(10, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
// wait a little longer - make sure we only get NUMBER of replays
Thread.sleep(1000);
assertEquals(NUMBER, count.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsSchedulerTest method testSchedule.
@Test
public void testSchedule() throws Exception {
final int COUNT = 1;
Connection connection = createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch latch = new CountDownLatch(COUNT);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
}
});
connection.start();
long time = 5000;
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("test msg");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
producer.send(message);
producer.close();
// make sure the message isn't delivered early
Thread.sleep(2000);
assertEquals(latch.getCount(), COUNT);
latch.await(5, TimeUnit.SECONDS);
assertEquals(latch.getCount(), 0);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MessageConsumerTest method testCloseAfterStop.
// Test that stop doesn't in any way break subsequent close
@Test
public void testCloseAfterStop() throws Exception {
Connection producerConnection = null;
Connection consumerConnection = null;
try {
producerConnection = createConnection();
consumerConnection = createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer queueProducer = producerSession.createProducer(queue1);
MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
MessageListener myListener = new MessageListener() {
@Override
public void onMessage(final Message message) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// Ignore
}
}
};
queueConsumer.setMessageListener(myListener);
consumerConnection.start();
for (int i = 0; i < 100; i++) {
queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
}
consumerConnection.stop();
consumerConnection.close();
consumerConnection = null;
} finally {
if (producerConnection != null) {
producerConnection.close();
}
if (consumerConnection != null) {
consumerConnection.close();
}
removeAllMessages(queue1.getQueueName(), true);
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MessageConsumerTest method testRelayMessage.
@Test
public void testRelayMessage() throws Exception {
Connection conn = null;
try {
conn = createConnection();
conn.start();
final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer prod = sess.createProducer(queue2);
MessageConsumer cons = sess.createConsumer(queue1);
final int numMessages = 100;
class MyListener implements MessageListener {
boolean failed;
int count;
@Override
public synchronized void onMessage(final Message m) {
try {
prod.send(m);
count++;
if (count == numMessages) {
notify();
}
} catch (JMSException e) {
failed = true;
}
}
synchronized void waitForMessages() throws Exception {
while (count < numMessages) {
this.wait();
}
}
}
MyListener listener = new MyListener();
cons.setMessageListener(listener);
final Session sess2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod2 = sess2.createProducer(queue1);
for (int i = 0; i < numMessages; i++) {
prod2.send(sess2.createMessage());
}
listener.waitForMessages();
conn.close();
ProxyAssertSupport.assertFalse(listener.failed);
} finally {
if (conn != null) {
conn.close();
}
removeAllMessages(queue2.getQueueName(), true);
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MessageConsumerTest method testStopConnectionDuringOnMessage.
@Test
public void testStopConnectionDuringOnMessage() throws Exception {
if (log.isTraceEnabled()) {
log.trace("testStopConnectionWhileOnMessageIsExecuting");
}
final AtomicInteger messagesReceived = new AtomicInteger(0);
CountDownLatch messagesReceivedLatch = new CountDownLatch(1);
MessageListener myListener = message -> {
if (messagesReceived.incrementAndGet() == 10) {
messagesReceivedLatch.countDown();
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
// Ignore
}
}
};
Connection producerConnection = null;
Connection consumerConnection = null;
try {
producerConnection = createConnection();
consumerConnection = createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer queueProducer = producerSession.createProducer(queue1);
MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
queueConsumer.setMessageListener(myListener);
log.trace("Starting consumer connection");
consumerConnection.start();
final int MESSAGE_COUNT = 100;
log.trace("Sending the first batch of messages");
for (int i = 0; i < MESSAGE_COUNT / 2; i++) {
queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
}
messagesReceivedLatch.await(500, TimeUnit.MILLISECONDS);
log.trace("Stopping consumer connection");
consumerConnection.stop();
int countAfterStop = messagesReceived.get();
ProxyAssertSupport.assertTrue("Should have received some messages before stopping", countAfterStop > 0);
log.trace("Sending the second batch of messages");
for (int i = MESSAGE_COUNT / 2; i < MESSAGE_COUNT; i++) {
queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
}
ProxyAssertSupport.assertFalse("Should not receive any messages after the connection has been stopped", Wait.waitFor(() -> messagesReceived.get() > countAfterStop, 2000, 1000));
log.trace("Restarting consumer connection");
consumerConnection.start();
ProxyAssertSupport.assertTrue("Should have received all messages after restarting", Wait.waitFor(() -> messagesReceived.get() == MESSAGE_COUNT, 15000, 100));
} finally {
if (producerConnection != null) {
producerConnection.close();
}
if (consumerConnection != null) {
consumerConnection.close();
}
removeAllMessages(queue1.getQueueName(), true);
}
}
Aggregations