use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class CloseDestroyedConnectionTest method testCloseDestroyedConnection.
/*
* Closing a connection that is destroyed should cleanly close everything without throwing exceptions
*/
@Test
public void testCloseDestroyedConnection() throws Exception {
long connectionTTL = 500;
cf.setClientFailureCheckPeriod(connectionTTL / 2);
// Need to set connection ttl to a low figure so connections get removed quickly on the server
cf.setConnectionTTL(connectionTTL);
conn = cf.createConnection();
Assert.assertEquals(1, server.getRemotingService().getConnections().size());
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Give time for the initial ping to reach the server before we fail (it has connection TTL in it)
Thread.sleep(500);
String queueName = "myqueue";
Queue queue = ActiveMQJMSClient.createQueue(queueName);
super.createQueue(queueName);
sess.createConsumer(queue);
sess.createProducer(queue);
sess.createBrowser(queue);
// Now fail the underlying connection
ClientSessionInternal sessi = (ClientSessionInternal) ((ActiveMQSession) sess).getCoreSession();
RemotingConnection rc = sessi.getConnection();
rc.fail(new ActiveMQInternalErrorException());
// Now close the connection
conn.close();
long start = System.currentTimeMillis();
while (true) {
int cons = server.getRemotingService().getConnections().size();
if (cons == 0) {
break;
}
long now = System.currentTimeMillis();
if (now - start > 10000) {
throw new Exception("Timed out waiting for connections to close");
}
Thread.sleep(50);
}
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class SessionClosedOnRemotingConnectionFailureTest method testSessionClosedOnRemotingConnectionFailure.
@Test
public void testSessionClosedOnRemotingConnectionFailure() throws Exception {
ClientSession session = addClientSession(sf.createSession());
session.createQueue("fooaddress", RoutingType.ANYCAST, "fooqueue");
ClientProducer prod = session.createProducer("fooaddress");
ClientConsumer cons = session.createConsumer("fooqueue");
session.start();
prod.send(session.createMessage(false));
Assert.assertNotNull(cons.receive());
// Now fail the underlying connection
RemotingConnection connection = ((ClientSessionInternal) session).getConnection();
connection.fail(new ActiveMQNotConnectedException());
Assert.assertTrue(session.isClosed());
Assert.assertTrue(prod.isClosed());
Assert.assertTrue(cons.isClosed());
try {
prod.send(session.createMessage(false));
Assert.fail("Should throw exception");
} catch (ActiveMQObjectClosedException oce) {
// ok
} catch (ActiveMQException e) {
fail("Invalid Exception type:" + e.getType());
}
try {
cons.receive();
Assert.fail("Should throw exception");
} catch (ActiveMQObjectClosedException oce) {
// ok
} catch (ActiveMQException e) {
fail("Invalid Exception type:" + e.getType());
}
session.close();
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class ReplicatedDistributionTest method fail.
/**
* @param session
* @throws InterruptedException
*/
private void fail(final ClientSession session) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
session.addFailureListener(new CountDownSessionFailureListener(latch, session));
RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
// Simulate failure on connection
conn.fail(new ActiveMQNotConnectedException());
// Wait to be informed of failure
boolean ok = latch.await(1000, TimeUnit.MILLISECONDS);
Assert.assertTrue(ok);
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class XATest method testMultipleSessionsOneTxRollbackAcknowledge.
@Test
public void testMultipleSessionsOneTxRollbackAcknowledge() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
// First send 2 messages
conn2 = cf.createConnection();
Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessProducer.createProducer(queue1);
Message m = sessProducer.createTextMessage("jellyfish1");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish2");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish3");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish4");
prod.send(m);
conn = xacf.createXAConnection();
conn.start();
tm.begin();
// Create 2 sessions and enlist them
XASession sess1 = conn.createXASession();
ClientSessionInternal res1 = (ClientSessionInternal) sess1.getXAResource();
XASession sess2 = conn.createXASession();
ClientSessionInternal res2 = (ClientSessionInternal) sess2.getXAResource();
res1.setForceNotSameRM(true);
res2.setForceNotSameRM(true);
Transaction tx = tm.getTransaction();
tx.enlistResource(res1);
tx.enlistResource(res2);
// Receive the messages, two on each consumer
MessageConsumer cons1 = sess1.createConsumer(queue1);
TextMessage r1 = (TextMessage) cons1.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("jellyfish1", r1.getText());
r1 = (TextMessage) cons1.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("jellyfish2", r1.getText());
cons1.close();
// Cancel is asynch
Thread.sleep(500);
MessageConsumer cons2 = sess2.createConsumer(queue1);
TextMessage r2 = (TextMessage) cons2.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("jellyfish3", r2.getText());
r2 = (TextMessage) cons2.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("jellyfish4", r2.getText());
// rollback
cons2.close();
tx.delistResource(res1, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.rollback();
// Rollback causes cancel which is asynch
Thread.sleep(1000);
// We cannot assume anything about the order in which the transaction manager rollsback
// the sessions - this is implementation dependent
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sess.createConsumer(queue1);
conn2.start();
TextMessage r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
boolean session1First = false;
if (r.getText().equals("jellyfish1")) {
session1First = true;
} else if (r.getText().equals("jellyfish3")) {
session1First = false;
} else {
Assert.fail("Unexpected message");
}
if (session1First) {
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish2", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish3", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish4", r.getText());
} else {
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish4", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish1", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish2", r.getText());
}
r = (TextMessage) cons.receive(100);
Assert.assertNull(r);
} finally {
if (conn != null) {
conn.close();
}
if (conn2 != null) {
conn2.close();
}
}
}
use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.
the class XATest method testMultipleSessionsOneTxCommitAcknowledge.
@Test
public void testMultipleSessionsOneTxCommitAcknowledge() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
// First send 2 messages
conn2 = cf.createConnection();
Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessProducer.createProducer(queue1);
Message m = sessProducer.createTextMessage("jellyfish1");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish2");
prod.send(m);
conn = xacf.createXAConnection();
conn.start();
tm.begin();
// Create 2 sessions and enlist them
XASession sess1 = conn.createXASession();
ClientSessionInternal res1 = (ClientSessionInternal) sess1.getXAResource();
XASession sess2 = conn.createXASession();
ClientSessionInternal res2 = (ClientSessionInternal) sess2.getXAResource();
res1.setForceNotSameRM(true);
res2.setForceNotSameRM(true);
Transaction tx = tm.getTransaction();
tx.enlistResource(res1);
tx.enlistResource(res2);
// Receive the messages, one on each consumer
MessageConsumer cons1 = sess1.createConsumer(queue1);
TextMessage r1 = (TextMessage) cons1.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("jellyfish1", r1.getText());
cons1.close();
MessageConsumer cons2 = sess2.createConsumer(queue1);
TextMessage r2 = (TextMessage) cons2.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("jellyfish2", r2.getText());
tx.delistResource(res1, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
// commit
tm.commit();
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sess.createConsumer(queue1);
conn2.start();
TextMessage r3 = (TextMessage) cons.receive(100);
Assert.assertNull(r3);
} finally {
if (conn != null) {
conn.close();
}
if (conn2 != null) {
conn2.close();
}
}
}
Aggregations