Search in sources :

Example 26 with Packet

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

the class CoreSessionCallback method sendMessage.

@Override
public int sendMessage(MessageReference ref, Message message, ServerConsumer consumer, int deliveryCount) {
    Packet packet;
    if (channel.getConnection().isVersionBeforeAddressChange()) {
        packet = new SessionReceiveMessage_1X(consumer.getID(), message.toCore(coreMessageObjectPools), deliveryCount);
    } else {
        packet = new SessionReceiveMessage(consumer.getID(), message.toCore(coreMessageObjectPools), deliveryCount);
    }
    int size = 0;
    if (channel.sendBatched(packet)) {
        size = packet.getPacketSize();
    }
    return size;
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionReceiveMessage_1X(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionReceiveMessage_1X) SessionReceiveMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionReceiveMessage)

Example 27 with Packet

use of org.apache.activemq.artemis.core.protocol.core.Packet 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 28 with Packet

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

the class ConsumerTest method testAcksWithSmallSendWindow.

@Test
public void testAcksWithSmallSendWindow() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        producer.send(message);
    }
    session.close();
    sf.close();
    final CountDownLatch latch = new CountDownLatch(numMessages);
    server.getRemotingService().addIncomingInterceptor(new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
            if (packet.getType() == PacketImpl.SESS_ACKNOWLEDGE) {
                latch.countDown();
            }
            return true;
        }
    });
    ServerLocator locator = createInVMNonHALocator().setConfirmationWindowSize(100).setAckBatchSize(-1);
    ClientSessionFactory sfReceive = createSessionFactory(locator);
    ClientSession sessionRec = sfReceive.createSession(false, true, true);
    ClientConsumer consumer = sessionRec.createConsumer(QUEUE);
    consumer.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                message.acknowledge();
            } catch (ActiveMQException e) {
                e.printStackTrace();
            }
        }
    });
    sessionRec.start();
    Assert.assertTrue(latch.await(60, TimeUnit.SECONDS));
    sessionRec.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 29 with Packet

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

Example 30 with Packet

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

the class ProducerTest method testProducerWithSmallWindowSizeAndLargeMessage.

@Test
public void testProducerWithSmallWindowSizeAndLargeMessage() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    server.getRemotingService().addIncomingInterceptor(new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
            if (packet.getType() == PacketImpl.SESS_SEND) {
                latch.countDown();
            }
            return true;
        }
    });
    ServerLocator locator = createInVMNonHALocator().setConfirmationWindowSize(100);
    ClientSessionFactory cf = locator.createSessionFactory();
    ClientSession session = cf.createSession(false, true, true);
    ClientProducer producer = session.createProducer(QUEUE);
    ClientMessage message = session.createMessage(true);
    byte[] body = new byte[1000];
    message.getBodyBuffer().writeBytes(body);
    producer.send(message);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    session.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

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