use of javax.jms.XAConnection in project activemq-artemis by apache.
the class ConcurrentDeliveryCancelTest method testConcurrentCancels.
@Test
@BMRules(rules = { @BMRule(name = "enterCancel-holdThere", targetClass = "org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl", targetMethod = "close", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ConcurrentDeliveryCancelTest.enterCancel();") })
public void testConcurrentCancels() throws Exception {
System.out.println(server.getConfiguration().getJournalLocation().toString());
server.getAddressSettingsRepository().clear();
AddressSettings settings = new AddressSettings();
settings.setMaxDeliveryAttempts(-1);
server.getAddressSettingsRepository().addMatch("#", settings);
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
cf.setReconnectAttempts(0);
cf.setRetryInterval(10);
System.out.println(".....");
for (ServerSession srvSess : server.getSessions()) {
System.out.println(srvSess);
}
String queueName = RandomUtil.randomString();
Queue queue = createQueue(queueName);
int numberOfMessages = 10000;
{
Connection connection = cf.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < numberOfMessages; i++) {
TextMessage msg = session.createTextMessage("message " + i);
msg.setIntProperty("i", i);
producer.send(msg);
}
session.commit();
connection.close();
}
for (int i = 0; i < 100; i++) {
XAConnectionFactory xacf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
final XAConnection connection = xacf.createXAConnection();
final XASession theSession = connection.createXASession();
((ActiveMQSession) theSession).getCoreSession().addMetaData("theSession", "true");
connection.start();
final MessageConsumer consumer = theSession.createConsumer(queue);
XidImpl xid = newXID();
theSession.getXAResource().start(xid, XAResource.TMNOFLAGS);
// I'm setting a small timeout just because I'm lazy to call end myself
theSession.getXAResource().setTransactionTimeout(1);
for (int msg = 0; msg < 11; msg++) {
Assert.assertNotNull(consumer.receiveNoWait());
}
System.out.println(".....");
final List<ServerSession> serverSessions = new LinkedList<>();
// We will force now the failure simultaneously from several places
for (ServerSession srvSess : server.getSessions()) {
if (srvSess.getMetaData("theSession") != null) {
System.out.println(srvSess);
serverSessions.add(srvSess);
}
}
// from Transactional reaper
resetLatches(2);
List<Thread> threads = new LinkedList<>();
threads.add(new Thread("ConsumerCloser") {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " closing consumer");
consumer.close();
System.out.println(Thread.currentThread().getName() + " closed consumer");
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(new Thread("SessionCloser") {
@Override
public void run() {
for (ServerSession sess : serverSessions) {
System.out.println("Thread " + Thread.currentThread().getName() + " starting");
try {
// A session.close could sneak in through failover or some other scenarios.
// a call to RemotingConnection.fail wasn't replicating the issue.
// I needed to call Session.close() directly to replicate what was happening in production
sess.close(true);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread " + Thread.currentThread().getName() + " done");
}
}
});
for (Thread t : threads) {
t.start();
}
Assert.assertTrue(latchEnter.await(10, TimeUnit.MINUTES));
latchFlag.countDown();
for (Thread t : threads) {
t.join(5000);
Assert.assertFalse(t.isAlive());
}
connection.close();
}
Connection connection = cf.createConnection();
try {
connection.setClientID("myID");
// I am too lazy to call end on all the transactions
Thread.sleep(5000);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
HashMap<Integer, AtomicInteger> mapCount = new HashMap<>();
while (true) {
TextMessage message = (TextMessage) consumer.receiveNoWait();
if (message == null) {
break;
}
Integer value = message.getIntProperty("i");
AtomicInteger count = mapCount.get(value);
if (count == null) {
count = new AtomicInteger(0);
mapCount.put(message.getIntProperty("i"), count);
}
count.incrementAndGet();
}
boolean failed = false;
for (int i = 0; i < numberOfMessages; i++) {
AtomicInteger count = mapCount.get(i);
if (count == null) {
System.out.println("Message " + i + " not received");
failed = true;
} else if (count.get() > 1) {
System.out.println("Message " + i + " received " + count.get() + " times");
failed = true;
}
}
Assert.assertFalse("test failed, look at the system.out of the test for more information", failed);
} finally {
connection.close();
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class FailureXATest method doTestCrashServerAfterXACommit.
private void doTestCrashServerAfterXACommit(boolean onePhase) throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
XAConnection connection = connectionFactory.createXAConnection();
try {
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = session.createQueue("Queue1");
final XASession xaSession = connection.createXASession();
MessageConsumer consumer = xaSession.createConsumer(queue);
MessageProducer producer = session.createProducer(queue);
producer.send(session.createTextMessage("hello " + 1));
session.commit();
XAResource xaResource = xaSession.getXAResource();
final Xid xid = newXID();
xaResource.start(xid, XAResource.TMNOFLAGS);
connection.start();
Assert.assertNotNull(consumer.receive(5000));
xaResource.end(xid, XAResource.TMSUCCESS);
try {
xaResource.commit(xid, onePhase);
Assert.fail("didn't get expected exception!");
} catch (XAException xae) {
if (onePhase) {
// expected error code is XAER_RMFAIL
Assert.assertEquals(XAException.XAER_RMFAIL, xae.errorCode);
} else {
// expected error code is XA_RETRY
Assert.assertEquals(XAException.XA_RETRY, xae.errorCode);
}
}
} finally {
connection.close();
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class SimpleOpenWireTest method testXAResourceCommitSuspendedNotRemoved.
@Test
public void testXAResourceCommitSuspendedNotRemoved() throws Exception {
Queue queue = null;
Xid xid = newXID();
try (XAConnection xaconnection = xaFactory.createXAConnection()) {
XASession session = xaconnection.createXASession();
queue = session.createQueue(queueName);
session.getXAResource().start(xid, XAResource.TMNOFLAGS);
session.getXAResource().end(xid, XAResource.TMSUSPEND);
XidImpl xid1 = new XidImpl(xid);
Transaction transaction = server.getResourceManager().getTransaction(xid1);
// amq5.x doesn't pass suspend flags to broker,
// directly suspend the tx
transaction.suspend();
session.getXAResource().commit(xid, true);
} catch (XAException ex) {
// ignore
} finally {
XidImpl xid1 = new XidImpl(xid);
Transaction transaction = server.getResourceManager().getTransaction(xid1);
assertNotNull(transaction);
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class SimpleOpenWireTest method testXAResourceCommittedRemoved.
@Test
public void testXAResourceCommittedRemoved() throws Exception {
Queue queue = null;
Xid xid = newXID();
try (XAConnection xaconnection = xaFactory.createXAConnection()) {
XASession session = xaconnection.createXASession();
queue = session.createQueue(queueName);
session.getXAResource().start(xid, XAResource.TMNOFLAGS);
MessageProducer producer = session.createProducer(queue);
producer.send(session.createTextMessage("xa message"));
session.getXAResource().end(xid, XAResource.TMSUCCESS);
session.getXAResource().commit(xid, true);
}
XidImpl xid1 = new XidImpl(xid);
Transaction transaction = server.getResourceManager().getTransaction(xid1);
assertNull(transaction);
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class SimpleOpenWireTest method testXASimple.
@Test
public void testXASimple() throws Exception {
XAConnection connection = xaFactory.createXAConnection();
Collection<Session> sessions = new LinkedList<>();
for (int i = 0; i < 10; i++) {
XASession session = connection.createXASession();
session.getXAResource().start(newXID(), XAResource.TMNOFLAGS);
sessions.add(session);
}
connection.close();
}
Aggregations