Search in sources :

Example 36 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal 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 ClientSessionInternal

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

the class AsynchronousFailoverTest method runTest.

private void runTest(final TestRunner runnable) throws Throwable {
    final int numIts = 1;
    try {
        for (int i = 0; i < numIts; i++) {
            AsynchronousFailoverTest.log.info("Iteration " + i);
            ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(15).setConfirmationWindowSize(10 * 1024 * 1024);
            sf = createSessionFactoryAndWaitForTopology(locator, 2);
            try {
                ClientSession createSession = sf.createSession(true, true);
                createSession.createQueue(FailoverTestBase.ADDRESS, FailoverTestBase.ADDRESS, (SimpleString) null, true);
                RemotingConnection conn = ((ClientSessionInternal) createSession).getConnection();
                Thread t = new Thread(runnable);
                t.setName("MainTEST");
                t.start();
                long randomDelay = (long) (2000 * Math.random());
                AsynchronousFailoverTest.log.info("Sleeping " + randomDelay);
                Thread.sleep(randomDelay);
                AsynchronousFailoverTest.log.info("Failing asynchronously");
                // Simulate failure on connection
                synchronized (lockFail) {
                    if (log.isDebugEnabled()) {
                        log.debug("#test crashing test");
                    }
                    crash(createSession);
                }
                /*if (listener != null)
               {
                  boolean ok = listener.latch.await(10000, TimeUnit.MILLISECONDS);

                  Assert.assertTrue(ok);
               }*/
                runnable.setFailed();
                AsynchronousFailoverTest.log.info("Fail complete");
                t.join(TimeUnit.SECONDS.toMillis(60));
                if (t.isAlive()) {
                    System.out.println(threadDump("Thread still running from the test"));
                    t.interrupt();
                    fail("Test didn't complete successful, thread still running");
                }
                runnable.checkForExceptions();
                createSession.close();
                Assert.assertEquals(0, sf.numSessions());
                locator.close();
            } finally {
                locator.close();
                Assert.assertEquals(0, sf.numConnections());
            }
            if (i != numIts - 1) {
                tearDown();
                runnable.checkForExceptions();
                runnable.reset();
                setUp();
            }
        }
    } finally {
    }
}
Also used : 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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Example 38 with ClientSessionInternal

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

the class NettyAsynchronousReattachTest method crash.

@Override
protected void crash(final ClientSession... sessions) throws Exception {
    for (ClientSession session : sessions) {
        log.debug("Crashing session " + session);
        ClientSessionInternal internalSession = (ClientSessionInternal) session;
        internalSession.getConnection().fail(new ActiveMQNotConnectedException("oops"));
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)

Example 39 with ClientSessionInternal

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

the class ActiveMQRAManagedConnection method getXAResource.

/**
 * Get the XAResource for the connection.
 *
 * @return The XAResource for the connection.
 * @throws ResourceException XA transaction not supported
 */
@Override
public XAResource getXAResource() throws ResourceException {
    if (logger.isTraceEnabled()) {
        ActiveMQRALogger.LOGGER.trace("getXAResource()");
    }
    // 
    if (xaResource == null) {
        ClientSessionInternal csi = (ClientSessionInternal) xaSession.getXAResource();
        ActiveMQRAXAResource activeMQRAXAResource = new ActiveMQRAXAResource(this, xaSession.getXAResource());
        Map<String, Object> xaResourceProperties = new HashMap<>();
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_JNDI_NAME, ra.getJndiName());
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_NODE_ID, csi.getNodeId());
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_PRODUCT_NAME, ActiveMQResourceAdapter.PRODUCT_NAME);
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_PRODUCT_VERSION, VersionLoader.getVersion().getFullVersion());
        xaResource = ServiceUtils.wrapXAResource(activeMQRAXAResource, xaResourceProperties);
    }
    if (logger.isTraceEnabled()) {
        ActiveMQRALogger.LOGGER.trace("XAResource=" + xaResource);
    }
    return xaResource;
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) HashMap(java.util.HashMap)

Example 40 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal 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

ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)46 Test (org.junit.Test)35 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)21 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)20 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)17 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)13 Connection (javax.jms.Connection)12 Session (javax.jms.Session)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)11 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)10 MessageConsumer (javax.jms.MessageConsumer)8 MessageProducer (javax.jms.MessageProducer)8 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 TextMessage (javax.jms.TextMessage)7 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)7 XAConnection (javax.jms.XAConnection)5 XASession (javax.jms.XASession)5