Search in sources :

Example 16 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailoverTest method testTimeoutOnFailover.

// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailover() throws Exception {
    locator.setCallTimeout(1000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setReconnectAttempts(300).setRetryInterval(100);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 500L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, true, true);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    final CountDownLatch latch = new CountDownLatch(10);
    final CountDownLatch latchFailed = new CountDownLatch(1);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < 500; i++) {
                ClientMessage message = session.createMessage(true);
                message.putIntProperty("counter", i);
                try {
                    System.out.println("Sent " + i);
                    producer.send(message);
                    if (i < 10) {
                        latch.countDown();
                        if (latch.getCount() == 0) {
                            latchFailed.await(10, TimeUnit.SECONDS);
                        }
                    }
                } catch (Exception e) {
                    // this is our retry
                    try {
                        if (!producer.isClosed())
                            producer.send(message);
                    } catch (ActiveMQException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    };
    Thread t = new Thread(r);
    t.start();
    Assert.assertTrue("latch released", latch.await(10, TimeUnit.SECONDS));
    crash(session);
    latchFailed.countDown();
    t.join(30000);
    if (t.isAlive()) {
        t.interrupt();
        Assert.fail("Thread still alive");
    }
    Assert.assertTrue(backupServer.getServer().waitForActivation(5, TimeUnit.SECONDS));
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    for (int i = 0; i < 500; i++) {
        ClientMessage m = consumer.receive(1000);
        Assert.assertNotNull("message #=" + i, m);
    // assertEquals(i, m.getIntProperty("counter").intValue());
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) XAException(javax.transaction.xa.XAException) Test(org.junit.Test)

Example 17 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailoverTest method testTimeoutOnFailoverTransactionRollback.

// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailoverTransactionRollback() throws Exception {
    locator.setCallTimeout(2000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setReconnectAttempts(300).setRetryInterval(100);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 5000L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, true, false, false);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    Xid xid = new XidImpl("uhuhuhu".getBytes(), 126512, "auhsduashd".getBytes());
    session.start(xid, XAResource.TMNOFLAGS);
    for (int i = 0; i < 500; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    crash(true, session);
    try {
        session.rollback(xid);
    } catch (XAException e) {
        try {
            // there is still an edge condition that we must deal with
            session.rollback(xid);
        } catch (Exception ignored) {
            log.trace(ignored.getMessage(), ignored);
        }
    }
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    ClientMessage m = consumer.receive(1000);
    Assert.assertNull(m);
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) XAException(javax.transaction.xa.XAException) Test(org.junit.Test)

Example 18 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailoverTest method testTimeoutOnFailoverConsumeBlocked.

@Test(timeout = 120000)
public void testTimeoutOnFailoverConsumeBlocked() throws Exception {
    locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setConsumerWindowSize(0).setBlockOnDurableSend(true).setAckBatchSize(0).setBlockOnAcknowledge(true).setReconnectAttempts(-1).setAckBatchSize(0);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 5000L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, true, true);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    for (int i = 0; i < 500; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("counter", i);
        message.putBooleanProperty("end", i == 499);
        producer.send(message);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(1);
    final ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    final Map<Integer, ClientMessage> received = new HashMap<>();
    Thread t = new Thread() {

        @Override
        public void run() {
            ClientMessage message = null;
            try {
                while ((message = getMessage()) != null) {
                    Integer counter = message.getIntProperty("counter");
                    received.put(counter, message);
                    try {
                        log.info("acking message = id = " + message.getMessageID() + ", counter = " + message.getIntProperty("counter"));
                        message.acknowledge();
                    } catch (ActiveMQException e) {
                        e.printStackTrace();
                        continue;
                    }
                    log.info("Acked counter = " + counter);
                    if (counter.equals(10)) {
                        latch.countDown();
                    }
                    if (received.size() == 500) {
                        endLatch.countDown();
                    }
                    if (message.getBooleanProperty("end")) {
                        break;
                    }
                }
            } catch (Exception e) {
                Assert.fail("failing due to exception " + e);
            }
        }

        private ClientMessage getMessage() {
            while (true) {
                try {
                    ClientMessage msg = consumer.receive(20000);
                    if (msg == null) {
                        log.info("Returning null message on consuming");
                    }
                    return msg;
                } catch (ActiveMQObjectClosedException oce) {
                    throw new RuntimeException(oce);
                } catch (ActiveMQException ignored) {
                    // retry
                    ignored.printStackTrace();
                }
            }
        }
    };
    t.start();
    latch.await(10, TimeUnit.SECONDS);
    log.info("crashing session");
    crash(session);
    endLatch.await(60, TimeUnit.SECONDS);
    t.join();
    Assert.assertTrue("received only " + received.size(), received.size() == 500);
    session.close();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) HashMap(java.util.HashMap) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) XAException(javax.transaction.xa.XAException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 19 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailoverTestBase method createSessionFactoryAndWaitForTopology.

protected ClientSessionFactoryInternal createSessionFactoryAndWaitForTopology(ServerLocator locator, int topologyMembers) throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(topologyMembers);
    locator.addClusterTopologyListener(new LatchClusterTopologyListener(countDownLatch));
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) locator.createSessionFactory();
    addSessionFactory(sf);
    Assert.assertTrue("topology members expected " + topologyMembers, countDownLatch.await(5, TimeUnit.SECONDS));
    return sf;
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with ClientSessionFactoryInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal in project activemq-artemis by apache.

the class FailBackManualTest method testNoAutoFailback.

@Test
public void testNoAutoFailback() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true).setReconnectAttempts(15);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = sendAndConsume(sf, true);
    CountDownSessionFailureListener listener = new CountDownSessionFailureListener(1, session);
    session.addFailureListener(listener);
    backupServer.stop();
    liveServer.crash();
    backupServer.start();
    assertTrue(listener.getLatch().await(5, TimeUnit.SECONDS));
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage message = session.createMessage(true);
    setBody(0, message);
    producer.send(message);
    session.removeFailureListener(listener);
    Thread t = new Thread(new ServerStarter(liveServer));
    t.start();
    waitForRemoteBackup(sf, 10, false, backupServer.getServer());
    assertTrue(backupServer.isStarted());
    backupServer.crash();
    waitForServerToStart(liveServer.getServer());
    assertTrue(liveServer.isStarted());
    sf.close();
    assertEquals(0, sf.numSessions());
    assertEquals(0, sf.numConnections());
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)43 Test (org.junit.Test)30 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)28 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)20 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)19 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)17 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)16 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)12 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)12 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)11 InVMNodeManager (org.apache.activemq.artemis.core.server.impl.InVMNodeManager)8 ActiveMQObjectClosedException (org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException)6 SessionFailureListener (org.apache.activemq.artemis.api.core.client.SessionFailureListener)6 ServerLocatorInternal (org.apache.activemq.artemis.core.client.impl.ServerLocatorInternal)6 XAException (javax.transaction.xa.XAException)5 ActiveMQDuplicateIdException (org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException)4 ActiveMQTransactionOutcomeUnknownException (org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException)4 ActiveMQTransactionRolledBackException (org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException)4