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();
}
}
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();
}
}
}
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();
}
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();
}
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();
}
}
Aggregations