Search in sources :

Example 1 with RemotingConnection

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

the class ClientSessionFactoryImpl method getConnection.

@Override
public RemotingConnection getConnection() {
    if (closed)
        throw new IllegalStateException("ClientSessionFactory is closed!");
    if (!clientProtocolManager.isAlive())
        return null;
    synchronized (connectionLock) {
        if (connection != null) {
            // a connection already exists, so returning the same one
            return connection;
        } else {
            RemotingConnection connection = establishNewConnection();
            this.connection = connection;
            // make sure to reset this.connection == null
            if (connection != null && liveNodeID != null) {
                try {
                    if (!clientProtocolManager.checkForFailover(liveNodeID)) {
                        connection.destroy();
                        this.connection = null;
                        return null;
                    }
                } catch (ActiveMQException e) {
                    connection.destroy();
                    this.connection = null;
                    return null;
                }
            }
            if (connection != null && serverLocator.getAfterConnectInternalListener() != null) {
                serverLocator.getAfterConnectInternalListener().onConnection(this);
            }
            if (serverLocator.getTopology() != null) {
                if (connection != null) {
                    if (ClientSessionFactoryImpl.logger.isTraceEnabled()) {
                        logger.trace(this + "::Subscribing Topology");
                    }
                    clientProtocolManager.sendSubscribeTopology(serverLocator.isClusterConnection());
                }
            } else {
                logger.debug("serverLocator@" + System.identityHashCode(serverLocator + " had no topology"));
            }
            return connection;
        }
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)

Example 2 with RemotingConnection

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

the class ClientSessionFactoryImpl method failoverOrReconnect.

/**
 * TODO: Maybe this belongs to ActiveMQClientProtocolManager
 *
 * @param connectionID
 * @param me
 */
private void failoverOrReconnect(final Object connectionID, final ActiveMQException me, String scaleDownTargetNodeID) {
    ActiveMQClientLogger.LOGGER.failoverOrReconnect(connectionID, me);
    for (ClientSessionInternal session : sessions) {
        SessionContext context = session.getSessionContext();
        if (context instanceof ActiveMQSessionContext) {
            ActiveMQSessionContext sessionContext = (ActiveMQSessionContext) context;
            if (sessionContext.isKilled()) {
                setReconnectAttempts(0);
            }
        }
    }
    Set<ClientSessionInternal> sessionsToClose = null;
    if (!clientProtocolManager.isAlive())
        return;
    Lock localFailoverLock = lockFailover();
    try {
        if (connection == null || !connection.getID().equals(connectionID) || !clientProtocolManager.isAlive()) {
            return;
        }
        if (ClientSessionFactoryImpl.logger.isTraceEnabled()) {
            logger.trace("Client Connection failed, calling failure listeners and trying to reconnect, reconnectAttempts=" + reconnectAttempts);
        }
        callFailoverListeners(FailoverEventType.FAILURE_DETECTED);
        // We call before reconnection occurs to give the user a chance to do cleanup, like cancel messages
        callSessionFailureListeners(me, false, false, scaleDownTargetNodeID);
        if (reconnectAttempts != 0) {
            if (clientProtocolManager.cleanupBeforeFailover(me)) {
                // Now we absolutely know that no threads are executing in or blocked in
                // createSession,
                // and no
                // more will execute it until failover is complete
                // So.. do failover / reconnection
                RemotingConnection oldConnection = connection;
                connection = null;
                Connector localConnector = connector;
                if (localConnector != null) {
                    try {
                        localConnector.close();
                    } catch (Exception ignore) {
                    // no-op
                    }
                }
                cancelScheduledTasks();
                connector = null;
                reconnectSessions(oldConnection, reconnectAttempts, me);
                if (oldConnection != null) {
                    oldConnection.destroy();
                }
                if (connection != null) {
                    callFailoverListeners(FailoverEventType.FAILOVER_COMPLETED);
                }
            }
        } else {
            RemotingConnection connectionToDestory = connection;
            if (connectionToDestory != null) {
                connectionToDestory.destroy();
            }
            connection = null;
        }
        if (connection == null) {
            synchronized (sessions) {
                sessionsToClose = new HashSet<>(sessions);
            }
            callFailoverListeners(FailoverEventType.FAILOVER_FAILED);
            callSessionFailureListeners(me, true, false, scaleDownTargetNodeID);
        }
    } finally {
        localFailoverLock.unlock();
    }
    // This needs to be outside the failover lock to prevent deadlock
    if (connection != null) {
        callSessionFailureListeners(me, true, true);
    }
    if (sessionsToClose != null) {
        for (ClientSessionInternal session : sessionsToClose) {
            try {
                session.cleanUp(true);
            } catch (Exception cause) {
                ActiveMQClientLogger.LOGGER.failedToCleanupSession(cause);
            }
        }
    }
}
Also used : Connector(org.apache.activemq.artemis.spi.core.remoting.Connector) ActiveMQSessionContext(org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQSessionContext) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQSessionContext(org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQSessionContext) SessionContext(org.apache.activemq.artemis.spi.core.remoting.SessionContext) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 3 with RemotingConnection

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

the class ClientSessionFactoryImpl method establishNewConnection.

protected RemotingConnection establishNewConnection() {
    Connection transportConnection = createTransportConnection();
    if (transportConnection == null) {
        if (ClientSessionFactoryImpl.logger.isTraceEnabled()) {
            logger.trace("Neither backup or live were active, will just give up now");
        }
        return null;
    }
    RemotingConnection newConnection = clientProtocolManager.connect(transportConnection, callTimeout, callFailoverTimeout, incomingInterceptors, outgoingInterceptors, new SessionFactoryTopologyHandler());
    newConnection.addFailureListener(new DelegatingFailureListener(newConnection.getID()));
    schedulePing();
    if (logger.isTraceEnabled()) {
        logger.trace("returning " + newConnection);
    }
    return newConnection;
}
Also used : CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(org.apache.activemq.artemis.spi.core.remoting.Connection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)

Example 4 with RemotingConnection

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

the class ActiveMQServerControlImpl method listConnectionsAsJSON.

@Override
public String listConnectionsAsJSON() throws Exception {
    checkStarted();
    clearIO();
    try {
        JsonArrayBuilder array = JsonLoader.createArrayBuilder();
        Set<RemotingConnection> connections = server.getRemotingService().getConnections();
        for (RemotingConnection connection : connections) {
            JsonObjectBuilder obj = JsonLoader.createObjectBuilder().add("connectionID", connection.getID().toString()).add("clientAddress", connection.getRemoteAddress()).add("creationTime", connection.getCreationTime()).add("implementation", connection.getClass().getSimpleName()).add("sessionCount", server.getSessions(connection.getID().toString()).size());
            array.add(obj);
        }
        return array.build().toString();
    } finally {
        blockOnIO();
    }
}
Also used : RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 5 with RemotingConnection

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

the class ActiveMQServerControlImpl method listConnectionIDs.

@Override
public String[] listConnectionIDs() {
    checkStarted();
    clearIO();
    try {
        Set<RemotingConnection> connections = remotingService.getConnections();
        String[] connectionIDs = new String[connections.size()];
        int i = 0;
        for (RemotingConnection connection : connections) {
            connectionIDs[i++] = connection.getID().toString();
        }
        return connectionIDs;
    } finally {
        blockOnIO();
    }
}
Also used : RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Aggregations

RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)84 Test (org.junit.Test)44 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)36 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)31 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)30 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)22 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)21 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)17 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)16 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)15 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)13 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)12 Connection (javax.jms.Connection)11 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)11 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)11 Session (javax.jms.Session)10