Search in sources :

Example 16 with Packet

use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.

the class PingTest method testClientFailureNoServerPing.

/*
   * Test the client triggering failure due to no ping from server received in time
   */
@Test
public void testClientFailureNoServerPing() throws Exception {
    // server must received at least one ping from the client to pass
    // so that the server connection TTL is configured with the client value
    final CountDownLatch pingOnServerLatch = new CountDownLatch(2);
    server.getRemotingService().addIncomingInterceptor(new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
            if (packet.getType() == PacketImpl.PING) {
                pingOnServerLatch.countDown();
            }
            return true;
        }
    });
    TransportConfiguration transportConfig = new TransportConfiguration(INVM_CONNECTOR_FACTORY);
    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(transportConfig));
    locator.setClientFailureCheckPeriod(PingTest.CLIENT_FAILURE_CHECK_PERIOD);
    locator.setConnectionTTL(PingTest.CLIENT_FAILURE_CHECK_PERIOD * 2);
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(false, true, true);
    Assert.assertEquals(1, ((ClientSessionFactoryInternal) csf).numConnections());
    final CountDownLatch clientLatch = new CountDownLatch(1);
    SessionFailureListener clientListener = new SessionFailureListener() {

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

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

        @Override
        public void beforeReconnect(final ActiveMQException exception) {
        }
    };
    final CountDownLatch serverLatch = new CountDownLatch(1);
    CloseListener serverListener = new CloseListener() {

        @Override
        public void connectionClosed() {
            serverLatch.countDown();
        }
    };
    session.addFailureListener(clientListener);
    CoreRemotingConnection serverConn = null;
    while (serverConn == null) {
        Set<RemotingConnection> conns = server.getRemotingService().getConnections();
        if (!conns.isEmpty()) {
            serverConn = (CoreRemotingConnection) server.getRemotingService().getConnections().iterator().next();
        } else {
            // It's async so need to wait a while
            Thread.sleep(10);
        }
    }
    serverConn.addCloseListener(serverListener);
    Assert.assertTrue("server has not received any ping from the client", pingOnServerLatch.await(4000, TimeUnit.MILLISECONDS));
    // we let the server receives at least 1 ping (so that it uses the client ConnectionTTL value)
    // Setting the handler to null will prevent server sending pings back to client
    serverConn.getChannel(0, -1).setHandler(null);
    Assert.assertTrue(clientLatch.await(8 * PingTest.CLIENT_FAILURE_CHECK_PERIOD, TimeUnit.MILLISECONDS));
    // Server connection will be closed too, when client closes client side connection after failure is detected
    Assert.assertTrue(serverLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval(), TimeUnit.MILLISECONDS));
    long start = System.currentTimeMillis();
    while (true) {
        if (!server.getRemotingService().getConnections().isEmpty() && System.currentTimeMillis() - start < 10000) {
            Thread.sleep(500);
        } else {
            break;
        }
    }
    Assert.assertTrue(server.getRemotingService().getConnections().isEmpty());
    session.close();
    csf.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 17 with Packet

use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.

the class PingTest method testNoPingingOnInVMConnection.

/*
    * Test that pinging is disabled for in-vm connection when using the default settings
    */
@Test
public void testNoPingingOnInVMConnection() throws Exception {
    // server should receive one and only one ping from the client so that
    // the server connection TTL is configured with the client value
    final CountDownLatch requiredPings = new CountDownLatch(1);
    final CountDownLatch unwantedPings = new CountDownLatch(2);
    server.getRemotingService().addIncomingInterceptor(new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
            if (packet.getType() == PacketImpl.PING) {
                Assert.assertEquals(ActiveMQClient.DEFAULT_CONNECTION_TTL_INVM, ((Ping) packet).getConnectionTTL());
                unwantedPings.countDown();
                requiredPings.countDown();
            }
            return true;
        }
    });
    TransportConfiguration transportConfig = new TransportConfiguration("org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory");
    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(transportConfig));
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(false, true, true);
    Assert.assertEquals(1, ((ClientSessionFactoryInternal) csf).numConnections());
    Assert.assertTrue("server didn't received an expected ping from the client", requiredPings.await(5000, TimeUnit.MILLISECONDS));
    Assert.assertFalse("server received an unexpected ping from the client", unwantedPings.await(ActiveMQClient.DEFAULT_CONNECTION_TTL * 2, TimeUnit.MILLISECONDS));
    session.close();
    csf.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Ping(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.Ping) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) CountDownLatch(java.util.concurrent.CountDownLatch) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 18 with Packet

