modify test to validate setting isDLQ flag via startup destination query options

This commit is contained in:
gtully 2017-05-03 16:17:29 +01:00
parent 35f30102a6
commit 18d05ba5e0
1 changed files with 192 additions and 199 deletions

View File

@ -1,199 +1,192 @@
/** /**
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.activemq.bugs; package org.apache.activemq.bugs;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.DeliveryMode; import javax.jms.DeliveryMode;
import javax.jms.Destination; import javax.jms.Destination;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Queue; import javax.jms.Queue;
import javax.jms.QueueBrowser; import javax.jms.QueueBrowser;
import javax.jms.Session; import javax.jms.Session;
import javax.management.MalformedObjectNameException; import javax.management.MalformedObjectNameException;
import javax.management.ObjectName; import javax.management.ObjectName;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.jmx.QueueViewMBean; import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.activemq.broker.region.policy.PolicyEntry; import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap; import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.broker.region.policy.SharedDeadLetterStrategy; import org.apache.activemq.broker.region.policy.SharedDeadLetterStrategy;
import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.leveldb.LevelDBStore; import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.util.IOHelper; import org.apache.activemq.leveldb.LevelDBStore;
import org.apache.activemq.util.Wait; import org.apache.activemq.util.IOHelper;
import org.junit.After; import org.apache.activemq.util.Wait;
import org.junit.Before; import org.junit.After;
import org.junit.Test; import org.junit.Before;
import org.slf4j.Logger; import org.junit.Test;
import org.slf4j.LoggerFactory; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Once the wire format is completed we can test against real persistence storage. /**
*/ * Once the wire format is completed we can test against real persistence storage.
public class AMQ6059Test { */
public class AMQ6059Test {
private static final Logger LOG = LoggerFactory.getLogger(AMQ6059Test.class);
private static final Logger LOG = LoggerFactory.getLogger(AMQ6059Test.class);
private BrokerService broker;
private BrokerService broker;
@Before
public void setUp() throws Exception { @Before
broker = createBroker(); public void setUp() throws Exception {
broker.start(); broker = createBroker();
broker.waitUntilStarted(); broker.start();
} broker.waitUntilStarted();
}
@After
public void tearDown() throws Exception { @After
if (broker != null) { public void tearDown() throws Exception {
broker.stop(); if (broker != null) {
broker.waitUntilStopped(); broker.stop();
} broker.waitUntilStopped();
} }
}
@Test
public void testDLQRecovery() throws Exception { @Test
public void testDLQRecovery() throws Exception {
sendMessage(new ActiveMQQueue("leveldbQueue"));
TimeUnit.SECONDS.sleep(3); sendMessage(new ActiveMQQueue("QName"));
TimeUnit.SECONDS.sleep(3);
LOG.info("### Check for expired message moving to DLQ.");
LOG.info("### Check for expired message moving to DLQ.");
Queue dlqQueue = (Queue) createDlqDestination();
verifyIsDlq(dlqQueue); Queue dlqQueue = (Queue) createDlqDestination();
verifyIsDlq(dlqQueue);
final QueueViewMBean queueViewMBean = getProxyToQueue(dlqQueue.getQueueName());
final QueueViewMBean queueViewMBean = getProxyToQueue(dlqQueue.getQueueName());
assertTrue("The message expired", Wait.waitFor(new Wait.Condition() {
@Override assertTrue("The message expired", Wait.waitFor(new Wait.Condition() {
public boolean isSatisified() throws Exception { @Override
LOG.info("DLQ stats: Enqueues {}, Dispatches {}, Expired {}, Inflight {}", public boolean isSatisified() throws Exception {
new Object[] { queueViewMBean.getEnqueueCount(), LOG.info("DLQ stats: Enqueues {}, Dispatches {}, Expired {}, Inflight {}",
queueViewMBean.getDispatchCount(), new Object[] { queueViewMBean.getEnqueueCount(),
queueViewMBean.getExpiredCount(), queueViewMBean.getDispatchCount(),
queueViewMBean.getInFlightCount()}); queueViewMBean.getExpiredCount(),
return queueViewMBean.getEnqueueCount() == 1; queueViewMBean.getInFlightCount()});
} return queueViewMBean.getEnqueueCount() == 1;
})); }
}));
verifyMessageIsRecovered(dlqQueue);
restartBroker(broker); verifyMessageIsRecovered(dlqQueue);
verifyMessageIsRecovered(dlqQueue); restartBroker();
} verifyIsDlq(dlqQueue);
verifyMessageIsRecovered(dlqQueue);
protected BrokerService createBroker() throws Exception { }
return createBrokerWithDLQ(true);
} protected BrokerService createBroker() throws Exception {
return createBrokerWithDLQ(true);
private BrokerService createBrokerWithDLQ(boolean purge) throws Exception { }
BrokerService broker = new BrokerService();
private BrokerService createBrokerWithDLQ(boolean purge) throws Exception {
File directory = new File("target/activemq-data/leveldb"); BrokerService broker = new BrokerService();
if (purge) { ActiveMQQueue dlq = new ActiveMQQueue("ActiveMQ.DLQ?isDLQ=true");
IOHelper.deleteChildren(directory);
} broker.setDestinations(new ActiveMQDestination[]{dlq});
LevelDBStore levelDBStore = new LevelDBStore(); PolicyMap pMap = new PolicyMap();
levelDBStore.setDirectory(directory);
if (purge) { SharedDeadLetterStrategy sharedDLQStrategy = new SharedDeadLetterStrategy();
levelDBStore.deleteAllMessages(); sharedDLQStrategy.setProcessNonPersistent(true);
} sharedDLQStrategy.setProcessExpired(true);
sharedDLQStrategy.setDeadLetterQueue(dlq);
PolicyMap pMap = new PolicyMap(); sharedDLQStrategy.setExpiration(10000);
SharedDeadLetterStrategy sharedDLQStrategy = new SharedDeadLetterStrategy(); PolicyEntry defaultPolicy = new PolicyEntry();
sharedDLQStrategy.setProcessNonPersistent(true); defaultPolicy.setDeadLetterStrategy(sharedDLQStrategy);
sharedDLQStrategy.setProcessExpired(true); defaultPolicy.setExpireMessagesPeriod(2000);
sharedDLQStrategy.setDeadLetterQueue(new ActiveMQQueue("ActiveMQ.DLQ")); defaultPolicy.setUseCache(false);
sharedDLQStrategy.setExpiration(10000);
pMap.put(new ActiveMQQueue(">"), defaultPolicy);
PolicyEntry defaultPolicy = new PolicyEntry(); broker.setDestinationPolicy(pMap);
defaultPolicy.setDeadLetterStrategy(sharedDLQStrategy); if (purge) {
defaultPolicy.setExpireMessagesPeriod(2000); broker.setDeleteAllMessagesOnStartup(true);
defaultPolicy.setUseCache(false); }
pMap.put(new ActiveMQQueue(">"), defaultPolicy); return broker;
broker.setDestinationPolicy(pMap); }
broker.setPersistenceAdapter(levelDBStore);
if (purge) { private void restartBroker() throws Exception {
broker.setDeleteAllMessagesOnStartup(true); broker.stop();
} broker.waitUntilStopped();
broker = createBrokerWithDLQ(false);
return broker; broker.start();
} broker.waitUntilStarted();
}
private void restartBroker(BrokerService broker) throws Exception {
broker.stop(); private void verifyMessageIsRecovered(final Queue dlqQueue) throws Exception, JMSException {
broker.waitUntilStopped(); Connection connection = createConnection();
broker = createBrokerWithDLQ(false); connection.start();
broker.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
broker.waitUntilStarted(); QueueBrowser browser = session.createBrowser(dlqQueue);
} Enumeration<?> elements = browser.getEnumeration();
assertTrue(elements.hasMoreElements());
private void verifyMessageIsRecovered(final Queue dlqQueue) throws Exception, JMSException { Message browsed = (Message) elements.nextElement();
Connection connection = createConnection(); assertNotNull("Recover message after broker restarts", browsed);
connection.start(); }
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
QueueBrowser browser = session.createBrowser(dlqQueue); private void sendMessage(Destination destination) throws Exception {
Enumeration<?> elements = browser.getEnumeration(); Connection connection = createConnection();
assertTrue(elements.hasMoreElements()); connection.start();
Message browsed = (Message) elements.nextElement(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
assertNotNull("Recover message after broker restarts", browsed); MessageProducer producer = session.createProducer(destination);
} producer.send(destination, session.createTextMessage("DLQ message"), DeliveryMode.PERSISTENT, 4, 1000);
connection.stop();
private void sendMessage(Destination destination) throws Exception { LOG.info("### Send message that will expire.");
Connection connection = createConnection(); }
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); private Connection createConnection() throws Exception {
MessageProducer producer = session.createProducer(destination); ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(broker.getVmConnectorURI());
producer.send(destination, session.createTextMessage("DLQ message"), DeliveryMode.PERSISTENT, 4, 1000); return factory.createConnection();
connection.stop(); }
LOG.info("### Send message that will expire.");
} private Destination createDlqDestination() {
return new ActiveMQQueue("ActiveMQ.DLQ");
private Connection createConnection() throws Exception { }
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(broker.getVmConnectorURI());
return factory.createConnection(); private void verifyIsDlq(Queue dlqQ) throws Exception {
} final QueueViewMBean queueViewMBean = getProxyToQueue(dlqQ.getQueueName());
assertTrue("is dlq", queueViewMBean.isDLQ());
private Destination createDlqDestination() { }
return new ActiveMQQueue("ActiveMQ.DLQ");
} private QueueViewMBean getProxyToQueue(String name) throws MalformedObjectNameException, JMSException {
ObjectName queueViewMBeanName = new ObjectName(
private void verifyIsDlq(Queue dlqQ) throws Exception { "org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + name);
final QueueViewMBean queueViewMBean = getProxyToQueue(dlqQ.getQueueName()); QueueViewMBean proxy = (QueueViewMBean) broker.getManagementContext().newProxyInstance(
assertTrue("is dlq", queueViewMBean.isDLQ()); queueViewMBeanName, QueueViewMBean.class, true);
} return proxy;
}
private QueueViewMBean getProxyToQueue(String name) throws MalformedObjectNameException, JMSException { }
ObjectName queueViewMBeanName = new ObjectName(
"org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + name);
QueueViewMBean proxy = (QueueViewMBean) broker.getManagementContext().newProxyInstance(
queueViewMBeanName, QueueViewMBean.class, true);
return proxy;
}
}