org.apache.activemq.artemis.core.transaction.impl.XidImpl

Here are the examples of the java api class org.apache.activemq.artemis.core.transaction.impl.XidImpl taken from open source projects.

1. BMFailoverTest#testFailoverOnCommit2()

View license
@Test
@BMRules(rules = { @BMRule(name = "trace clientsessionimpl commit", targetClass = "org.apache.activemq.artemis.core.client.impl.ClientSessionImpl", targetMethod = "start(javax.transaction.xa.Xid, int)", targetLocation = "AT EXIT", action = "org.apache.activemq.artemis.tests.extras.byteman.BMFailoverTest.serverToStop.getServer().stop(true)") })
public void testFailoverOnCommit2() throws Exception {
    serverToStop = liveServer;
    locator = getServerLocator().setFailoverOnInitialConnection(true);
    SimpleString inQueue = new SimpleString("inQueue");
    SimpleString outQueue = new SimpleString("outQueue");
    createSessionFactory();
    createSessionFactory2();
    // closeable will take care of closing it
    try (ClientSession session = sf.createSession(false, true, true);
        ClientProducer sendInitialProducer = session.createProducer()) {
        session.createQueue(inQueue, inQueue, null, true);
        session.createQueue(outQueue, outQueue, null, true);
        sendInitialProducer.send(inQueue, createMessage(session, 0, true));
    }
    ClientSession xaSessionRec = addClientSession(sf.createSession(true, false, false));
    ClientConsumer consumer = addClientConsumer(xaSessionRec.createConsumer(inQueue));
    byte[] globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    Xid xidRec = new XidImpl("xa2".getBytes(), 1, globalTransactionId);
    xaSessionRec.start();
    xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
    //failover is now occurring, receive, ack and end will be called whilst this is happening.
    ClientMessageImpl m = (ClientMessageImpl) consumer.receive(5000);
    assertNotNull(m);
    System.out.println("********************" + m.getIntProperty("counter"));
    //the mdb would ack the message before calling onMessage()
    m.acknowledge();
    try {
        //this may fail but thats ok, it depends on the race and when failover actually happens
        xaSessionRec.end(xidRec, XAResource.TMSUCCESS);
    } catch (XAException ignore) {
    }
    //we always reset the client on the RA
    ((ClientSessionInternal) xaSessionRec).resetIfNeeded();
    // closeable will take care of closing it
    try (ClientSession session = sf.createSession(false, true, true);
        ClientProducer sendInitialProducer = session.createProducer()) {
        sendInitialProducer.send(inQueue, createMessage(session, 0, true));
    }
    //now receive and send a message successfully
    globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    xidRec = new XidImpl("xa4".getBytes(), 1, globalTransactionId);
    xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
    Binding binding = backupServer.getServer().getPostOffice().getBinding(inQueue);
    Queue inQ = (Queue) binding.getBindable();
    m = (ClientMessageImpl) consumer.receive(5000);
    assertNotNull(m);
    //the mdb would ack the message before calling onMessage()
    m.acknowledge();
    System.out.println("********************" + m.getIntProperty("counter"));
    xaSessionRec.getXAResource().end(xidRec, XAResource.TMSUCCESS);
    xaSessionRec.getXAResource().prepare(xidRec);
    xaSessionRec.getXAResource().commit(xidRec, false);
    //let's close the consumer so anything pending is handled
    consumer.close();
    assertEquals(1, getMessageCount(inQ));
}