Author: jstrachan Date: Tue Nov 7 11:10:01 2006 New Revision: 472207 URL: http://svn.apache.org/viewvc?view=rev&rev=472207 Log: added a test case to reproduce a bad ack bug
Added: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java (with props) incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java (with props) Modified: incubator/activemq/trunk/activemq-core/pom.xml Modified: incubator/activemq/trunk/activemq-core/pom.xml URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/pom.xml?view=diff&rev=472207&r1=472206&r2=472207 ============================================================================== --- incubator/activemq/trunk/activemq-core/pom.xml (original) +++ incubator/activemq/trunk/activemq-core/pom.xml Tue Nov 7 11:10:01 2006 @@ -253,6 +253,9 @@ <!-- TODO need to get the JUnit test configured to create SSL sockets nicely via system properties --> <exclude>**/StompSslTest.*</exclude> + + <!-- TODO reproduces a bad ack bug --> + <exclude>**/RollbacksWhileConsumingLargeQueueTest.*</exclude> </excludes> </configuration> </plugin> Added: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java?view=auto&rev=472207 ============================================================================== --- incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java (added) +++ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java Tue Nov 7 11:10:01 2006 @@ -0,0 +1,75 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * 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 obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.test.rollback; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.jms.Connection; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Session; + + +public class DelegatingTransactionalMessageListener implements MessageListener { + private static final transient Log log = LogFactory.getLog(DelegatingTransactionalMessageListener.class); + + private final MessageListener underlyingListener; + private boolean transacted = true; + private int ackMode = Session.AUTO_ACKNOWLEDGE; + private Session session; + + public DelegatingTransactionalMessageListener(MessageListener underlyingListener, Connection connection, Destination destination) { + this.underlyingListener = underlyingListener; + + try { + session = connection.createSession(transacted, ackMode); + MessageConsumer consumer = session.createConsumer(destination); + consumer.setMessageListener(this); + } + catch (JMSException e) { + throw new IllegalStateException("Could not listen to " + destination, e); + } + } + + public void onMessage(Message message) { + try { + underlyingListener.onMessage(message); + session.commit(); + } + catch (Exception e) { + rollback(); + } + } + + private void rollback() { + try { + session.rollback(); + } + catch (JMSException e) { + log.error("Failed to rollback: " + e, e); + } + } + + public Session getSession() { + return session; + } +} Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/DelegatingTransactionalMessageListener.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java?view=auto&rev=472207 ============================================================================== --- incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java (added) +++ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java Tue Nov 7 11:10:01 2006 @@ -0,0 +1,97 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * 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 obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.test.rollback; + +import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch; +import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; +import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger; +import org.apache.activemq.EmbeddedBrokerTestSupport; +import org.springframework.jms.core.MessageCreator; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.Session; +import javax.jms.TextMessage; + +/** + * @version $Revision$ + */ +public class RollbacksWhileConsumingLargeQueueTest extends EmbeddedBrokerTestSupport implements MessageListener { + + protected int numberOfMessagesOnQueue = 6500; + private Connection connection; + private DelegatingTransactionalMessageListener messageListener; + private AtomicInteger counter = new AtomicInteger(0); + private CountDownLatch latch; + + public void testConsumeOnFullQueue() throws Exception { + boolean answer = latch.await(1000, TimeUnit.SECONDS); + + System.out.println("Received: " + counter.get() + " message(s)"); + assertTrue("Did not receive the latch!", answer); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + + connection = createConnection(); + connection.start(); + + // lets fill the queue up + for (int i = 0; i < numberOfMessagesOnQueue; i++) { + template.send(createMessageCreator(i)); + } + + latch = new CountDownLatch(numberOfMessagesOnQueue); + messageListener = new DelegatingTransactionalMessageListener(this, connection, destination); + } + + + @Override + protected void tearDown() throws Exception { + if (connection != null) { + connection.close(); + } + super.tearDown(); + } + + protected MessageCreator createMessageCreator(final int i) { + return new MessageCreator() { + public Message createMessage(Session session) throws JMSException { + TextMessage answer = session.createTextMessage("Message: " + i); + answer.setIntProperty("Counter", i); + return answer; + } + }; + } + + public void onMessage(Message message) { + int value = counter.incrementAndGet(); + if (value % 10 == 0) { + throw new RuntimeException("Dummy exception on message: " + value); + } + + log.info("Received message: " + value + " content: " + message); + + latch.countDown(); + } +} Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/test/rollback/RollbacksWhileConsumingLargeQueueTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain