Search in sources :

Example 1 with Interceptor

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

the class ServerLocatorImpl method fromInterceptors.

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();
}
Also used : Interceptor(org.apache.activemq.artemis.api.core.Interceptor)

Example 2 with Interceptor

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

the class ReattachTest method testOverflowCredits.

/*
    * 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();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionProducerCreditsMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionProducerCreditsMessage) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 3 with Interceptor

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

the class FailoverTest method testCommitDidNotOccurUnblockedAndResend.

@Test(timeout = 120000)
public void testCommitDidNotOccurUnblockedAndResend() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setReconnectAttempts(300).setRetryInterval(100);
    sf = createSessionFactoryAndWaitForTopology(locator, 2);
    final ClientSession session = createSession(sf, false, false);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    sendMessages(session, producer, NUM_MESSAGES);
    class Committer extends Thread {

        @Override
        public void run() {
            Interceptor interceptor = new DelayInterceptor3();
            try {
                liveServer.addInterceptor(interceptor);
                session.commit();
            } catch (ActiveMQTransactionRolledBackException trbe) {
                // Ok - now we retry the commit after removing the interceptor
                liveServer.removeInterceptor(interceptor);
                try {
                    session.commit();
                    failed = false;
                } catch (ActiveMQException e2) {
                }
            } catch (ActiveMQTransactionOutcomeUnknownException toue) {
                // Ok - now we retry the commit after removing the interceptor
                liveServer.removeInterceptor(interceptor);
                try {
                    session.commit();
                    failed = false;
                } catch (ActiveMQException e2) {
                }
            } catch (ActiveMQException e) {
            // ignore
            }
        }

        volatile boolean failed = true;
    }
    Committer committer = new Committer();
    committer.start();
    crash(session);
    committer.join();
    Assert.assertFalse("commiter failed should be false", committer.failed);
    session.close();
    ClientSession session2 = createSession(sf, false, false);
    producer = session2.createProducer(FailoverTestBase.ADDRESS);
    // We now try and resend the messages since we get a transaction rolled back exception
    sendMessages(session2, producer, NUM_MESSAGES);
    session2.commit();
    ClientConsumer consumer = session2.createConsumer(FailoverTestBase.ADDRESS);
    session2.start();
    receiveMessages(consumer);
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull("expecting null message", message);
}
Also used : ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) 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) Test(org.junit.Test)

Example 4 with Interceptor

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

the class FailoverOnFlowControlTest method testOverflowSend.

@Test
public void testOverflowSend() throws Exception {
    ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setProducerWindowSize(1000).setRetryInterval(100);
    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();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionProducerCreditsMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionProducerCreditsMessage) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ArrayList(java.util.ArrayList) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) 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 5 with Interceptor

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

the class XaTimeoutTest method testTimeoutOnXACall.

// HORNETQ-1117 - Test that will timeout on a XA transaction and then will perform another XA operation
@Test
public void testTimeoutOnXACall() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    class SomeInterceptor implements Interceptor {

        /* (non-Javadoc)
          * @see Interceptor#intercept(org.apache.activemq.artemis.core.protocol.core.Packet, RemotingConnection)
          */
        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            if (packet instanceof SessionXAStartMessage) {
                try {
                    latch.await(1, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
    }
    server.getRemotingService().addIncomingInterceptor(new SomeInterceptor());
    ServerLocator locatorTimeout = createInVMNonHALocator().setCallTimeout(300);
    ClientSessionFactory factoryTimeout = locatorTimeout.createSessionFactory();
    final ClientSession sessionTimeout = factoryTimeout.createSession(true, false, false);
    Xid xid = newXID();
    boolean expectedException = false;
    try {
        sessionTimeout.start(xid, XAResource.TMNOFLAGS);
    } catch (Exception e) {
        expectedException = true;
        e.printStackTrace();
    }
    assertTrue(expectedException);
    // this will release the interceptor and the next response will be out of sync unless we do something about
    latch.countDown();
    sessionTimeout.setTransactionTimeout(30);
    sessionTimeout.close();
    factoryTimeout.close();
    locatorTimeout.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) XAException(javax.transaction.xa.XAException) SessionXAStartMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionXAStartMessage) Xid(javax.transaction.xa.Xid) 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) Test(org.junit.Test)

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