use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.

the class ActiveMQSessionContext method xaEnd.

@Override
public void xaEnd(Xid xid, int flags) throws XAException, ActiveMQException {
    Packet packet;
    if (flags == XAResource.TMSUSPEND) {
        packet = new PacketImpl(PacketImpl.SESS_XA_SUSPEND);
    } else if (flags == XAResource.TMSUCCESS) {
        packet = new SessionXAEndMessage(xid, false);
    } else if (flags == XAResource.TMFAIL) {
        packet = new SessionXAEndMessage(xid, true);
    } else {
        throw new XAException(XAException.XAER_INVAL);
    }
    SessionXAResponseMessage response = (SessionXAResponseMessage) sessionChannel.sendBlocking(packet, PacketImpl.SESS_XA_RESP);
    if (response.isError()) {
        throw new XAException(response.getResponseCode());
    }
}
Also used : SessionXAResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionXAResponseMessage) Packet(org.apache.activemq.artemis.core.protocol.core.Packet) XAException(javax.transaction.xa.XAException) SessionXAEndMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionXAEndMessage)

Example 19 with Packet

use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.

the class ActiveMQSessionContext method recreateSession.

@Override
public void recreateSession(final String username, final String password, final int minLargeMessageSize, final boolean xa, final boolean autoCommitSends, final boolean autoCommitAcks, final boolean preAcknowledge) throws ActiveMQException {
    Packet createRequest = newCreateSession(username, password, minLargeMessageSize, xa, autoCommitSends, autoCommitAcks, preAcknowledge);
    boolean retry;
    do {
        try {
            getCreateChannel().sendBlocking(createRequest, PacketImpl.CREATESESSION_RESP);
            retry = false;
        } catch (ActiveMQException e) {
            // the session was created while its server was starting, retry it:
            if (e.getType() == ActiveMQExceptionType.SESSION_CREATION_REJECTED) {
                ActiveMQClientLogger.LOGGER.retryCreateSessionSeverStarting(name);
                retry = true;
                // sleep a little bit to avoid spinning too much
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    throw e;
                }
            } else {
                throw e;
            }
        }
    } while (retry && !session.isClosing());
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 20 with Packet

use of org.apache.activemq.artemis.core.protocol.core.Packet in project activemq-artemis by apache.

the class ActiveMQSessionContext method reattachOnNewConnection.

@Override
public boolean reattachOnNewConnection(RemotingConnection newConnection) throws ActiveMQException {
    this.remotingConnection = newConnection;
    sessionChannel.transferConnection((CoreRemotingConnection) newConnection);
    Packet request = new ReattachSessionMessage(name, sessionChannel.getLastConfirmedCommandID());
    Channel channel1 = getCoreConnection().getChannel(1, -1);
    ReattachSessionResponseMessage response = (ReattachSessionResponseMessage) channel1.sendBlocking(request, PacketImpl.REATTACH_SESSION_RESP);
    if (response.isReattached()) {
        ActiveMQClientLogger.LOGGER.replayingCommands(sessionChannel.getID(), response.getLastConfirmedCommandID());
        // The session was found on the server - we reattached transparently ok
        sessionChannel.replayCommands(response.getLastConfirmedCommandID());
        return true;
    } else {
        ActiveMQClientLogger.LOGGER.reconnectCreatingNewSession(sessionChannel.getID());
        sessionChannel.clearCommands();
        return false;
    }
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ReattachSessionMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ReattachSessionMessage) Channel(org.apache.activemq.artemis.core.protocol.core.Channel) ReattachSessionResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ReattachSessionResponseMessage)

Aggregations

Packet (org.apache.activemq.artemis.core.protocol.core.Packet)35 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)16 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)11 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)10 Test (org.junit.Test)10 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)8 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)7 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)5 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)5 Channel (org.apache.activemq.artemis.core.protocol.core.Channel)5 ArrayList (java.util.ArrayList)3 XAException (javax.transaction.xa.XAException)3 ActiveMQInternalErrorException (org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException)3 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)3 CreateSessionResponseMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage)3 SessionProducerCreditsMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionProducerCreditsMessage)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2