Search in sources :

Example 46 with RemotingConnection

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

the class PingTest method testServerFailureNoPing.

/*
    * Test the server timing out a connection since it doesn't receive a ping in time
    */
@Test
public void testServerFailureNoPing() throws Exception {
    TransportConfiguration transportConfig = new TransportConfiguration(INVM_CONNECTOR_FACTORY);
    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(transportConfig));
    locator.setClientFailureCheckPeriod(PingTest.CLIENT_FAILURE_CHECK_PERIOD);
    locator.setConnectionTTL(PingTest.CLIENT_FAILURE_CHECK_PERIOD * 2);
    ClientSessionFactoryImpl csf = (ClientSessionFactoryImpl) createSessionFactory(locator);
    Listener clientListener = new Listener();
    ClientSession session = csf.createSession(false, true, true);
    Assert.assertEquals(1, csf.numConnections());
    session.addFailureListener(clientListener);
    // We need to get it to stop pinging after one
    csf.stopPingingAfterOne();
    RemotingConnection serverConn = null;
    while (serverConn == null) {
        Set<RemotingConnection> conns = server.getRemotingService().getConnections();
        if (!conns.isEmpty()) {
            serverConn = server.getRemotingService().getConnections().iterator().next();
        } else {
            // It's async so need to wait a while
            Thread.sleep(10);
        }
    }
    Listener serverListener = new Listener();
    serverConn.addFailureListener(serverListener);
    for (int i = 0; i < 1000; i++) {
        // a few tries to avoid a possible race caused by GCs or similar issues
        if (server.getRemotingService().getConnections().isEmpty() && clientListener.getException() != null) {
            break;
        }
        Thread.sleep(10);
    }
    if (!server.getRemotingService().getConnections().isEmpty()) {
        RemotingConnection serverConn2 = server.getRemotingService().getConnections().iterator().next();
        PingTest.log.info("Serverconn2 is " + serverConn2);
    }
    Assert.assertTrue(server.getRemotingService().getConnections().isEmpty());
    // The client listener should be called too since the server will close it from the server side which will result
    // in the
    // netty detecting closure on the client side and then calling failure listener
    Assert.assertNotNull(clientListener.getException());
    Assert.assertNotNull(serverListener.getException());
    session.close();
    csf.close();
    locator.close();
}
Also used : ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 47 with RemotingConnection

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

the class PingTest method testNoPingingOnInVMConnection.

/*
    * Test that pinging is disabled for in-vm connection when using the default settings
    */
@Test
public void testNoPingingOnInVMConnection() throws Exception {
    // server should receive one and only one ping from the client so that
    // the server connection TTL is configured with the client value
    final CountDownLatch requiredPings = new CountDownLatch(1);
    final CountDownLatch unwantedPings = new CountDownLatch(2);
    server.getRemotingService().addIncomingInterceptor(new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
            if (packet.getType() == PacketImpl.PING) {
                Assert.assertEquals(ActiveMQClient.DEFAULT_CONNECTION_TTL_INVM, ((Ping) packet).getConnectionTTL());
                unwantedPings.countDown();
                requiredPings.countDown();
            }
            return true;
        }
    });
    TransportConfiguration transportConfig = new TransportConfiguration("org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory");
    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(transportConfig));
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(false, true, true);
    Assert.assertEquals(1, ((ClientSessionFactoryInternal) csf).numConnections());
    Assert.assertTrue("server didn't received an expected ping from the client", requiredPings.await(5000, TimeUnit.MILLISECONDS));
    Assert.assertFalse("server received an unexpected ping from the client", unwantedPings.await(ActiveMQClient.DEFAULT_CONNECTION_TTL * 2, TimeUnit.MILLISECONDS));
    session.close();
    csf.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Ping(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.Ping) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) 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 48 with RemotingConnection

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

the class PingTest method testNoFailureWithPinging.

/*
    * Test that no failure listeners are triggered in a non failure case with pinging going on
    */
