Search in sources :

Example 36 with ClientSessionFactoryInternal

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

the class ReattachTest method testImmediateReattach.

/*
    * Test failure on connection, but server is still up so should immediately reconnect
    */
@Test
public void testImmediateReattach() throws Exception {
    final long retryInterval = 50;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 10;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
    final int numIterations = 10;
    for (int j = 0; j < numIterations; j++) {
        ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
        final int numMessages = 100;
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
            message.putIntProperty(new SimpleString("count"), i);
            message.getBodyBuffer().writeString("aardvarks");
            producer.send(message);
        }
        ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
        RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
        conn.fail(new ActiveMQNotConnectedException());
        session.start();
        for (int i = 0; i < numMessages; i++) {
            ClientMessage message = consumer.receive(500);
            Assert.assertNotNull(message);
            Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
            Assert.assertEquals(i, message.getObjectProperty(new SimpleString("count")));
            message.acknowledge();
        }
        ClientMessage message = consumer.receiveImmediate();
        Assert.assertNull(message);
        producer.close();
        consumer.close();
    }
    session.close();
    sf.close();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) 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) Test(org.junit.Test)

Example 37 with ClientSessionFactoryInternal

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

the class MultipleLivesMultipleBackupsFailoverTest method testMultipleFailovers2LiveServers.

@Test
public void testMultipleFailovers2LiveServers() throws Exception {
    NodeManager nodeManager1 = new InVMNodeManager(!sharedStore);
    NodeManager nodeManager2 = new InVMNodeManager(!sharedStore);
    createLiveConfig(nodeManager1, 0, 3, 4, 5);
    createBackupConfig(nodeManager1, 0, 1, true, new int[] { 0, 2 }, 3, 4, 5);
    createBackupConfig(nodeManager1, 0, 2, true, new int[] { 0, 1 }, 3, 4, 5);
    createLiveConfig(nodeManager2, 3, 0);
    createBackupConfig(nodeManager2, 3, 4, true, new int[] { 3, 5 }, 0, 1, 2);
    createBackupConfig(nodeManager2, 3, 5, true, new int[] { 3, 4 }, 0, 1, 2);
    servers.get(0).start();
    waitForServerToStart(servers.get(0).getServer());
    servers.get(3).start();
    waitForServerToStart(servers.get(3).getServer());
    servers.get(1).start();
    waitForServerToStart(servers.get(1).getServer());
    servers.get(2).start();
    servers.get(4).start();
    waitForServerToStart(servers.get(4).getServer());
    servers.get(5).start();
    waitForServerToStart(servers.get(4).getServer());
    locator = getServerLocator(0).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setReconnectAttempts(15);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 4, servers.get(0).getServer());
    ClientSession session = sendAndConsume(sf, true);
    System.out.println(((ServerLocatorInternal) locator).getTopology().describe());
    Thread.sleep(500);
    servers.get(0).crash(session);
    int liveAfter0 = waitForNewLive(10000, true, servers, 1, 2);
    locator2 = getServerLocator(3).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setReconnectAttempts(15);
    ClientSessionFactoryInternal sf2 = createSessionFactoryAndWaitForTopology(locator2, 4);
    ClientSession session2 = sendAndConsume(sf2, true);
    System.setProperty("foo", "bar");
    servers.get(3).crash(session2);
    int liveAfter3 = waitForNewLive(10000, true, servers, 4, 5);
    locator.close();
    locator2.close();
    if (liveAfter0 == 2) {
        Thread.sleep(500);
        servers.get(1).stop();
        Thread.sleep(500);
        servers.get(2).stop();
    } else {
        Thread.sleep(500);
        servers.get(2).stop();
        Thread.sleep(500);
        servers.get(1).stop();
    }
    if (liveAfter3 == 4) {
        Thread.sleep(500);
        servers.get(5).stop();
        Thread.sleep(500);
        servers.get(4).stop();
    } else {
        Thread.sleep(500);
        servers.get(4).stop();
        Thread.sleep(500);
        servers.get(5).stop();
    }
}
Also used : InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) NodeManager(org.apache.activemq.artemis.core.server.NodeManager) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ServerLocatorInternal(org.apache.activemq.artemis.core.client.impl.ServerLocatorInternal) Test(org.junit.Test)

Example 38 with ClientSessionFactoryInternal

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

the class NettyFailoverTest method testFailoverWithHostAlias.

