|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.activemq.broker.scheduler; |
| 19 | + |
| 20 | +import jakarta.jms.Connection; |
| 21 | +import jakarta.jms.Message; |
| 22 | +import jakarta.jms.MessageConsumer; |
| 23 | +import jakarta.jms.MessageListener; |
| 24 | +import jakarta.jms.MessageProducer; |
| 25 | +import jakarta.jms.Session; |
| 26 | +import jakarta.jms.TextMessage; |
| 27 | +import org.apache.activemq.ScheduledMessage; |
| 28 | +import org.junit.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | + |
| 37 | +public class JmsSchedulerDeleteAllMessageOnStartupOptionTest extends JobSchedulerTestSupport { |
| 38 | + |
| 39 | + private static final transient Logger LOG = LoggerFactory.getLogger(JmsSchedulerDeleteAllMessageOnStartupOptionTest.class); |
| 40 | + |
| 41 | + @Override |
| 42 | + protected boolean shouldDeleteAllScheduledMessagesOnStartup() throws Exception { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testDeleteAllMessageOnRestart() throws Exception { |
| 48 | + // Send a message delayed by 8 seconds |
| 49 | + Connection connection = createConnection(); |
| 50 | + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 51 | + connection.start(); |
| 52 | + long time_ms = 10 * 1000; |
| 53 | + MessageProducer producer = session.createProducer(destination); |
| 54 | + TextMessage message = session.createTextMessage("test msg"); |
| 55 | + message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time_ms); |
| 56 | + producer.send(message); |
| 57 | + producer.close(); |
| 58 | + // Shutdown broker |
| 59 | + restartBroker(RestartType.NORMAL); |
| 60 | + // Make sure the consumer won't get the message |
| 61 | + connection = createConnection(); |
| 62 | + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 63 | + MessageConsumer consumer = session.createConsumer(destination); |
| 64 | + final int COUNT = 1; |
| 65 | + final CountDownLatch latch = new CountDownLatch(COUNT); |
| 66 | + consumer.setMessageListener(new MessageListener() { |
| 67 | + @Override |
| 68 | + public void onMessage(Message message) { |
| 69 | + latch.countDown(); |
| 70 | + } |
| 71 | + }); |
| 72 | + latch.await(20, TimeUnit.SECONDS); |
| 73 | + assertEquals(latch.getCount(), COUNT); |
| 74 | + connection.close(); |
| 75 | + } |
| 76 | +} |
0 commit comments