Search in sources :

Example 6 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException 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 7 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class ClientSessionFactoryImpl method connectionDestroyed.

@Override
public void connectionDestroyed(final Object connectionID) {
    // The exception has to be created in the same thread where it's being called
    // as to avoid a different stack trace cause
    final ActiveMQException ex = ActiveMQClientMessageBundle.BUNDLE.channelDisconnected();
    // It has to use the same executor as the disconnect message is being sent through
    closeExecutor.execute(new Runnable() {

        @Override
        public void run() {
            handleConnectionFailure(connectionID, ex);
        }
    });
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 8 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class ClientSessionImpl method prepare.

@Override
public int prepare(final Xid xid) throws XAException {
    checkXA();
    if (logger.isTraceEnabled()) {
        logger.trace("Calling prepare:: " + convert(xid));
    }
    if (rollbackOnly) {
        throw new XAException(XAException.XAER_RMFAIL);
    }
    // Note - don't need to flush acks since the previous end would have
    // done this
    startCall();
    try {
        return sessionContext.xaPrepare(xid);
    } catch (XAException xae) {
        throw xae;
    } catch (ActiveMQException e) {
        if (e.getType() == ActiveMQExceptionType.UNBLOCKED || e.getType() == ActiveMQExceptionType.CONNECTION_TIMEDOUT) {
            // Unblocked on failover
            try {
                // will retry once after failover & unblock
                return sessionContext.xaPrepare(xid);
            } catch (Throwable t) {
            // ignore and rollback
            }
            ActiveMQClientLogger.LOGGER.failoverDuringPrepareRollingBack();
            try {
                rollback(false);
            } catch (Throwable t) {
                // This could occur if the TM interrupts the thread
                XAException xaException = new XAException(XAException.XAER_RMFAIL);
                xaException.initCause(t);
                throw xaException;
            }
            ActiveMQClientLogger.LOGGER.errorDuringPrepare(e);
            throw new XAException(XAException.XAER_RMFAIL);
        }
        ActiveMQClientLogger.LOGGER.errorDuringPrepare(e);
        // This should never occur
        XAException xaException = new XAException(XAException.XAER_RMFAIL);
        xaException.initCause(e);
        throw xaException;
    } catch (Throwable t) {
        ActiveMQClientLogger.LOGGER.errorDuringPrepare(t);
        // This could occur if the TM interrupts the thread
        XAException xaException = new XAException(XAException.XAER_RMFAIL);
        xaException.initCause(t);
        throw xaException;
    } finally {
        endCall();
    }
}
Also used : XAException(javax.transaction.xa.XAException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 9 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class LargeMessageControllerImpl method checkException.

/**
 * @throws ActiveMQException
 */
private void checkException() throws ActiveMQException {
    // once the exception is set, the controller is pretty much useless
    if (handledException != null) {
        if (handledException instanceof ActiveMQException) {
            ActiveMQException nestedException;
            // instead to just where it was canceled.
            if (handledException instanceof ActiveMQLargeMessageInterruptedException) {
                nestedException = new ActiveMQLargeMessageInterruptedException(handledException.getMessage());
            } else {
                nestedException = new ActiveMQException(((ActiveMQException) handledException).getType(), handledException.getMessage());
            }
            nestedException.initCause(handledException);
            throw nestedException;
        } else {
            throw new ActiveMQException(ActiveMQExceptionType.LARGE_MESSAGE_ERROR_BODY, "Error on saving LargeMessageBufferImpl", handledException);
        }
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQLargeMessageInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQLargeMessageInterruptedException)

Example 10 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.

the class ServerLocatorImpl method initialise.

private synchronized void initialise() throws ActiveMQException {
    if (state == STATE.INITIALIZED)
        return;
    synchronized (stateGuard) {
        if (state == STATE.CLOSING)
            throw new ActiveMQIllegalStateException();
        try {
            state = STATE.INITIALIZED;
            latch = new CountDownLatch(1);
            setThreadPools();
            instantiateLoadBalancingPolicy();
            if (discoveryGroupConfiguration != null) {
                discoveryGroup = createDiscoveryGroup(nodeID, discoveryGroupConfiguration);
                discoveryGroup.registerListener(this);
                discoveryGroup.start();
            }
        } catch (Exception e) {
            state = null;
            throw ActiveMQClientMessageBundle.BUNDLE.failedToInitialiseSessionFactory(e);
        }
    }
}
Also used : ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ObjectStreamException(java.io.ObjectStreamException)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)254 Test (org.junit.Test)139 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)121 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)84 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)79 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)78 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)59 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)54 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)45 CountDownLatch (java.util.concurrent.CountDownLatch)40 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)36 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)31 HashSet (java.util.HashSet)29 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)27 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)23 Role (org.apache.activemq.artemis.core.security.Role)22 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)20 Set (java.util.Set)16 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)16 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)15