Search in sources :

Example 1 with TransportListener

use of org.apache.activemq.transport.TransportListener in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testDeletedStopsBroker.

@Test
public void testDeletedStopsBroker() throws Exception {
    underTest = new ActiveMQServiceFactory();
    underTest.curator = curator;
    Properties props = new Properties();
    props.put("config", "amq.xml");
    props.put("broker-name", "amq");
    props.put("connectors", "openwire");
    for (int i = 0; i < 10; i++) {
        underTest.updated("b", props);
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(tcp://localhost:61616)?useExponentialBackOff=false&timeout=10000");
        final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
        connection.start();
        assertTrue("is connected", connection.getTransport().isConnected());
        final CountDownLatch interrupted = new CountDownLatch(1);
        connection.getTransport().setTransportListener(new TransportListener() {

            @Override
            public void onCommand(Object o) {
            }

            @Override
            public void onException(IOException e) {
            }

            @Override
            public void transportInterupted() {
                interrupted.countDown();
            }

            @Override
            public void transportResumed() {
            }
        });
        underTest.deleted("b");
        assertTrue("Broker stopped - failover transport interrupted", interrupted.await(5, TimeUnit.SECONDS));
        connection.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TransportListener(org.apache.activemq.transport.TransportListener) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) IOException(java.io.IOException) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with TransportListener

use of org.apache.activemq.transport.TransportListener in project activemq-artemis by apache.

the class FailoverTransactionTest method testPoolingNConsumesAfterReconnect.

@Test
@BMRules(rules = { @BMRule(name = "set no return response and stop the broker", targetClass = "org.apache.activemq.artemis.core.protocol.openwire.OpenWireConnection$CommandProcessor", targetMethod = "processRemoveConsumer", targetLocation = "ENTRY", action = "org.apache.activemq.transport.failover.FailoverTransactionTest.stopBrokerOnCounter()") })
public void testPoolingNConsumesAfterReconnect() throws Exception {
    LOG.info(this + " running test testPoolingNConsumesAfterReconnect");
    count = 0;
    broker = createBroker();
    startBrokerWithDurableQueue();
    doByteman.set(true);
    Vector<Connection> connections = new Vector<>();
    final ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
        configureConnectionFactory(cf);
        Connection connection = cf.createConnection();
        connection.start();
        Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Queue destination = producerSession.createQueue(QUEUE_NAME + "?consumer.prefetchSize=1");
        produceMessage(producerSession, destination);
        connection.close();
        connection = cf.createConnection();
        connection.start();
        connections.add(connection);
        final Session consumerSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        final int sessionCount = 10;
        final Stack<Session> sessions = new Stack<>();
        for (int i = 0; i < sessionCount; i++) {
            sessions.push(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
        }
        final int consumerCount = 1000;
        final Deque<MessageConsumer> consumers = new ArrayDeque<>();
        for (int i = 0; i < consumerCount; i++) {
            consumers.push(consumerSession.createConsumer(destination));
        }
        final FailoverTransport failoverTransport = ((ActiveMQConnection) connection).getTransport().narrow(FailoverTransport.class);
        final TransportListener delegate = failoverTransport.getTransportListener();
        failoverTransport.setTransportListener(new TransportListener() {

            @Override
            public void onCommand(Object command) {
                delegate.onCommand(command);
            }

            @Override
            public void onException(IOException error) {
                delegate.onException(error);
            }

            @Override
            public void transportInterupted() {
                LOG.error("Transport interrupted: " + failoverTransport, new RuntimeException("HERE"));
                for (int i = 0; i < consumerCount && !consumers.isEmpty(); i++) {
                    executorService.execute(new Runnable() {

                        @Override
                        public void run() {
                            MessageConsumer localConsumer = null;
                            try {
                                synchronized (delegate) {
                                    localConsumer = consumers.pop();
                                }
                                localConsumer.receive(1);
                                LOG.info("calling close() " + ((ActiveMQMessageConsumer) localConsumer).getConsumerId());
                                localConsumer.close();
                            } catch (NoSuchElementException nse) {
                            } catch (Exception ignored) {
                                LOG.error("Ex on: " + ((ActiveMQMessageConsumer) localConsumer).getConsumerId(), ignored);
                            }
                        }
                    });
                }
                delegate.transportInterupted();
            }

            @Override
            public void transportResumed() {
                delegate.transportResumed();
            }
        });
        MessageConsumer consumer = null;
        synchronized (delegate) {
            consumer = consumers.pop();
        }
        LOG.info("calling close to trigger broker stop " + ((ActiveMQMessageConsumer) consumer).getConsumerId());
        consumer.close();
        LOG.info("waiting latch: " + brokerStopLatch.getCount());
        // will be stopped by the plugin
        Assert.assertTrue(brokerStopLatch.await(60, TimeUnit.SECONDS));
        doByteman.set(false);
        broker = createBroker();
        broker.start();
        consumer = consumerSession.createConsumer(destination);
        LOG.info("finally consuming message: " + ((ActiveMQMessageConsumer) consumer).getConsumerId());
        Message msg = null;
        for (int i = 0; i < 4 && msg == null; i++) {
            msg = consumer.receive(1000);
        }
        LOG.info("post: from consumer1 received: " + msg);
        Assert.assertNotNull("got message after failover", msg);
        msg.acknowledge();
    } finally {
        executorService.shutdown();
        for (Connection c : connections) {
            c.close();
        }
    }
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) Connection(javax.jms.Connection) OpenWireConnection(org.apache.activemq.artemis.core.protocol.openwire.OpenWireConnection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) TransactionRolledBackException(javax.jms.TransactionRolledBackException) Stack(java.util.Stack) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TransportListener(org.apache.activemq.transport.TransportListener) ExecutorService(java.util.concurrent.ExecutorService) Vector(java.util.Vector) Queue(javax.jms.Queue) NoSuchElementException(java.util.NoSuchElementException) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession) Test(org.junit.Test) OpenwireArtemisBaseTest(org.apache.activemq.broker.artemiswrapper.OpenwireArtemisBaseTest) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 3 with TransportListener

use of org.apache.activemq.transport.TransportListener in project activemq-artemis by apache.

the class FailoverTransportBrokerTest method testNoBrokersInBrokerInfo.

public void testNoBrokersInBrokerInfo() throws Exception {
    final BrokerInfo[] info = new BrokerInfo[1];
    TransportListener listener = new TransportListener() {

        @Override
        public void onCommand(Object command) {
            LOG.info("Got command: " + command);
            if (command instanceof BrokerInfo) {
                info[0] = (BrokerInfo) command;
            }
        }

        @Override
        public void onException(IOException error) {
        // To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void transportInterupted() {
        // To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void transportResumed() {
        // To change body of implemented methods use File | Settings | File Templates.
        }
    };
    @SuppressWarnings("unused") StubConnection c = createFailoverConnection(listener);
    int count = 0;
    while (count++ < 20 && info[0] == null) {
        TimeUnit.SECONDS.sleep(1);
    }
    Assert.assertNotNull("got a valid brokerInfo after 20 secs", info[0]);
    Assert.assertNull("no peer brokers present", info[0].getPeerBrokerInfos());
}
Also used : TransportListener(org.apache.activemq.transport.TransportListener) StubConnection(org.apache.activemq.broker.StubConnection) IOException(java.io.IOException) BrokerInfo(org.apache.activemq.command.BrokerInfo)

Example 4 with TransportListener

use of org.apache.activemq.transport.TransportListener in project activemq-artemis by apache.

the class FailoverTransportTest method testLocalhostPortSyntax.

@Test
public void testLocalhostPortSyntax() throws Exception {
    transport = TransportFactory.connect(new URI("failover://(tcp://localhost:1111/localhost:2111)"));
    transport.setTransportListener(new TransportListener() {

        @Override
        public void onCommand(Object command) {
        }

        @Override
        public void onException(IOException error) {
        }

        @Override
        public void transportInterupted() {
        }

        @Override
        public void transportResumed() {
        }
    });
    failoverTransport = transport.narrow(FailoverTransport.class);
    transport.start();
}
Also used : TransportListener(org.apache.activemq.transport.TransportListener) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.Test)