@Test
public void testNoFailureWithPinging() throws Exception {
    ServerLocator locator = createNettyNonHALocator();
    locator.setClientFailureCheckPeriod(PingTest.CLIENT_FAILURE_CHECK_PERIOD);
    locator.setConnectionTTL(PingTest.CLIENT_FAILURE_CHECK_PERIOD * 2);
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(false, true, true);
    PingTest.log.info("Created session");
    Assert.assertEquals(1, ((ClientSessionFactoryInternal) csf).numConnections());
    Listener clientListener = new Listener();
    session.addFailureListener(clientListener);
    RemotingConnection serverConn = null;
    while (serverConn == null) {
        Set<RemotingConnection> conns = server.getRemotingService().getConnections();
        if (!conns.isEmpty()) {
            serverConn = server.getRemotingService().getConnections().iterator().next();
        } else {
            // It's async so need to wait a while
            Thread.sleep(10);
        }
    }
    Listener serverListener = new Listener();
    serverConn.addFailureListener(serverListener);
    Thread.sleep(PingTest.CLIENT_FAILURE_CHECK_PERIOD * 10);
    Assert.assertNull(clientListener.getException());
    Assert.assertNull(serverListener.getException());
    RemotingConnection serverConn2 = server.getRemotingService().getConnections().iterator().next();
    PingTest.log.info("Server conn2 is " + serverConn2);
    Assert.assertTrue(serverConn == serverConn2);
    session.close();
    csf.close();
    locator.close();
}
Also used : SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 49 with RemotingConnection

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

the class ClientSessionFactoryImpl method checkCloseConnection.

private void checkCloseConnection() {
    RemotingConnection connectionInUse = connection;
    Connector connectorInUse = connector;
    if (connectionInUse != null && sessions.size() == 0) {
        cancelScheduledTasks();
        try {
            connectionInUse.destroy();
        } catch (Throwable ignore) {
        }
        connection = null;
        try {
            if (connectorInUse != null) {
                connectorInUse.close();
            }
        } catch (Throwable ignore) {
        }
        connector = null;
    }
}
Also used : Connector(org.apache.activemq.artemis.spi.core.remoting.Connector) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)

Example 50 with RemotingConnection

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

the class MDBMultipleHandlersServerDisconnectTest method testReconnectMDBNoMessageLoss.

