Search in sources :

Example 1 with ClientSessionFactoryImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl in project activemq-artemis by apache.

the class ConsumerStuckTest method testClientStuckTestWithDirectDelivery.

@Test
public void testClientStuckTestWithDirectDelivery() throws Exception {
    ServerLocator locator = createNettyNonHALocator().setConnectionTTL(1000).setClientFailureCheckPeriod(100).setConsumerWindowSize(10 * 1024 * 1024).setCallTimeout(1000);
    ClientSessionFactory sf = locator.createSessionFactory();
    ((ClientSessionFactoryImpl) sf).stopPingingAfterOne();
    RemotingConnectionImpl remotingConnection = (RemotingConnectionImpl) sf.getConnection();
    ClientSession session = sf.createSession(false, true, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    final int numMessages = 10000;
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    final NettyConnection nettyConnection = (NettyConnection) remotingConnection.getTransportConnection();
    Thread tReceive = new Thread() {

        @Override
        public void run() {
            boolean first = true;
            try {
                while (!Thread.interrupted()) {
                    ClientMessage received = consumer.receive(500);
                    System.out.println("Received " + received);
                    if (first) {
                        first = false;
                        nettyConnection.getNettyChannel().config().setAutoRead(false);
                    }
                    if (received != null) {
                        received.acknowledge();
                    }
                }
            } catch (Throwable e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        }
    };
    tReceive.start();
    Thread sender = new Thread() {

        @Override
        public void run() {
            try (ServerLocator locator = createNettyNonHALocator();
                ClientSessionFactory factory = locator.createSessionFactory();
                ClientSession session = factory.createSession(false, true, true, true);
                ClientProducer producer = session.createProducer(QUEUE)) {
                for (int i = 0; i < numMessages; i++) {
                    ClientMessage message = createTextMessage(session, "m" + i);
                    producer.send(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    sender.start();
    try {
        long timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getSessions().size() != 2) {
            Thread.sleep(10);
        }
        assertEquals(2, server.getSessions().size());
        System.out.println("sessions = " + server.getSessions().size());
        assertEquals(2, server.getConnectionCount());
        timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getSessions().size() != 1) {
            Thread.sleep(10);
        }
        System.out.println("Size = " + server.getConnectionCount());
        System.out.println("sessions = " + server.getSessions().size());
        if (server.getSessions().size() != 1) {
            System.out.println(threadDump("Thread dump"));
            fail("The cleanup wasn't able to finish cleaning the session. It's probably stuck, look at the thread dump generated by the test for more information");
        }
        sender.join();
        timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getConnectionCount() != 0) {
            Thread.sleep(10);
        }
        assertEquals(0, server.getConnectionCount());
    } finally {
        nettyConnection.getNettyChannel().config().setAutoRead(true);
        tReceive.interrupt();
        tReceive.join();
    }
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) RemotingConnectionImpl(org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 2 with ClientSessionFactoryImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl in project activemq-artemis by apache.

the class ActiveMQServerControlTest method testListConnections.

@Test
public void testListConnections() throws Exception {
    SimpleString queueName1 = new SimpleString("my_queue_one");
    SimpleString addressName1 = new SimpleString("my_address_one");
    ActiveMQServerControl serverControl = createManagementControl();
    server.addAddressInfo(new AddressInfo(addressName1, RoutingType.ANYCAST));
    server.createQueue(addressName1, RoutingType.ANYCAST, queueName1, null, false, false);
    ClientSessionFactoryImpl csf = null;
    ClientSessionFactoryImpl csf2 = null;
    ClientSessionFactoryImpl csf3 = null;
    // create some consumers
    try (ServerLocator locator = createInVMNonHALocator()) {
        // sleep as test compares creationTime
        csf = (ClientSessionFactoryImpl) createSessionFactory(locator);
        Thread.sleep(500);
        csf2 = (ClientSessionFactoryImpl) createSessionFactory(locator);
        Thread.sleep(500);
        csf3 = (ClientSessionFactoryImpl) createSessionFactory(locator);
        ClientSession session1_c1 = csf.createSession();
        ClientSession session2_c1 = csf.createSession();
        ClientSession session1_c2 = csf2.createSession();
        ClientSession session2_c2 = csf2.createSession();
        ClientSession session3_c2 = csf2.createSession();
        ClientSession session4_c2 = csf2.createSession();
        ClientSession session1_c4 = csf3.createSession();
        ClientSession session2_c4 = csf3.createSession();
        ClientSession session3_c4 = csf3.createSession();
        String filterString = createJsonFilter("SESSION_COUNT", "GREATER_THAN", "1");
        String connectionsAsJsonString = serverControl.listConnections(filterString, 1, 50);
        JsonObject connectionsAsJsonObject = JsonUtil.readJsonObject(connectionsAsJsonString);
        JsonArray array = (JsonArray) connectionsAsJsonObject.get("data");
        Assert.assertEquals("number of connections returned from query", 3, array.size());
        JsonObject jsonConnection = array.getJsonObject(0);
        // check all fields
        Assert.assertNotEquals("connectionID", "", jsonConnection.getString("connectionID"));
        Assert.assertNotEquals("remoteAddress", "", jsonConnection.getString("remoteAddress"));
        Assert.assertEquals("users", "", jsonConnection.getString("users"));
        Assert.assertNotEquals("creationTime", "", jsonConnection.getString("creationTime"));
        Assert.assertNotEquals("implementation", "", jsonConnection.getString("implementation"));
        Assert.assertNotEquals("protocol", "", jsonConnection.getString("protocol"));
        Assert.assertEquals("clientID", "", jsonConnection.getString("clientID"));
        Assert.assertNotEquals("localAddress", "", jsonConnection.getString("localAddress"));
        Assert.assertEquals("sessionCount", 2, jsonConnection.getInt("sessionCount"));
        // check default order
        Assert.assertEquals("connection1 default Order", csf.getConnection().getID(), array.getJsonObject(0).getString("connectionID"));
        Assert.assertEquals("connection2 default Order", csf2.getConnection().getID(), array.getJsonObject(1).getString("connectionID"));
        Assert.assertEquals("connection3 session Order", csf3.getConnection().getID(), array.getJsonObject(2).getString("connectionID"));
        // check order by session count desc
        filterString = createJsonFilter("SESSION_COUNT", "GREATER_THAN", "1", "sessionCount", "desc");
        connectionsAsJsonString = serverControl.listConnections(filterString, 1, 50);
        connectionsAsJsonObject = JsonUtil.readJsonObject(connectionsAsJsonString);
        array = (JsonArray) connectionsAsJsonObject.get("data");
        Assert.assertEquals("number of connections returned from query", 3, array.size());
        Assert.assertEquals("connection2 session Order", csf2.getConnection().getID(), array.getJsonObject(0).getString("connectionID"));
        Assert.assertEquals("connection3 session Order", csf3.getConnection().getID(), array.getJsonObject(1).getString("connectionID"));
        Assert.assertEquals("connection1 session Order", csf.getConnection().getID(), array.getJsonObject(2).getString("connectionID"));
        // check order by creationTime desc
        filterString = createJsonFilter("SESSION_COUNT", "GREATER_THAN", "1", "creationTime", "desc");
        connectionsAsJsonString = serverControl.listConnections(filterString, 1, 50);
        connectionsAsJsonObject = JsonUtil.readJsonObject(connectionsAsJsonString);
        array = (JsonArray) connectionsAsJsonObject.get("data");
        Assert.assertEquals("number of connections returned from query", 3, array.size());
        Assert.assertEquals("connection3 creationTime Order", csf3.getConnection().getID(), array.getJsonObject(0).getString("connectionID"));
        Assert.assertEquals("connection2 creationTime Order", csf2.getConnection().getID(), array.getJsonObject(1).getString("connectionID"));
        Assert.assertEquals("connection1 creationTime Order", csf.getConnection().getID(), array.getJsonObject(2).getString("connectionID"));
    } finally {
        if (csf != null) {
            csf.close();
        }
        if (csf2 != null) {
            csf.close();
        }
        if (csf3 != null) {
            csf.close();
        }
    }
}
Also used : JsonArray(javax.json.JsonArray) ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JsonObject(javax.json.JsonObject) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Example 3 with ClientSessionFactoryImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl 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 4 with ClientSessionFactoryImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl in project activemq-artemis by apache.

the class NettyConnectorTest method testConnectionTimeoutConfig.

// make sure the 'connect-timeout' passed to netty.
@Test
public void testConnectionTimeoutConfig() throws Exception {
    final int timeout = 23456;
    TransportConfiguration transport = new TransportConfiguration(NETTY_CONNECTOR_FACTORY);
    transport.getParams().put(TransportConstants.NETTY_CONNECT_TIMEOUT, timeout);
    ServerLocator locator = ActiveMQClient.createServerLocatorWithoutHA(transport);
    ClientSessionFactoryImpl factory = (ClientSessionFactoryImpl) locator.createSessionFactory();
    NettyConnector connector = (NettyConnector) factory.getConnector();
    Bootstrap bootstrap = connector.getBootStrap();
    assertEquals(timeout, bootstrap.register().channel().config().getConnectTimeoutMillis());
    factory.close();
    locator.close();
}
Also used : ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) Bootstrap(io.netty.bootstrap.Bootstrap) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) NettyConnector(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 5 with ClientSessionFactoryImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl in project activemq-artemis by apache.

the class ConsumerStuckTest method testClientStuckTest.

@Test
public void testClientStuckTest() throws Exception {
    ServerLocator locator = createNettyNonHALocator().setConnectionTTL(1000).setClientFailureCheckPeriod(100).setConsumerWindowSize(10 * 1024 * 1024).setCallTimeout(1000);
    ClientSessionFactory sf = locator.createSessionFactory();
    ((ClientSessionFactoryImpl) sf).stopPingingAfterOne();
    RemotingConnectionImpl remotingConnection = (RemotingConnectionImpl) sf.getConnection();
    ClientSession session = sf.createSession(false, true, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 10000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        producer.send(message);
    }
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    final NettyConnection nettyConnection = (NettyConnection) remotingConnection.getTransportConnection();
    Thread tReceive = new Thread() {

        @Override
        public void run() {
            boolean first = true;
            try {
                while (!Thread.interrupted()) {
                    ClientMessage received = consumer.receive(500);
                    System.out.println("Received " + received);
                    if (first) {
                        first = false;
                        nettyConnection.getNettyChannel().config().setAutoRead(false);
                    }
                    if (received != null) {
                        received.acknowledge();
                    }
                }
            } catch (Throwable e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        }
    };
    tReceive.start();
    try {
        assertEquals(1, server.getSessions().size());
        System.out.println("sessions = " + server.getSessions().size());
        assertEquals(1, server.getConnectionCount());
        long timeout = System.currentTimeMillis() + 20000;
        long timeStart = System.currentTimeMillis();
        while (timeout > System.currentTimeMillis() && (server.getSessions().size() != 0 || server.getConnectionCount() != 0)) {
            Thread.sleep(10);
        }
        System.out.println("Time = " + System.currentTimeMillis() + " time diff = " + (System.currentTimeMillis() - timeStart) + ", connections Size = " + server.getConnectionCount() + " sessions = " + server.getSessions().size());
        if (server.getSessions().size() != 0) {
            System.out.println(threadDump("Thread dump"));
            fail("The cleanup wasn't able to finish cleaning the session. It's probably stuck, look at the thread dump generated by the test for more information");
        }
        System.out.println("Size = " + server.getConnectionCount());
        System.out.println("sessions = " + server.getSessions().size());
        if (server.getSessions().size() != 0) {
            System.out.println(threadDump("Thread dump"));
            fail("The cleanup wasn't able to finish cleaning the session. It's probably stuck, look at the thread dump generated by the test for more information");
        }
        assertEquals(0, server.getConnectionCount());
    } finally {
        nettyConnection.getNettyChannel().config().setAutoRead(true);
        tReceive.interrupt();
        tReceive.join();
    }
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) RemotingConnectionImpl(org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Aggregations

ClientSessionFactoryImpl (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl)6 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)5 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)5 Test (org.junit.Test)5 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)3 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)3 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)3 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 RemotingConnectionImpl (org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl)2 NettyConnection (org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 SessionFailureListener (org.apache.activemq.artemis.api.core.client.SessionFailureListener)1 ActiveMQServerControl (org.apache.activemq.artemis.api.core.management.ActiveMQServerControl)1 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)1 CloseListener (org.apache.activemq.artemis.core.remoting.CloseListener)1 NettyConnector (org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector)1