Example 5 with TransportListener

use of org.apache.activemq.transport.TransportListener in project activemq-artemis by apache.

the class FailoverTransportTest method testReconnectUnlimited.

@Test(timeout = 30000)
@Ignore("Test fails on windows")
public void testReconnectUnlimited() throws Exception {
    Transport transport = TransportFactory.connect(new URI("failover://(tcp://0.0.0.0:61616)?useExponentialBackOff=false&reconnectDelay=0&initialReconnectDelay=0"));
    transport.setTransportListener(new TransportListener() {

        @Override
        public void onCommand(Object command) {
        }

        @Override
        public void onException(IOException error) {
        }

        @Override
        public void transportInterupted() {
        }

        @Override
        public void transportResumed() {
        }
    });
    transport.start();
    this.failoverTransport = transport.narrow(FailoverTransport.class);
    assertTrue("no implicit limit of 1000", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return failoverTransport.getConnectFailures() > 1002;
        }
    }));
}
Also used : TransportListener(org.apache.activemq.transport.TransportListener) IOException(java.io.IOException) Transport(org.apache.activemq.transport.Transport) URI(java.net.URI) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TransportListener (org.apache.activemq.transport.TransportListener)16 IOException (java.io.IOException)15 URI (java.net.URI)8 Transport (org.apache.activemq.transport.Transport)6 Test (org.junit.Test)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 WireFormatInfo (org.apache.activemq.command.WireFormatInfo)3 URISyntaxException (java.net.URISyntaxException)2 Connection (javax.jms.Connection)2 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 Queue (javax.jms.Queue)2 Session (javax.jms.Session)2 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)2 OpenwireArtemisBaseTest (org.apache.activemq.broker.artemiswrapper.OpenwireArtemisBaseTest)2 BrokerInfo (org.apache.activemq.command.BrokerInfo)2 TransportAcceptListener (org.apache.activemq.transport.TransportAcceptListener)2 ArrayDeque (java.util.ArrayDeque)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1