org.apache.activemq.artemis.api.core.Interceptor

Here are the examples of the java api class org.apache.activemq.artemis.api.core.Interceptor taken from open source projects.

1. ServerLocatorImpl#fromInterceptors()

View license
private String fromInterceptors(final List<Interceptor> interceptors) {
    StringBuffer buffer = new StringBuffer();
    boolean first = true;
    for (Interceptor value : interceptors) {
        if (!first) {
            buffer.append(",");
        }
        first = false;
        buffer.append(value.getClass().getName());
    }
    return buffer.toString();
}

2. FailoverOnFlowControlTest#testOverflowSend()

View license
@Test
public void testOverflowSend() throws Exception {
    ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(-1).setProducerWindowSize(1000).setRetryInterval(123);
    final ArrayList<ClientSession> sessionList = new ArrayList<>();
    Interceptor interceptorClient = new Interceptor() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            log.debug("Intercept..." + packet.getClass().getName());
            if (packet instanceof SessionProducerCreditsMessage) {
                SessionProducerCreditsMessage credit = (SessionProducerCreditsMessage) packet;
                log.debug("Credits: " + credit.getCredits());
                if (count.incrementAndGet() == 2) {
                    log.debug("### crashing server");
                    try {
                        InVMConnection.setFlushEnabled(false);
                        crash(false, sessionList.get(0));
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        InVMConnection.setFlushEnabled(true);
                    }
                    return false;
                }
            }
            return true;
        }
    };
    locator.addIncomingInterceptor(interceptorClient);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = sf.createSession(true, true);
    sessionList.add(session);
    session.createQueue(ADDRESS, ADDRESS, null, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    final int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[5000]);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.close();
}

3. ReattachTest#testOverflowCredits()

Project: activemq-artemis
Source File: ReattachTest.java
View license
/*
    * Test failure on connection, but server is still up so should immediately reconnect
    */
@Test
public void testOverflowCredits() throws Exception {
    final long retryInterval = 500;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 1;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024).setProducerWindowSize(1000);
    final AtomicInteger count = new AtomicInteger(0);
    Interceptor intercept = new Interceptor() {

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            System.out.println("Intercept..." + packet.getClass().getName());
            if (packet instanceof SessionProducerCreditsMessage) {
                SessionProducerCreditsMessage credit = (SessionProducerCreditsMessage) packet;
                System.out.println("Credits: " + credit.getCredits());
                if (count.incrementAndGet() == 2) {
                    System.out.println("Failing");
                    connection.fail(new ActiveMQException(ActiveMQExceptionType.UNSUPPORTED_PACKET, "bye"));
                    return false;
                }
            }
            return true;
        }
    };
    locator.addIncomingInterceptor(intercept);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
    ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
    final int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
        message.putIntProperty(new SimpleString("count"), i);
        message.getBodyBuffer().writeBytes(new byte[5000]);
        producer.send(message);
    }
    session.close();
    sf.close();
}

4. PingStressTest#internalTest()

View license
/*
    * 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();
}