@Test(timeout = 120000)
public void testFailoverWithHostAlias() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put(TransportConstants.HOST_PROP_NAME, "127.0.0.1");
    TransportConfiguration tc = createTransportConfiguration(true, false, params);
    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(tc)).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setReconnectAttempts(15);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = createSession(sf, true, true, 0);
    session.createQueue(ADDRESS, ADDRESS, null, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    final int numMessages = 10;
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    session.start();
    crash(session);
    sendMessages(session, producer, numMessages);
    receiveMessages(consumer, 0, numMessages, true);
    session.close();
    sf.close();
    Assert.assertEquals(0, sf.numSessions());
    Assert.assertEquals(0, sf.numConnections());
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) HashMap(java.util.HashMap) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 39 with ClientSessionFactoryInternal

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

the class PagingFailoverTest method testExpireMessage.

@Test
public void testExpireMessage() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(15);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    session = sf.createSession(true, true, 0);
    session.createQueue(PagingFailoverTest.ADDRESS, PagingFailoverTest.ADDRESS, true);
    ClientProducer prod = session.createProducer(PagingFailoverTest.ADDRESS);
    final int TOTAL_MESSAGES = 1000;
    for (int i = 0; i < TOTAL_MESSAGES; i++) {
        ClientMessage msg = session.createMessage(true);
        msg.putIntProperty(new SimpleString("key"), i);
        msg.setExpiration(System.currentTimeMillis() + 1000);
        prod.send(msg);
    }
    crash(session);
    session.close();
    Queue queue = backupServer.getServer().locateQueue(ADDRESS);
    long timeout = System.currentTimeMillis() + 60000;
    while (timeout > System.currentTimeMillis() && queue.getPageSubscription().isPaging()) {
        Thread.sleep(100);
        // Simulating what would happen on expire
        queue.expireReferences();
    }
    Assert.assertFalse(queue.getPageSubscription().isPaging());
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) Test(org.junit.Test)

Example 40 with ClientSessionFactoryInternal

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

the class ReconnectTest method internalTestInterruptReconnect.

public void internalTestInterruptReconnect(final boolean isNetty, final boolean interruptMainThread) throws Exception {
    final int pingPeriod = 1000;
    ActiveMQServer server = createServer(false, isNetty);
    server.start();
    try {
        ServerLocator locator = createFactory(isNetty).setClientFailureCheckPeriod(pingPeriod).setRetryInterval(500).setRetryIntervalMultiplier(1d).setReconnectAttempts(-1).setConfirmationWindowSize(1024 * 1024);
        ClientSessionFactoryInternal factory = (ClientSessionFactoryInternal) locator.createSessionFactory();
        // One for beforeReconnecto from the Factory, and one for the commit about to be done
        final CountDownLatch latchCommit = new CountDownLatch(2);
        final ArrayList<Thread> threadToBeInterrupted = new ArrayList<>();
        factory.addFailureListener(new SessionFailureListener() {

            @Override
            public void connectionFailed(ActiveMQException exception, boolean failedOver) {
            }

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
                connectionFailed(me, failedOver);
            }

            @Override
            public void beforeReconnect(ActiveMQException exception) {
                threadToBeInterrupted.add(Thread.currentThread());
                System.out.println("Thread " + Thread.currentThread() + " reconnecting now");
                latchCommit.countDown();
            }
        });
        final ClientSessionInternal session = (ClientSessionInternal) factory.createSession();
        final AtomicInteger count = new AtomicInteger(0);
        final CountDownLatch latch = new CountDownLatch(1);
        session.addFailureListener(new SessionFailureListener() {

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver) {
                count.incrementAndGet();
                latch.countDown();
            }

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
                connectionFailed(me, failedOver);
            }

            @Override
            public void beforeReconnect(final ActiveMQException exception) {
            }
        });
        server.stop();
        Thread tcommitt = new Thread() {

            @Override
            public void run() {
                latchCommit.countDown();
                try {
                    session.commit();
                } catch (ActiveMQException e) {
                    e.printStackTrace();
                }
            }
        };
        tcommitt.start();
        assertTrue(latchCommit.await(10, TimeUnit.SECONDS));
        // There should be only one thread
        assertEquals(1, threadToBeInterrupted.size());
        if (interruptMainThread) {
            tcommitt.interrupt();
        } else {
            for (Thread tint : threadToBeInterrupted) {
                tint.interrupt();
            }
        }
        tcommitt.join(5000);
        assertFalse(tcommitt.isAlive());
        locator.close();
    } finally {
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

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