Search in sources :

Example 26 with RemotingConnection

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

the class ReattachTest method testRetryInterval.

@Test
public void testRetryInterval() throws Exception {
    final long retryInterval = 500;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 60;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    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 = 1000;
    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().writeString("aardvarks");
        producer.send(message);
    }
    ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
    InVMConnector.failOnCreateConnection = true;
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    long start = System.currentTimeMillis();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(retryInterval / 2);
            } catch (InterruptedException ignore) {
            }
            InVMConnector.failOnCreateConnection = false;
        }
    };
    t.start();
    conn.fail(new ActiveMQNotConnectedException());
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(500);
        Assert.assertNotNull(message);
        Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
        Assert.assertEquals(i, message.getIntProperty("count").intValue());
        message.acknowledge();
    }
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull(message);
    long end = System.currentTimeMillis();
    Assert.assertTrue(end - start >= retryInterval);
    session.close();
    sf.close();
    t.join();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) 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) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 27 with RemotingConnection

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

the class ReattachTest method testDelayedReattach.

/*
    * Test failure on connection, simulate failure to create connection for a while, then
    * allow connection to be recreated
    */
@Test
public void testDelayedReattach() throws Exception {
    final long retryInterval = 50;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 60;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    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 = 1000;
    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().writeString("aardvarks");
        producer.send(message);
    }
    ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
    InVMConnector.failOnCreateConnection = true;
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(retryInterval * 3);
            } catch (InterruptedException ignore) {
            }
            InVMConnector.failOnCreateConnection = false;
        }
    };
    t.start();
    conn.fail(new ActiveMQNotConnectedException());
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(500);
        Assert.assertNotNull(message);
        Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
        Assert.assertEquals(i, message.getIntProperty("count").intValue());
        message.acknowledge();
    }
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull(message);
    session.close();
    sf.close();
    t.join();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) 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) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 28 with RemotingConnection

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

the class BindingsClusterTest method getForwardingConnection.

private RemotingConnection getForwardingConnection(final Bridge bridge) throws Exception {
    long start = System.currentTimeMillis();
    do {
        RemotingConnection forwardingConnection = ((BridgeImpl) bridge).getForwardingConnection();
        if (forwardingConnection != null) {
            return forwardingConnection;
        }
        Thread.sleep(10);
    } while (System.currentTimeMillis() - start < 50000);
    throw new IllegalStateException("Failed to get forwarding connection");
}
Also used : BridgeImpl(org.apache.activemq.artemis.core.server.cluster.impl.BridgeImpl) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)

Example 29 with RemotingConnection

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

the class MQTTInterceptorPropertiesTest method testCheckInterceptedMQTTMessageProperties.

@Test(timeout = 60000)
public void testCheckInterceptedMQTTMessageProperties() throws Exception {
    final String addressQueue = name.getMethodName();
    final String msgText = "Test intercepted message";
    final boolean retained = true;
    Map<String, Object> expectedProperties = new ArrayMap<>();
    expectedProperties.put(ADDRESS, addressQueue);
    expectedProperties.put(MESSAGE_TEXT, msgText);
    expectedProperties.put(RETAINED, retained);
    final MQTTClientProvider subscribeProvider = getMQTTClientProvider();
    initializeConnection(subscribeProvider);
    subscribeProvider.subscribe(addressQueue, AT_MOST_ONCE);
    final CountDownLatch latch = new CountDownLatch(1);
    MQTTInterceptor incomingInterceptor = new MQTTInterceptor() {

        @Override
        public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
            System.out.println("incoming");
            if (packet.getClass() == MqttPublishMessage.class) {
                return checkMessageProperties(packet, expectedProperties);
            } else {
                return true;
            }
        }
    };
    MQTTInterceptor outgoingInterceptor = new MQTTInterceptor() {

        @Override
        public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
            System.out.println("outgoing");
            if (packet.getClass() == MqttPublishMessage.class) {
                return checkMessageProperties(packet, expectedProperties);
            } else {
                return true;
            }
        }
    };
    server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
    server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                byte[] payload = subscribeProvider.receive(10000);
                assertNotNull("Should get a message", payload);
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    final MQTTClientProvider publishProvider = getMQTTClientProvider();
    initializeConnection(publishProvider);
    publishProvider.publish(addressQueue, msgText.getBytes(), AT_MOST_ONCE, retained);
    latch.await(10, TimeUnit.SECONDS);
    subscribeProvider.disconnect();
    publishProvider.disconnect();
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ArrayMap(org.apache.felix.resolver.util.ArrayMap) CountDownLatch(java.util.concurrent.CountDownLatch) MQTTInterceptor(org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Example 30 with RemotingConnection

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

the class JMSFailoverTest method testManualFailover.

@Test
public void testManualFailover() throws Exception {
    ActiveMQConnectionFactory jbcfLive = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcfLive.setBlockOnNonDurableSend(true);
    jbcfLive.setBlockOnDurableSend(true);
    ActiveMQConnectionFactory jbcfBackup = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, backupParams));
    jbcfBackup.setBlockOnNonDurableSend(true);
    jbcfBackup.setBlockOnDurableSend(true);
    jbcfBackup.setInitialConnectAttempts(-1);
    jbcfBackup.setReconnectAttempts(-1);
    Connection connLive = jbcfLive.createConnection();
    MyExceptionListener listener = new MyExceptionListener();
    connLive.setExceptionListener(listener);
    Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
    RemotingConnection coreConnLive = ((ClientSessionInternal) coreSessionLive).getConnection();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSessionLive.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sessLive.createQueue("myqueue");
    final int numMessages = 1000;
    MessageProducer producerLive = sessLive.createProducer(queue);
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = sessLive.createTextMessage("message" + i);
        producerLive.send(tm);
    }
    // Note we block on P send to make sure all messages get to server before failover
    JMSUtil.crash(liveServer, coreSessionLive);
    connLive.close();
    // Now recreate on backup
    Connection connBackup = jbcfBackup.createConnection();
    Session sessBackup = connBackup.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumerBackup = sessBackup.createConsumer(queue);
    connBackup.start();
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = (TextMessage) consumerBackup.receive(1000);
        Assert.assertNotNull(tm);
        Assert.assertEquals("message" + i, tm.getText());
    }
    TextMessage tm = (TextMessage) consumerBackup.receiveNoWait();
    Assert.assertNull(tm);
    connBackup.close();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Aggregations

RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)84 Test (org.junit.Test)44 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)36 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)31 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)30 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)22 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)21 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)17 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)16 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)15 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)13 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)12 Connection (javax.jms.Connection)11 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)11 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)11 Session (javax.jms.Session)10