Search in sources :

Example 16 with ClientSessionInternal

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);
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) Queue(javax.jms.Queue) JMSException(javax.jms.JMSException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 17 with ClientSessionInternal

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();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 18 with ClientSessionInternal

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);
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)

Example 19 with ClientSessionInternal

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();
        }
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Transaction(javax.transaction.Transaction) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 20 with ClientSessionInternal

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();
        }
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Transaction(javax.transaction.Transaction) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)46 Test (org.junit.Test)35 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)21 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)20 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)17 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)13 Connection (javax.jms.Connection)12 Session (javax.jms.Session)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)11 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)10 MessageConsumer (javax.jms.MessageConsumer)8 MessageProducer (javax.jms.MessageProducer)8 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 TextMessage (javax.jms.TextMessage)7 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)7 XAConnection (javax.jms.XAConnection)5 XASession (javax.jms.XASession)5