|
| 1 | +package org.apache.activemq.broker.scheduler; |
| 2 | + |
| 3 | +import jakarta.jms.Connection; |
| 4 | +import jakarta.jms.Message; |
| 5 | +import jakarta.jms.MessageConsumer; |
| 6 | +import jakarta.jms.MessageListener; |
| 7 | +import jakarta.jms.MessageProducer; |
| 8 | +import jakarta.jms.Session; |
| 9 | +import jakarta.jms.TextMessage; |
| 10 | +import org.apache.activemq.ScheduledMessage; |
| 11 | +import org.apache.activemq.broker.BrokerService; |
| 12 | +import org.apache.activemq.store.kahadb.scheduler.JobSchedulerStoreImpl; |
| 13 | +import org.junit.Test; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.util.concurrent.CountDownLatch; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +public class JmsSchedulerDeleteAllMessageOptionTest extends JobSchedulerTestSupport { |
| 23 | + |
| 24 | + private static final transient Logger LOG = LoggerFactory.getLogger(JmsSchedulerDeleteAllMessageOptionTest.class); |
| 25 | + |
| 26 | + @Override |
| 27 | + protected boolean shouldDeleteAllScheduledMessages() throws Exception { |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testDeleteAllMessageOnRestart() throws Exception { |
| 33 | + // Send a message delayed by 8 seconds |
| 34 | + Connection connection = createConnection(); |
| 35 | + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 36 | + connection.start(); |
| 37 | + long time_ms = 10 * 1000; |
| 38 | + MessageProducer producer = session.createProducer(destination); |
| 39 | + TextMessage message = session.createTextMessage("test msg"); |
| 40 | + message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time_ms); |
| 41 | + producer.send(message); |
| 42 | + producer.close(); |
| 43 | + // Shutdown broker |
| 44 | + restartBroker(RestartType.NORMAL); |
| 45 | + // Make sure the consumer won't get the message |
| 46 | + connection = createConnection(); |
| 47 | + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 48 | + MessageConsumer consumer = session.createConsumer(destination); |
| 49 | + final int COUNT = 1; |
| 50 | + final CountDownLatch latch = new CountDownLatch(COUNT); |
| 51 | + consumer.setMessageListener(new MessageListener() { |
| 52 | + @Override |
| 53 | + public void onMessage(Message message) { |
| 54 | + latch.countDown(); |
| 55 | + } |
| 56 | + }); |
| 57 | + latch.await(20, TimeUnit.SECONDS); |
| 58 | + assertEquals(latch.getCount(), COUNT); |
| 59 | + connection.close(); |
| 60 | + } |
| 61 | +} |
0 commit comments