Search in sources :

Example 1 with CoreRemotingConnection

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

the class ClientSessionFactoryImpl method reconnectSessions.

/*
    * Re-attach sessions all pre-existing sessions to the new remoting connection
    */
private void reconnectSessions(final RemotingConnection oldConnection, final int reconnectAttempts, final ActiveMQException cause) {
    HashSet<ClientSessionInternal> sessionsToFailover;
    synchronized (sessions) {
        sessionsToFailover = new HashSet<>(sessions);
    }
    for (ClientSessionInternal session : sessionsToFailover) {
        session.preHandleFailover(connection);
    }
    getConnectionWithRetry(reconnectAttempts);
    if (connection == null) {
        if (!clientProtocolManager.isAlive())
            ActiveMQClientLogger.LOGGER.failedToConnectToServer();
        return;
    }
    List<FailureListener> oldListeners = oldConnection.getFailureListeners();
    List<FailureListener> newListeners = new ArrayList<>(connection.getFailureListeners());
    for (FailureListener listener : oldListeners) {
        // Add all apart from the old DelegatingFailureListener
        if (listener instanceof DelegatingFailureListener == false) {
            newListeners.add(listener);
        }
    }
    connection.setFailureListeners(newListeners);
    // This used to be done inside failover
    // it needs to be done on the protocol
    ((CoreRemotingConnection) connection).syncIDGeneratorSequence(((CoreRemotingConnection) oldConnection).getIDGeneratorSequence());
    for (ClientSessionInternal session : sessionsToFailover) {
        session.handleFailover(connection, cause);
    }
}
Also used : SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) FailureListener(org.apache.activemq.artemis.core.remoting.FailureListener) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) ArrayList(java.util.ArrayList)

Example 2 with CoreRemotingConnection

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

the class CoreProtocolManager method createConnectionEntry.

@Override
public ConnectionEntry createConnectionEntry(final Acceptor acceptorUsed, final Connection connection) {
    final Configuration config = server.getConfiguration();
    Executor connectionExecutor = server.getExecutorFactory().getExecutor();
    final CoreRemotingConnection rc = new RemotingConnectionImpl(new ServerPacketDecoder(), connection, incomingInterceptors, outgoingInterceptors, config.isAsyncConnectionExecutionEnabled() ? connectionExecutor : null, server.getNodeID());
    Channel channel1 = rc.getChannel(CHANNEL_ID.SESSION.id, -1);
    ChannelHandler handler = new ActiveMQPacketHandler(this, server, channel1, rc);
    channel1.setHandler(handler);
    long ttl = ActiveMQClient.DEFAULT_CONNECTION_TTL;
    if (config.getConnectionTTLOverride() != -1) {
        ttl = config.getConnectionTTLOverride();
    }
    final ConnectionEntry entry = new ConnectionEntry(rc, connectionExecutor, System.currentTimeMillis(), ttl);
    final Channel channel0 = rc.getChannel(ChannelImpl.CHANNEL_ID.PING.id, -1);
    channel0.setHandler(new LocalChannelHandler(config, entry, channel0, acceptorUsed, rc));
    server.getClusterManager().addClusterChannelHandler(rc.getChannel(CHANNEL_ID.CLUSTER.id, -1), acceptorUsed, rc, server.getActivation());
    return entry;
}
Also used : Executor(java.util.concurrent.Executor) Configuration(org.apache.activemq.artemis.core.config.Configuration) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ConnectionEntry(org.apache.activemq.artemis.spi.core.protocol.ConnectionEntry) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) Channel(org.apache.activemq.artemis.core.protocol.core.Channel) ChannelHandler(org.apache.activemq.artemis.core.protocol.core.ChannelHandler) ServerPacketDecoder(org.apache.activemq.artemis.core.protocol.ServerPacketDecoder)

Example 3 with CoreRemotingConnection

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

the class ChannelImpl method transferConnection.

@Override
public void transferConnection(final CoreRemotingConnection newConnection) {
    // the old connection get processed after transfer has occurred
    synchronized (connection.getTransferLock()) {
        connection.removeChannel(id);
        if (logger.isTraceEnabled()) {
            logger.trace("RemotingConnectionID=" + (connection == null ? "NULL" : connection.getID()) + " transferConnection to new RemotingConnectionID=" + (newConnection == null ? "NULL" : newConnection.getID()));
        }
        // And switch it
        final CoreRemotingConnection rnewConnection = newConnection;
        rnewConnection.putChannel(id, this);
        connection = rnewConnection;
        transferring = true;
    }
}
Also used : CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)

Example 4 with CoreRemotingConnection

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

the class ClusterControl method authorize.

/**
 * authorise this cluster control so it can communicate with the cluster, it will set the cluster channel on a successful
 * authentication.
 *
 * @throws ActiveMQException if authorisation wasn't successful.
 */
public void authorize() throws ActiveMQException {
    CoreRemotingConnection connection = (CoreRemotingConnection) sessionFactory.getConnection();
    clusterChannel = connection.getChannel(ChannelImpl.CHANNEL_ID.CLUSTER.id, -1);
    ClusterConnectReplyMessage packet = (ClusterConnectReplyMessage) clusterChannel.sendBlocking(new ClusterConnectMessage(clusterUser, clusterPassword), PacketImpl.CLUSTER_CONNECT_REPLY);
    if (!packet.isAuthorized()) {
        throw ActiveMQMessageBundle.BUNDLE.unableToValidateClusterUser(clusterUser);
    }
}
Also used : ClusterConnectReplyMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ClusterConnectReplyMessage) ClusterConnectMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ClusterConnectMessage) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)

Example 5 with CoreRemotingConnection

use of org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection 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)

Aggregations

CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)8 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 SessionFailureListener (org.apache.activemq.artemis.api.core.client.SessionFailureListener)2 ConnectionEntry (org.apache.activemq.artemis.spi.core.protocol.ConnectionEntry)2 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)1 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)1 Configuration (org.apache.activemq.artemis.core.config.Configuration)1 ServerPacketDecoder (org.apache.activemq.artemis.core.protocol.ServerPacketDecoder)1 Channel (org.apache.activemq.artemis.core.protocol.core.Channel)1