@Test
public void testReconnectMDBNoMessageLoss() throws Exception {
    AddressSettings settings = new AddressSettings();
    settings.setRedeliveryDelay(100);
    settings.setMaxDeliveryAttempts(-1);
    server.getAddressSettingsRepository().clear();
    server.getAddressSettingsRepository().addMatch("#", settings);
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    resourceAdapter = qResourceAdapter;
    resourceAdapter.setConfirmationWindowSize(-1);
    resourceAdapter.setCallTimeout(1000L);
    resourceAdapter.setConsumerWindowSize(1024 * 1024);
    resourceAdapter.setReconnectAttempts(-1);
    resourceAdapter.setRetryInterval(100L);
    // qResourceAdapter.setTransactionManagerLocatorClass(DummyTMLocator.class.getName());
    // qResourceAdapter.setTransactionManagerLocatorMethod("getTM");
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.setConnectorClassName(NETTY_CONNECTOR_FACTORY);
    qResourceAdapter.start(ctx);
    final int NUMBER_OF_SESSIONS = 10;
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setTransactionTimeout(1);
    spec.setMaxSession(NUMBER_OF_SESSIONS);
    spec.setSetupAttempts(-1);
    spec.setSetupInterval(100L);
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    // Some the routines would be screwed up if using the default one
    Assert.assertFalse(spec.isHasBeenUpdated());
    TestEndpointFactory endpointFactory = new TestEndpointFactory(true);
    qResourceAdapter.endpointActivation(endpointFactory, spec);
    Assert.assertEquals(1, resourceAdapter.getActivations().values().size());
    ActiveMQActivation activation = resourceAdapter.getActivations().values().toArray(new ActiveMQActivation[1])[0];
    final int NUMBER_OF_MESSAGES = 1000;
    Thread producer = new Thread() {

        @Override
        public void run() {
            try {
                ServerLocator locator = createInVMLocator(0);
                ClientSessionFactory factory = locator.createSessionFactory();
                ClientSession session = factory.createSession(false, false);
                ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
                StringBuffer buffer = new StringBuffer();
                for (int b = 0; b < 500; b++) {
                    buffer.append("ab");
                }
                for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
                    ClientMessage message = session.createMessage(true);
                    message.getBodyBuffer().writeString(buffer.toString() + i);
                    message.putIntProperty("i", i);
                    clientProducer.send(message);
                    if (i % 100 == 0) {
                        session.commit();
                    }
                }
                session.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    producer.start();
    final AtomicBoolean metaDataFailed = new AtomicBoolean(false);
    Thread buggerThread = new Thread() {

        @Override
        public void run() {
            while (running.get()) {
                try {
                    Thread.sleep(RandomUtil.randomInterval(100, 200));
                } catch (InterruptedException intex) {
                    intex.printStackTrace();
                    return;
                }
                List<ServerSession> serverSessions = lookupServerSessions("resource-adapter", NUMBER_OF_SESSIONS);
                System.err.println("Contains " + serverSessions.size() + " RA sessions");
                if (serverSessions.size() != NUMBER_OF_SESSIONS) {
                    System.err.println("the server was supposed to have " + NUMBER_OF_MESSAGES + " RA Sessions but it only contained accordingly to the meta-data");
                    metaDataFailed.set(true);
                } else if (serverSessions.size() == NUMBER_OF_SESSIONS) {
                    // it became the same after some reconnect? which would be acceptable
                    metaDataFailed.set(false);
                }
                if (playServerClosingSession && serverSessions.size() > 0) {
                    int randomBother = RandomUtil.randomInterval(0, serverSessions.size() - 1);
                    System.out.println("bugging session " + randomBother);
                    ServerSession serverSession = serverSessions.get(randomBother);
                    if (playServerClosingConsumer && RandomUtil.randomBoolean()) {
                        // will play this randomly, only half of the times
                        for (ServerConsumer consumer : serverSession.getServerConsumers()) {
                            try {
                                // Simulating a rare race that could happen in production
                                // where the consumer is closed while things are still happening
                                consumer.close(true);
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    RemotingConnection connection = serverSession.getRemotingConnection();
                    connection.fail(new ActiveMQException("failed at random " + randomBother));
                }
            }
        }
    };
    buggerThread.start();
    ServerLocator locator = createInVMLocator(0);
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, false);
    session.start();
    ClientConsumer consumer = session.createConsumer("outQueue");
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        ClientMessage message = consumer.receive(60000);
        if (message == null) {
            break;
        }
        if (i == NUMBER_OF_MESSAGES * 0.50) {
            // This is to make sure the MDBs will survive a reboot
            // and no duplications or message loss will happen because of this
            System.err.println("Rebooting the MDBs at least once!");
            activation.startReconnectThread("I");
        }
        if (i == NUMBER_OF_MESSAGES * 0.90) {
            System.out.println("Disabled failures at " + i);
            playTXTimeouts = false;
            playServerClosingSession = false;
            playServerClosingConsumer = false;
        }
        System.out.println("Received " + i + " messages");
        doReceiveMessage(message);
        if (i % 200 == 0) {
            System.out.println("received " + i);
            session.commit();
        }
    }
    session.commit();
    while (true) {
        ClientMessage message = consumer.receiveImmediate();
        if (message == null) {
            break;
        }
        System.out.println("Received extra message " + message);
        doReceiveMessage(message);
    }
    session.commit();
    Assert.assertNull(consumer.receiveImmediate());
    StringWriter writer = new StringWriter();
    PrintWriter out = new PrintWriter(writer);
    boolean failed = false;
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        AtomicInteger atomicInteger = mapCounter.get(Integer.valueOf(i));
        if (atomicInteger == null) {
            out.println("didn't receive message with i=" + i);
            failed = true;
        } else if (atomicInteger.get() > 1) {
            out.println("message with i=" + i + " received " + atomicInteger.get() + " times");
            failed = true;
        }
    }
    running.set(false);
    buggerThread.join();
    producer.join();
    qResourceAdapter.stop();
    session.close();
    if (failed) {
        for (int i = 0; i < 10; i++) {
            System.out.println("----------------------------------------------------");
        }
        System.out.println(writer.toString());
    }
    Assert.assertFalse(writer.toString(), failed);
    System.out.println("Received " + NUMBER_OF_MESSAGES + " messages");
    Assert.assertFalse("There was meta-data failures, some sessions didn't reconnect properly", metaDataFailed.get());
}
Also used : ActiveMQActivation(org.apache.activemq.artemis.ra.inflow.ActiveMQActivation) 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) StringWriter(java.io.StringWriter) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) PrintWriter(java.io.PrintWriter) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint) ResourceException(javax.resource.ResourceException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) LocalTransactionException(javax.resource.spi.LocalTransactionException) RollbackException(javax.transaction.RollbackException) UnavailableException(javax.resource.spi.UnavailableException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) 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