mirror of https://github.com/apache/activemq.git
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@738566 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
43982afdfa
commit
06da208e16
|
@ -0,0 +1,99 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.activemq.broker.BrokerService;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class JmsRollbackRedeliveryTest extends TestCase {
|
||||
protected static final Log LOG = LogFactory.getLog(JmsRollbackRedeliveryTest.class);
|
||||
private boolean consumerClose = true;
|
||||
|
||||
public void testRedeliveryNoConsumerClose() throws Exception {
|
||||
consumerClose = false;
|
||||
testRedelivery();
|
||||
}
|
||||
|
||||
public void testRedelivery() throws Exception {
|
||||
|
||||
final int nbMessages = 10;
|
||||
final String destinationName = "Destination";
|
||||
|
||||
BrokerService broker = new BrokerService();
|
||||
broker.setPersistent(false);
|
||||
broker.setUseJmx(false);
|
||||
broker.start();
|
||||
|
||||
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?jms.redeliveryPolicy.maximumRedeliveries=100");
|
||||
|
||||
Connection connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
// Enqueue nbMessages messages
|
||||
{
|
||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
Destination destination = session.createQueue(destinationName);
|
||||
MessageProducer producer = session.createProducer(destination);
|
||||
for (int i = 1; i <= nbMessages; i++) {
|
||||
producer.send(session.createTextMessage("<hello id='" + i + "'/>"));
|
||||
}
|
||||
producer.close();
|
||||
session.close();
|
||||
}
|
||||
|
||||
// Consume messages and rollback transactions
|
||||
{
|
||||
AtomicInteger received = new AtomicInteger();
|
||||
Map<String, Boolean> rolledback = new ConcurrentHashMap<String, Boolean>();
|
||||
while (received.get() < nbMessages) {
|
||||
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
|
||||
Destination destination = session.createQueue(destinationName);
|
||||
MessageConsumer consumer = session.createConsumer(destination);
|
||||
TextMessage msg = (TextMessage) consumer.receive(6000000);
|
||||
if (msg != null) {
|
||||
if (msg != null && rolledback.put(msg.getText(), Boolean.TRUE) != null) {
|
||||
LOG.info("Received message " + msg.getText() + " (" + received.getAndIncrement() + ")" + msg.getJMSMessageID());
|
||||
session.commit();
|
||||
} else {
|
||||
LOG.info("Rollback message " + msg.getText() + " id: " + msg.getJMSMessageID());
|
||||
session.rollback();
|
||||
}
|
||||
}
|
||||
if (consumerClose ) {
|
||||
consumer.close();
|
||||
}
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue