| 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.bugs;
|
| 19 |
|
| 20 |
import static org.junit.Assert.assertEquals;
|
| 21 |
import static org.junit.Assert.assertTrue;
|
| 22 |
|
| 23 |
import java.util.ArrayList;
|
| 24 |
import java.util.List;
|
| 25 |
|
| 26 |
import javax.jms.Connection;
|
| 27 |
import javax.jms.ConnectionFactory;
|
| 28 |
import javax.jms.Destination;
|
| 29 |
import javax.jms.Message;
|
| 30 |
import javax.jms.MessageConsumer;
|
| 31 |
import javax.jms.MessageProducer;
|
| 32 |
import javax.jms.Session;
|
| 33 |
import javax.jms.TextMessage;
|
| 34 |
|
| 35 |
import org.apache.activemq.ActiveMQConnectionFactory;
|
| 36 |
import org.apache.activemq.broker.BrokerService;
|
| 37 |
import org.apache.activemq.command.ActiveMQQueue;
|
| 38 |
import org.junit.After;
|
| 39 |
import org.junit.AfterClass;
|
| 40 |
import org.junit.Before;
|
| 41 |
import org.junit.BeforeClass;
|
| 42 |
import org.junit.Test;
|
| 43 |
|
| 44 |
public class RawRollbackTests {
|
| 45 |
|
| 46 |
private static ConnectionFactory connectionFactory;
|
| 47 |
private static Destination queue;
|
| 48 |
private static BrokerService broker;
|
| 49 |
|
| 50 |
@BeforeClass
|
| 51 |
public static void clean() throws Exception {
|
| 52 |
broker = new BrokerService();
|
| 53 |
broker.setDeleteAllMessagesOnStartup(true);
|
| 54 |
broker.setUseJmx(true);
|
| 55 |
broker.start();
|
| 56 |
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
|
| 57 |
connectionFactory.setBrokerURL("vm://localhost?async=false&waitForStart=5000&jms.prefetchPolicy.all=0");
|
| 58 |
RawRollbackTests.connectionFactory = connectionFactory;
|
| 59 |
queue = new ActiveMQQueue("queue");
|
| 60 |
}
|
| 61 |
|
| 62 |
@AfterClass
|
| 63 |
public static void close() throws Exception {
|
| 64 |
broker.stop();
|
| 65 |
}
|
| 66 |
|
| 67 |
@Before
|
| 68 |
public void clearData() throws Exception {
|
| 69 |
getMessages(false); // drain queue
|
| 70 |
convertAndSend("foo");
|
| 71 |
convertAndSend("bar");
|
| 72 |
}
|
| 73 |
|
| 74 |
|
| 75 |
@After
|
| 76 |
public void checkPostConditions() throws Exception {
|
| 77 |
|
| 78 |
Thread.sleep(1000L);
|
| 79 |
List<String> list = getMessages(false);
|
| 80 |
assertEquals(2, list.size());
|
| 81 |
|
| 82 |
}
|
| 83 |
|
| 84 |
@Test
|
| 85 |
public void testReceiveMessages() throws Exception {
|
| 86 |
|
| 87 |
List<String> list = getMessages(true);
|
| 88 |
assertEquals(2, list.size());
|
| 89 |
assertTrue(list.contains("foo"));
|
| 90 |
|
| 91 |
}
|
| 92 |
|
| 93 |
private void convertAndSend(String msg) throws Exception {
|
| 94 |
Connection connection = connectionFactory.createConnection();
|
| 95 |
connection.start();
|
| 96 |
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
|
| 97 |
MessageProducer producer = session.createProducer(queue);
|
| 98 |
producer.send(session.createTextMessage(msg));
|
| 99 |
producer.close();
|
| 100 |
session.commit();
|
| 101 |
session.close();
|
| 102 |
connection.close();
|
| 103 |
}
|
| 104 |
|
| 105 |
private List<String> getMessages(boolean rollback) throws Exception {
|
| 106 |
Connection connection = connectionFactory.createConnection();
|
| 107 |
connection.start();
|
| 108 |
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
|
| 109 |
String next = "";
|
| 110 |
List<String> msgs = new ArrayList<String>();
|
| 111 |
while (next != null) {
|
| 112 |
next = (String) receiveAndConvert(session);
|
| 113 |
if (next != null)
|
| 114 |
msgs.add(next);
|
| 115 |
}
|
| 116 |
if (rollback) {
|
| 117 |
session.rollback();
|
| 118 |
} else {
|
| 119 |
session.commit();
|
| 120 |
}
|
| 121 |
session.close();
|
| 122 |
connection.close();
|
| 123 |
return msgs;
|
| 124 |
}
|
| 125 |
|
| 126 |
private String receiveAndConvert(Session session) throws Exception {
|
| 127 |
MessageConsumer consumer = session.createConsumer(queue);
|
| 128 |
Message message = consumer.receive(100L);
|
| 129 |
consumer.close();
|
| 130 |
if (message==null) {
|
| 131 |
return null;
|
| 132 |
}
|
| 133 |
return ((TextMessage)message).getText();
|
| 134 |
}
|
| 135 |
}
|