Search in sources :

Example 11 with Interceptor

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

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

Example 13 with Interceptor

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

the class BridgeTest method internalTestMessageLoss.

/**
 * This test will ignore messages
 * What will cause the bridge to fail with a timeout
 * The bridge should still recover the failure and reconnect on that case
 */
public void internalTestMessageLoss(final boolean largeMessage) throws Exception {
    class MyInterceptor implements Interceptor {

        public boolean ignoreSends = true;

        public CountDownLatch latch;

        MyInterceptor(int numberOfIgnores) {
            latch = new CountDownLatch(numberOfIgnores);
        }

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            if (ignoreSends && packet instanceof SessionSendMessage || ignoreSends && packet instanceof SessionSendLargeMessage || ignoreSends && packet instanceof SessionSendContinuationMessage && !((SessionSendContinuationMessage) packet).isContinues()) {
                IntegrationTestLogger.LOGGER.info("IGNORED: " + packet);
                latch.countDown();
                return false;
            } else {
                IntegrationTestLogger.LOGGER.info(packet);
                return true;
            }
        }
    }
    MyInterceptor myInterceptor = new MyInterceptor(3);
    Map<String, Object> server0Params = new HashMap<>();
    server0 = createClusteredServerWithParams(isNetty(), 0, true, server0Params);
    Map<String, Object> server1Params = new HashMap<>();
    addTargetParameters(server1Params);
    server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
    final String testAddress = "testAddress";
    final String queueName0 = "queue0";
    final String forwardAddress = "forwardAddress";
    final String queueName1 = "queue1";
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
    TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
    HashMap<String, TransportConfiguration> connectors = new HashMap<>();
    connectors.put(server1tc.getName(), server1tc);
    server0.getConfiguration().setConnectorConfigurations(connectors);
    final int messageSize = 1024;
    final int numMessages = 1;
    ArrayList<String> connectorConfig = new ArrayList<>();
    connectorConfig.add(server1tc.getName());
    BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName("bridge1").setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(100).setReconnectAttempts(-1).setReconnectAttemptsOnSameNode(-1).setUseDuplicateDetection(false).setConfirmationWindowSize(numMessages * messageSize / 2).setStaticConnectors(connectorConfig).setCallTimeout(500);
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server1.getConfiguration().setQueueConfigurations(queueConfigs1);
    server1.start();
    server1.getRemotingService().addIncomingInterceptor(myInterceptor);
    server0.start();
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc, server1tc));
    ClientSessionFactory sf0 = addSessionFactory(locator.createSessionFactory(server0tc));
    ClientSessionFactory sf1 = addSessionFactory(locator.createSessionFactory(server1tc));
    ClientSession session0 = sf0.createSession(false, true, true);
    ClientSession session1 = sf1.createSession(false, true, true);
    ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));
    ClientConsumer consumer1 = session1.createConsumer(queueName1);
    session1.start();
    final byte[] bytes = new byte[messageSize];
    final SimpleString propKey = new SimpleString("testkey");
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(true);
        if (largeMessage) {
            message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(10 * 1024));
        }
        message.putIntProperty(propKey, i);
        message.getBodyBuffer().writeBytes(bytes);
        producer0.send(message);
    }
    assertTrue("where is the countDown?", myInterceptor.latch.await(30, TimeUnit.SECONDS));
    myInterceptor.ignoreSends = false;
    server1.getRemotingService().removeIncomingInterceptor(myInterceptor);
    IntegrationTestLogger.LOGGER.info("No longer ignoring packets.");
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer1.receive(30000);
        Assert.assertNotNull(message);
        Assert.assertEquals(i, message.getObjectProperty(propKey));
        if (largeMessage) {
            readLargeMessages(message, 10);
        }
        message.acknowledge();
    }
    Assert.assertNull(consumer1.receiveImmediate());
    session0.close();
    session1.close();
    sf0.close();
    sf1.close();
    closeFields();
    assertEquals("there should be no queues", 0, loadQueues(server0).size());
}
Also used : SessionSendContinuationMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionSendContinuationMessage) HashMap(java.util.HashMap) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ArrayList(java.util.ArrayList) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) 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) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) SessionSendLargeMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionSendLargeMessage) Packet(org.apache.activemq.artemis.core.protocol.core.Packet) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) SessionSendMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionSendMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer)

Example 14 with Interceptor

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

the class TemporaryQueueTest method testDeleteTemporaryQueueWhenClientCrash.

