Search in sources :

Example 1 with CreateSessionResponseMessage

use of org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage in project activemq-artemis by apache.

the class ActiveMQClientProtocolManager method createSessionContext.

public SessionContext createSessionContext(Version clientVersion, String name, String username, String password, boolean xa, boolean autoCommitSends, boolean autoCommitAcks, boolean preAcknowledge, int minLargeMessageSize, int confirmationWindowSize) throws ActiveMQException {
    if (!isAlive())
        throw ActiveMQClientMessageBundle.BUNDLE.clientSessionClosed();
    Channel sessionChannel = null;
    CreateSessionResponseMessage response = null;
    boolean retry;
    do {
        retry = false;
        Lock lock = null;
        try {
            lock = lockSessionCreation();
            // We now set a flag saying createSession is executing
            synchronized (inCreateSessionGuard) {
                if (!isAlive())
                    throw ActiveMQClientMessageBundle.BUNDLE.clientSessionClosed();
                inCreateSession = true;
                inCreateSessionLatch = new CountDownLatch(1);
            }
            long sessionChannelID = connection.generateChannelID();
            Packet request = newCreateSessionPacket(clientVersion, name, username, password, xa, autoCommitSends, autoCommitAcks, preAcknowledge, minLargeMessageSize, confirmationWindowSize, sessionChannelID);
            try {
                // channel1 reference here has to go away
                response = (CreateSessionResponseMessage) getChannel1().sendBlocking(request, PacketImpl.CREATESESSION_RESP);
            } catch (ActiveMQException cause) {
                if (!isAlive())
                    throw cause;
                if (cause.getType() == ActiveMQExceptionType.UNBLOCKED) {
                    // This means the thread was blocked on create session and failover unblocked it
                    // so failover could occur
                    retry = true;
                    continue;
                } else {
                    throw cause;
                }
            }
            sessionChannel = connection.getChannel(sessionChannelID, confirmationWindowSize);
        } catch (Throwable t) {
            if (lock != null) {
                lock.unlock();
                lock = null;
            }
            if (t instanceof ActiveMQException) {
                throw (ActiveMQException) t;
            } else {
                throw ActiveMQClientMessageBundle.BUNDLE.failedToCreateSession(t);
            }
        } finally {
            if (lock != null) {
                lock.unlock();
            }
            // Execution has finished so notify any failover thread that may be waiting for us to be done
            inCreateSession = false;
            inCreateSessionLatch.countDown();
        }
    } while (retry);
    sessionChannel.getConnection().setChannelVersion(response.getServerVersion());
    return newSessionContext(name, confirmationWindowSize, sessionChannel, response);
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) CreateSessionResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Channel(org.apache.activemq.artemis.core.protocol.core.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) Lock(java.util.concurrent.locks.Lock)

Example 2 with CreateSessionResponseMessage

use of org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage in project activemq-artemis by apache.

the class ActiveMQPacketHandler method handleCreateSession.

