Search in sources :

Example 61 with MessageListener

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);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ScheduledMessage(org.apache.activemq.ScheduledMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) Test(org.junit.Test)

Example 62 with MessageListener

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());
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ScheduledMessage(org.apache.activemq.ScheduledMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) Test(org.junit.Test)

Example 63 with MessageListener

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);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ScheduledMessage(org.apache.activemq.ScheduledMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) Test(org.junit.Test)

Example 64 with MessageListener

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());
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageConsumer(javax.jms.MessageConsumer) ScheduledMessage(org.apache.activemq.ScheduledMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 65 with MessageListener

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());
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ScheduledMessage(org.apache.activemq.ScheduledMessage) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobSchedulerStoreImpl(org.apache.activemq.store.kahadb.scheduler.JobSchedulerStoreImpl) File(java.io.File) BrokerService(org.apache.activemq.broker.BrokerService) Queue(javax.jms.Queue) Session(javax.jms.Session)

Aggregations

MessageListener (javax.jms.MessageListener)146 Message (javax.jms.Message)128 MessageConsumer (javax.jms.MessageConsumer)103 Session (javax.jms.Session)97 TextMessage (javax.jms.TextMessage)95 CountDownLatch (java.util.concurrent.CountDownLatch)72 Test (org.junit.Test)71 Connection (javax.jms.Connection)60 MessageProducer (javax.jms.MessageProducer)58 JMSException (javax.jms.JMSException)57 Destination (javax.jms.Destination)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)26 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)16 ScheduledMessage (org.apache.activemq.ScheduledMessage)15 ArrayList (java.util.ArrayList)14 Topic (javax.jms.Topic)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)13