Search in sources :

Example 41 with RemotingConnection

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

the class AmqpInboundConnectionTest method testCloseIsSentOnConnectionClose.

@Test(timeout = 60000)
public void testCloseIsSentOnConnectionClose() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection amqpConnection = client.connect();
    try {
        for (RemotingConnection connection : server.getRemotingService().getConnections()) {
            server.getRemotingService().removeConnection(connection);
            connection.disconnect(true);
        }
        Wait.assertTrue(amqpConnection::isClosed);
        assertEquals(AmqpSupport.CONNECTION_FORCED, amqpConnection.getConnection().getRemoteCondition().getCondition());
    } finally {
        amqpConnection.close();
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Test(org.junit.Test)

Example 42 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection 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)

Example 43 with RemotingConnection

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

the class StompPluginTest method createServer.

@Override
protected JMSServerManager createServer() throws Exception {
    JMSServerManager server = super.createServer();
    server.getActiveMQServer().registerBrokerPlugin(verifier);
    server.getActiveMQServer().registerBrokerPlugin(new ActiveMQServerPlugin() {

        @Override
        public void beforeCreateSession(String name, String username, int minLargeMessageSize, RemotingConnection connection, boolean autoCommitSends, boolean autoCommitAcks, boolean preAcknowledge, boolean xa, String defaultAddress, SessionCallback callback, boolean autoCreateQueues, OperationContext context, Map<SimpleString, RoutingType> prefixes) throws ActiveMQException {
            if (connection instanceof StompConnection) {
                stompBeforeCreateSession.set(true);
            }
        }

        @Override
        public void beforeCloseSession(ServerSession session, boolean failed) throws ActiveMQException {
            if (session.getRemotingConnection() instanceof StompConnection) {
                stompBeforeRemoveSession.set(true);
            }
        }
    });
    return server;
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) JMSServerManager(org.apache.activemq.artemis.jms.server.JMSServerManager) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SessionCallback(org.apache.activemq.artemis.spi.core.protocol.SessionCallback) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQServerPlugin(org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin) StompConnection(org.apache.activemq.artemis.core.protocol.stomp.StompConnection) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Example 44 with RemotingConnection

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

the class NetworkAddressTestBase method testConnection.

public void testConnection(final String acceptorHost, final String connectorHost, final boolean mustConnect, final int localPort) throws Exception {
    System.out.println("acceptor=" + acceptorHost + ", connector=" + connectorHost + ", mustConnect=" + mustConnect);
    Map<String, Object> params = new HashMap<>();
    params.put(getHostPropertyKey(), acceptorHost);
    TransportConfiguration acceptorConfig = new TransportConfiguration(getAcceptorFactoryClassName(), params);
    Set<TransportConfiguration> transportConfigs = new HashSet<>();
    transportConfigs.add(acceptorConfig);
    Configuration config = createDefaultNettyConfig();
    config.setAcceptorConfigurations(transportConfigs);
    ActiveMQServer messagingService = createServer(false, config);
    try {
        messagingService.start();
        params = new HashMap<>();
        params.put(getHostPropertyKey(), connectorHost);
        if (localPort != 0) {
            params.put(getLocalPortProperty(), localPort);
        }
        TransportConfiguration connectorConfig = new TransportConfiguration(getConnectorFactoryClassName(), params);
        ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(connectorConfig));
        if (mustConnect) {
            ClientSessionFactory sf = createSessionFactory(locator);
            if (localPort != 0) {
                Iterator<RemotingConnection> iterator = messagingService.getRemotingService().getConnections().iterator();
                Assert.assertTrue("no connection created", iterator.hasNext());
                String address = iterator.next().getTransportConnection().getRemoteAddress();
                Assert.assertTrue(address.endsWith(":" + localPort));
            }
            sf.close();
            System.out.println("connection OK");
        } else {
            try {
                locator.createSessionFactory();
                Assert.fail("session creation must fail because connector must not be able to connect to the server bound to another network interface");
            } catch (Exception e) {
            }
        }
    } finally {
        messagingService.stop();
    }
}
Also used : Configuration(org.apache.activemq.artemis.core.config.Configuration) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) HashMap(java.util.HashMap) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) HashSet(java.util.HashSet)

Example 45 with RemotingConnection

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

the class PingTest method testClientFailureNoServerPing.

/*
   * Test the client triggering failure due to no ping from server received in time
   */
@Test
public void testClientFailureNoServerPing() throws Exception {
    // server must received at least one ping from the client to pass
    // so that the server connection TTL is configured with the client value
    final CountDownLatch pingOnServerLatch = 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) {
                pingOnServerLatch.countDown();
            }
            return true;
        }
    });
    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);
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(false, true, true);
    Assert.assertEquals(1, ((ClientSessionFactoryInternal) csf).numConnections());
    final CountDownLatch clientLatch = new CountDownLatch(1);
    SessionFailureListener clientListener = new SessionFailureListener() {

        @Override
        public void connectionFailed(final ActiveMQException me, boolean failedOver) {
            clientLatch.countDown();
        }

        @Override
        public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed(me, failedOver);
        }

        @Override
        public void beforeReconnect(final ActiveMQException exception) {
        }
    };
    final CountDownLatch serverLatch = new CountDownLatch(1);
    CloseListener serverListener = new CloseListener() {

        @Override
        public void connectionClosed() {
            serverLatch.countDown();
        }
    };
    session.addFailureListener(clientListener);
    CoreRemotingConnection serverConn = null;
    while (serverConn == null) {
        Set<RemotingConnection> conns = server.getRemotingService().getConnections();
        if (!conns.isEmpty()) {
            serverConn = (CoreRemotingConnection) server.getRemotingService().getConnections().iterator().next();
        } else {
            // It's async so need to wait a while
            Thread.sleep(10);
        }
    }
    serverConn.addCloseListener(serverListener);
    Assert.assertTrue("server has not received any ping from the client", pingOnServerLatch.await(4000, TimeUnit.MILLISECONDS));
    // we let the server receives at least 1 ping (so that it uses the client ConnectionTTL value)
    // Setting the handler to null will prevent server sending pings back to client
    serverConn.getChannel(0, -1).setHandler(null);
    Assert.assertTrue(clientLatch.await(8 * PingTest.CLIENT_FAILURE_CHECK_PERIOD, TimeUnit.MILLISECONDS));
    // Server connection will be closed too, when client closes client side connection after failure is detected
    Assert.assertTrue(serverLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval(), TimeUnit.MILLISECONDS));
    long start = System.currentTimeMillis();
    while (true) {
        if (!server.getRemotingService().getConnections().isEmpty() && System.currentTimeMillis() - start < 10000) {
            Thread.sleep(500);
        } else {
            break;
        }
    }
    Assert.assertTrue(server.getRemotingService().getConnections().isEmpty());
    session.close();
    csf.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) 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) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) 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

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