private void handleCreateSession(final CreateSessionMessage request) {
    boolean incompatibleVersion = false;
    Packet response;
    try {
        Version version = server.getVersion();
        if (!version.isCompatible(request.getVersion())) {
            throw ActiveMQMessageBundle.BUNDLE.incompatibleClientServer();
        }
        if (!server.isStarted()) {
            throw ActiveMQMessageBundle.BUNDLE.serverNotStarted();
        }
        if (connection.getChannelVersion() == 0) {
            connection.setChannelVersion(request.getVersion());
        } else if (connection.getChannelVersion() != request.getVersion()) {
            ActiveMQServerLogger.LOGGER.incompatibleVersionAfterConnect(request.getVersion(), connection.getChannelVersion());
        }
        Channel channel = connection.getChannel(request.getSessionChannelID(), request.getWindowSize());
        ActiveMQPrincipal activeMQPrincipal = null;
        if (request.getUsername() == null) {
            activeMQPrincipal = connection.getDefaultActiveMQPrincipal();
        }
        OperationContext sessionOperationContext = server.newOperationContext();
        Map<SimpleString, RoutingType> routingTypeMap = protocolManager.getPrefixes();
        CoreSessionCallback sessionCallback = new CoreSessionCallback(request.getName(), protocolManager, channel, connection);
        ServerSession session = server.createSession(request.getName(), activeMQPrincipal == null ? request.getUsername() : activeMQPrincipal.getUserName(), activeMQPrincipal == null ? request.getPassword() : activeMQPrincipal.getPassword(), request.getMinLargeMessageSize(), connection, request.isAutoCommitSends(), request.isAutoCommitAcks(), request.isPreAcknowledge(), request.isXA(), request.getDefaultAddress(), sessionCallback, true, sessionOperationContext, routingTypeMap);
        ServerProducer serverProducer = new ServerProducerImpl(session.getName(), "CORE", request.getDefaultAddress());
        session.addProducer(serverProducer);
        ServerSessionPacketHandler handler = new ServerSessionPacketHandler(server, protocolManager, session, server.getStorageManager(), channel);
        channel.setHandler(handler);
        sessionCallback.setSessionHandler(handler);
        // TODO - where is this removed?
        protocolManager.addSessionHandler(request.getName(), handler);
        response = new CreateSessionResponseMessage(server.getVersion().getIncrementingVersion());
    } catch (ActiveMQClusterSecurityException | ActiveMQSecurityException e) {
        ActiveMQServerLogger.LOGGER.securityProblemWhileCreatingSession(e.getMessage());
        response = new ActiveMQExceptionMessage(e);
    } catch (ActiveMQException e) {
        if (e.getType() == ActiveMQExceptionType.INCOMPATIBLE_CLIENT_SERVER_VERSIONS) {
            incompatibleVersion = true;
            logger.debug("Sending ActiveMQException after Incompatible client", e);
        } else {
            ActiveMQServerLogger.LOGGER.failedToCreateSession(e);
        }
        response = new ActiveMQExceptionMessage(e);
    } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.failedToCreateSession(e);
        response = new ActiveMQExceptionMessage(new ActiveMQInternalErrorException());
    }
    // are not compatible
    if (incompatibleVersion) {
        channel1.sendAndFlush(response);
    } else {
        channel1.send(response);
    }
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) ServerSessionPacketHandler(org.apache.activemq.artemis.core.protocol.core.ServerSessionPacketHandler) Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQPrincipal(org.apache.activemq.artemis.core.security.ActiveMQPrincipal) CreateSessionResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage) Channel(org.apache.activemq.artemis.core.protocol.core.Channel) ActiveMQExceptionMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ActiveMQExceptionMessage) ActiveMQClusterSecurityException(org.apache.activemq.artemis.api.core.ActiveMQClusterSecurityException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) ServerProducer(org.apache.activemq.artemis.core.server.ServerProducer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQClusterSecurityException(org.apache.activemq.artemis.api.core.ActiveMQClusterSecurityException) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) ServerProducerImpl(org.apache.activemq.artemis.core.server.impl.ServerProducerImpl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Version(org.apache.activemq.artemis.core.version.Version) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Example 3 with CreateSessionResponseMessage

use of org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage in project activemq-artemis by apache.

the class IncompatibleVersionTest method doTestClientVersionCompatibility.

private void doTestClientVersionCompatibility(boolean compatible) throws Exception {
    Channel channel1 = connection.getChannel(1, -1);
    long sessionChannelID = connection.generateChannelID();
    int version = VersionLoader.getVersion().getIncrementingVersion();
    if (!compatible) {
        version = -1;
    }
    Packet request = new CreateSessionMessage(randomString(), sessionChannelID, version, null, null, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, false, true, true, false, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, null);
    if (compatible) {
        CreateSessionResponseMessage packet = (CreateSessionResponseMessage) channel1.sendBlocking(request, PacketImpl.CREATESESSION_RESP);
        assertNotNull(packet);
        // 1 connection on the server
        assertEquals(1, server.getConnectionCount());
    } else {
        try {
            channel1.sendBlocking(request, PacketImpl.CREATESESSION_RESP);
            fail();
        } catch (ActiveMQIncompatibleClientServerException icsv) {
        // ok
        } catch (ActiveMQException e) {
            fail("Invalid Exception type:" + e.getType());
        }
        long start = System.currentTimeMillis();
        while (System.currentTimeMillis() < start + 3 * server.getConfiguration().getConnectionTtlCheckInterval()) {
            if (server.getConnectionCount() == 0) {
                break;
            }
        }
        // no connection on the server
        assertEquals(0, server.getConnectionCount());
    }
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) CreateSessionResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Channel(org.apache.activemq.artemis.core.protocol.core.Channel) ActiveMQIncompatibleClientServerException(org.apache.activemq.artemis.api.core.ActiveMQIncompatibleClientServerException) CreateSessionMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionMessage)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 Channel (org.apache.activemq.artemis.core.protocol.core.Channel)3 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)3 CreateSessionResponseMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 Lock (java.util.concurrent.locks.Lock)1 ActiveMQClusterSecurityException (org.apache.activemq.artemis.api.core.ActiveMQClusterSecurityException)1 ActiveMQIncompatibleClientServerException (org.apache.activemq.artemis.api.core.ActiveMQIncompatibleClientServerException)1 ActiveMQInternalErrorException (org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException)1 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)1 RoutingType (org.apache.activemq.artemis.api.core.RoutingType)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 OperationContext (org.apache.activemq.artemis.core.persistence.OperationContext)1 ServerSessionPacketHandler (org.apache.activemq.artemis.core.protocol.core.ServerSessionPacketHandler)1 ActiveMQExceptionMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ActiveMQExceptionMessage)1 CreateSessionMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionMessage)1 ActiveMQPrincipal (org.apache.activemq.artemis.core.security.ActiveMQPrincipal)1 ServerProducer (org.apache.activemq.artemis.core.server.ServerProducer)1 ServerSession (org.apache.activemq.artemis.core.server.ServerSession)1 ServerProducerImpl (org.apache.activemq.artemis.core.server.impl.ServerProducerImpl)1