@Test
public void testDeleteTemporaryQueueWhenClientCrash() throws Exception {
    session.close();
    sf.close();
    final SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString address = RandomUtil.randomSimpleString();
    // 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(1);
    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;
        }
    });
    ServerLocator locator = createInVMNonHALocator();
    locator.setConnectionTTL(TemporaryQueueTest.CONNECTION_TTL);
    sf = addSessionFactory(createSessionFactory(locator));
    session = sf.createSession(false, true, true);
    session.createTemporaryQueue(address, queue);
    assertTrue("server has not received any ping from the client", pingOnServerLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval(), TimeUnit.MILLISECONDS));
    assertEquals(1, server.getConnectionCount());
    RemotingConnection remotingConnection = server.getRemotingService().getConnections().iterator().next();
    final CountDownLatch serverCloseLatch = new CountDownLatch(1);
    remotingConnection.addCloseListener(new CloseListener() {

        @Override
        public void connectionClosed() {
            serverCloseLatch.countDown();
        }
    });
    ((ClientSessionInternal) session).getConnection().fail(new ActiveMQInternalErrorException("simulate a client failure"));
    // let some time for the server to clean the connections
    assertTrue("server has not closed the connection", serverCloseLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval() + 2 * TemporaryQueueTest.CONNECTION_TTL, TimeUnit.MILLISECONDS));
    // The next getCount will be asynchronously done at the end of failure. We will wait some time until it has reached there.
    for (long timeout = System.currentTimeMillis() + 5000; timeout > System.currentTimeMillis() && server.getConnectionCount() > 0; ) {
        Thread.sleep(1);
    }
    assertEquals(0, server.getConnectionCount());
    session.close();
    sf.close();
    ServerLocator locator2 = createInVMNonHALocator();
    sf = addSessionFactory(createSessionFactory(locator2));
    session = sf.createSession(false, true, true);
    session.start();
    ActiveMQAction activeMQAction = new ActiveMQAction() {

        @Override
        public void run() throws ActiveMQException {
            session.createConsumer(queue);
        }
    };
    ActiveMQTestBase.expectActiveMQException("temp queue must not exist after the server detected the client crash", ActiveMQExceptionType.QUEUE_DOES_NOT_EXIST, activeMQAction);
    session.close();
    locator2.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) CountDownLatch(java.util.concurrent.CountDownLatch) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 15 with Interceptor

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

the class PingStressTest method internalTest.

/*
    * Test the client triggering failure due to no pong received in time
    */
private void internalTest() throws Exception {
    Interceptor noPongInterceptor = new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection conn) throws ActiveMQException {
            PingStressTest.log.info("In interceptor, packet is " + packet.getType());
            if (packet.getType() == PacketImpl.PING) {
                PingStressTest.log.info("Ignoring Ping packet.. it will be dropped");
                return false;
            } else {
                return true;
            }
        }
    };
    server.getRemotingService().addIncomingInterceptor(noPongInterceptor);
    ServerLocator locator = createNettyNonHALocator().setClientFailureCheckPeriod(PingStressTest.PING_INTERVAL).setConnectionTTL((long) (PingStressTest.PING_INTERVAL * 1.5)).setCallTimeout(PingStressTest.PING_INTERVAL * 10);
    final ClientSessionFactory csf1 = createSessionFactory(locator);
    final int numberOfSessions = 1;
    final int numberOfThreads = 30;
    final CountDownLatch flagStart = new CountDownLatch(1);
    final CountDownLatch flagAligned = new CountDownLatch(numberOfThreads);
    class LocalThread extends Thread {

        Throwable failure;

        int threadNumber;

        LocalThread(final int i) {
            super("LocalThread i = " + i);
            threadNumber = i;
        }

        @Override
        public void run() {
            try {
                ServerLocator locator = createNettyNonHALocator().setClientFailureCheckPeriod(PingStressTest.PING_INTERVAL).setConnectionTTL((long) (PingStressTest.PING_INTERVAL * 1.5)).setCallTimeout(PingStressTest.PING_INTERVAL * 10);
                final ClientSessionFactory csf2 = createSessionFactory(locator);
                // Start all at once to make concurrency worst
                flagAligned.countDown();
                flagStart.await();
                for (int i = 0; i < numberOfSessions; i++) {
                    System.out.println(getName() + " Session = " + i);
                    ClientSession session;
                    // them)
                    if (RandomUtil.randomBoolean()) {
                        session = csf1.createSession(false, false, false);
                    } else {
                        session = csf2.createSession(false, false, false);
                    }
                    // We will wait to anything between 0 to PING_INTERVAL * 2
                    Thread.sleep(PingStressTest.PING_INTERVAL * (threadNumber % 3));
                    session.close();
                    csf2.close();
                    locator.close();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                failure = e;
            }
        }
    }
    LocalThread[] threads = new LocalThread[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        threads[i] = new LocalThread(i);
        threads[i].start();
    }
    assertTrue(flagAligned.await(10, TimeUnit.SECONDS));
    flagStart.countDown();
    Throwable e = null;
    for (LocalThread t : threads) {
        t.join();
        if (t.failure != null) {
            e = t.failure;
        }
    }
    if (e != null) {
        throw new Exception("Test Failed", e);
    }
    csf1.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) 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) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Aggregations

Interceptor (org.apache.activemq.artemis.api.core.Interceptor)17 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)11 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)11 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)11 Test (org.junit.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)8 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)7 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)6 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)6 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)4 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)4 ArrayList (java.util.ArrayList)3 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)2 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)2