use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JobSchedulerManagementTest method testBrowseAllScheduled.
@Test
public void testBrowseAllScheduled() throws Exception {
final int COUNT = 10;
Connection connection = createConnection();
// Setup the scheduled Message
scheduleMessage(connection, TimeUnit.SECONDS.toMillis(9), COUNT);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the Browse Destination and the Reply To location
Destination requestBrowse = session.createTopic(ScheduledMessage.AMQ_SCHEDULER_MANAGEMENT_DESTINATION);
Destination browseDest = session.createTemporaryQueue();
// Create the eventual Consumer to receive the scheduled message
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch latch = new CountDownLatch(COUNT);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
}
});
// Create the "Browser"
MessageConsumer browser = session.createConsumer(browseDest);
final CountDownLatch browsedLatch = new CountDownLatch(COUNT);
browser.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
browsedLatch.countDown();
LOG.debug("Scheduled Message Browser got Message: " + message);
}
});
connection.start();
// Send the browse request
MessageProducer producer = session.createProducer(requestBrowse);
Message request = session.createMessage();
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION, ScheduledMessage.AMQ_SCHEDULER_ACTION_BROWSE);
request.setJMSReplyTo(browseDest);
producer.send(request);
// make sure the message isn't delivered early because we browsed it
Thread.sleep(2000);
assertEquals(latch.getCount(), COUNT);
// now see if we got all the scheduled messages on the browse
// destination.
latch.await(10, TimeUnit.SECONDS);
assertEquals(browsedLatch.getCount(), 0);
// now check that they all got delivered
latch.await(10, TimeUnit.SECONDS);
assertEquals(latch.getCount(), 0);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JobSchedulerManagementTest method testBrowseWindowlScheduled.
@Test
public void testBrowseWindowlScheduled() throws Exception {
final int COUNT = 10;
Connection connection = createConnection();
// Setup the scheduled Message
scheduleMessage(connection, TimeUnit.SECONDS.toMillis(5));
scheduleMessage(connection, TimeUnit.SECONDS.toMillis(10), COUNT);
scheduleMessage(connection, TimeUnit.SECONDS.toMillis(20));
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the Browse Destination and the Reply To location
Destination requestBrowse = session.createTopic(ScheduledMessage.AMQ_SCHEDULER_MANAGEMENT_DESTINATION);
Destination browseDest = session.createTemporaryQueue();
// Create the eventual Consumer to receive the scheduled message
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch latch = new CountDownLatch(COUNT + 2);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
}
});
// Create the "Browser"
MessageConsumer browser = session.createConsumer(browseDest);
final CountDownLatch browsedLatch = new CountDownLatch(COUNT);
browser.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
browsedLatch.countDown();
LOG.debug("Scheduled Message Browser got Message: " + message);
}
});
connection.start();
long start = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(6);
long end = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(15);
// Send the browse request
MessageProducer producer = session.createProducer(requestBrowse);
Message request = session.createMessage();
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION, ScheduledMessage.AMQ_SCHEDULER_ACTION_BROWSE);
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION_START_TIME, Long.toString(start));
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION_END_TIME, Long.toString(end));
request.setJMSReplyTo(browseDest);
producer.send(request);
// make sure the message isn't delivered early because we browsed it
Thread.sleep(2000);
assertEquals(COUNT + 2, latch.getCount());
// now see if we got all the scheduled messages on the browse
// destination.
latch.await(15, TimeUnit.SECONDS);
assertEquals(0, browsedLatch.getCount());
// now see if we got all the scheduled messages on the browse
// destination.
latch.await(20, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JobSchedulerManagementTest method testRemoveAllScheduled.
@Test
public void testRemoveAllScheduled() throws Exception {
final int COUNT = 5;
Connection connection = createConnection();
// Setup the scheduled Message
scheduleMessage(connection, TimeUnit.SECONDS.toMillis(6), COUNT);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the Browse Destination and the Reply To location
Destination management = session.createTopic(ScheduledMessage.AMQ_SCHEDULER_MANAGEMENT_DESTINATION);
// Create the eventual Consumer to receive the scheduled message
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();
// Send the remove request
MessageProducer producer = session.createProducer(management);
Message request = session.createMessage();
request.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION, ScheduledMessage.AMQ_SCHEDULER_ACTION_REMOVEALL);
producer.send(request);
// Now wait and see if any get delivered, none should.
latch.await(10, TimeUnit.SECONDS);
assertEquals(latch.getCount(), COUNT);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class LostScheduledMessagesTest method doTest.
private void doTest(boolean useScheduling) throws Exception {
int DELIVERY_DELAY_MS = 5000;
startBroker();
long startTime = System.currentTimeMillis();
// Send a message scheduled for delivery in 5 seconds
ConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost");
Connection connection = cf.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(QUEUE_NAME));
Message message = session.createTextMessage("test");
if (useScheduling) {
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, DELIVERY_DELAY_MS);
}
producer.send(message);
session.close();
connection.close();
broker.getServices();
// shut down broker
broker.stop();
broker.waitUntilStopped();
// Make sure that broker have stopped within delivery delay
long shutdownTime = System.currentTimeMillis();
assertTrue("Failed to shut down broker in expected time. Test results inconclusive", shutdownTime - startTime < DELIVERY_DELAY_MS);
// make sure that delivery falls into down time window
TimeUnit.MILLISECONDS.sleep(DELIVERY_DELAY_MS);
// Start new broker instance
startBroker();
final AtomicLong receiveCounter = new AtomicLong();
cf = new ActiveMQConnectionFactory("vm://localhost");
connection = cf.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE_NAME));
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
receiveCounter.incrementAndGet();
}
});
// Wait for a while to let MQ process the message
TimeUnit.MILLISECONDS.sleep(DELIVERY_DELAY_MS * 2);
session.close();
connection.close();
assertEquals(1, receiveCounter.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class SchedulerDBVersionTest method doTestScheduleRepeated.
public void doTestScheduleRepeated(File existingStore) throws Exception {
File testDir = new File("target/activemq-data/store/scheduler/versionDB");
IOHelper.deleteFile(testDir);
IOHelper.copyFile(existingStore, testDir);
final int NUMBER = 10;
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost");
for (int i = 0; i < 3; ++i) {
JobSchedulerStoreImpl scheduler = new JobSchedulerStoreImpl();
scheduler.setDirectory(testDir);
scheduler.setJournalMaxFileLength(1024 * 1024);
BrokerService broker = createBroker(scheduler);
broker.start();
broker.waitUntilStarted();
final AtomicInteger count = new AtomicInteger();
Connection connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("test.queue");
MessageConsumer consumer = session.createConsumer(queue);
final CountDownLatch latch = new CountDownLatch(NUMBER);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
LOG.info("Received scheduled message: {}", message);
latch.countDown();
count.incrementAndGet();
}
});
connection.start();
assertEquals(latch.getCount(), NUMBER);
latch.await(30, TimeUnit.SECONDS);
connection.close();
broker.stop();
broker.waitUntilStopped();
assertEquals(0, latch.getCount());
}
}
Aggregations