Search in sources :

Example 26 with IllegalStateException

use of javax.jms.IllegalStateException in project qpid-broker-j by apache.

the class ExceptionListenerTest method testExceptionListenerStopsConnection_ThrowsIllegalStateException.

@Test
public void testExceptionListenerStopsConnection_ThrowsIllegalStateException() throws Exception {
    assumeThat(getBrokerAdmin().supportsRestart(), is(equalTo(true)));
    final Connection connection = getConnection();
    try {
        final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1);
        final AtomicReference<JMSException> exceptionHolder = new AtomicReference<>();
        final AtomicReference<Throwable> unexpectedExceptionHolder = new AtomicReference<>();
        final ExceptionListener listener = exception -> {
            exceptionHolder.set(exception);
            try {
                connection.stop();
                fail("Exception not thrown");
            } catch (IllegalStateException ise) {
            // PASS
            } catch (Throwable t) {
                unexpectedExceptionHolder.set(t);
            } finally {
                exceptionReceivedLatch.countDown();
            }
        };
        connection.setExceptionListener(listener);
        getBrokerAdmin().restart();
        assertTrue("Exception was not propagated into exception listener in timely manner", exceptionReceivedLatch.await(getReceiveTimeout(), TimeUnit.MILLISECONDS));
        assertNotNull("Unexpected exception", exceptionHolder.get());
        assertNull("Connection#stop() should not have thrown exception", unexpectedExceptionHolder.get());
    } finally {
        connection.close();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Connection(javax.jms.Connection) Assume.assumeThat(org.junit.Assume.assumeThat) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IllegalStateException(javax.jms.IllegalStateException) AtomicReference(java.util.concurrent.atomic.AtomicReference) JMSException(javax.jms.JMSException) ExceptionListener(javax.jms.ExceptionListener) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) Assert.fail(org.junit.Assert.fail) JmsTestBase(org.apache.qpid.systests.JmsTestBase) IllegalStateException(javax.jms.IllegalStateException) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExceptionListener(javax.jms.ExceptionListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 27 with IllegalStateException

use of javax.jms.IllegalStateException in project qpid-broker-j by apache.

the class CommitRollbackTest method testCommitOnClosedSession.

@Test
public void testCommitOnClosedSession() throws Exception {
    Connection connection = getConnection();
    try {
        Session transactedSession = connection.createSession(true, Session.SESSION_TRANSACTED);
        transactedSession.close();
        try {
            transactedSession.commit();
            fail("Commit on closed session should throw IllegalStateException!");
        } catch (IllegalStateException e) {
        // passed
        }
    } finally {
        connection.close();
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

Example 28 with IllegalStateException

use of javax.jms.IllegalStateException in project qpid-broker-j by apache.

the class CommitRollbackTest method testGetTransactedOnClosedSession.

@Test
public void testGetTransactedOnClosedSession() throws Exception {
    Connection connection = getConnection();
    try {
        Session transactedSession = connection.createSession(true, Session.SESSION_TRANSACTED);
        transactedSession.close();
        try {
            transactedSession.getTransacted();
            fail("According to Sun TCK invocation of Session#getTransacted on closed session should throw IllegalStateException!");
        } catch (IllegalStateException e) {
        // passed
        }
    } finally {
        connection.close();
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

Example 29 with IllegalStateException

use of javax.jms.IllegalStateException in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method rollback.

/**
 * {@inheritDoc}
 */
@Override
public void rollback() throws JMSException {
    logger.trace("rollback transaction on session {}", this);
    illegalStateExceptionIfClosed();
    if (!this.transacted)
        throw new IllegalStateException("Session is not transacted");
    if (this.enterCommittingBlock()) {
        try {
            // rollback the RabbitMQ transaction which may cause some messages to become unacknowledged
            this.channel.txRollback();
            // requeue all unacknowledged messages (not automatically done by RabbitMQ)
            // requeue
            this.channel.basicRecover(true);
        } catch (IOException x) {
            this.logger.error("RabbitMQ exception on channel.txRollback() or channel.basicRecover(true) in session {}", this, x);
            throw new RMQJMSException(x);
        } finally {
            this.leaveCommittingBlock();
        }
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) IOException(java.io.IOException)

Example 30 with IllegalStateException

use of javax.jms.IllegalStateException in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method commit.

/**
 * {@inheritDoc}
 */
@Override
public void commit() throws JMSException {
    logger.trace("commit transaction on session {}", this);
    illegalStateExceptionIfClosed();
    if (!this.transacted)
        throw new IllegalStateException("Session is not transacted");
    if (this.enterCommittingBlock()) {
        try {
            // Call commit on the channel.
            // All messages ought already to have been acked.
            this.channel.txCommit();
        } catch (Exception x) {
            this.logger.error("RabbitMQ exception on channel.txCommit() in session {}", this, x);
            throw new RMQJMSException(x);
        } finally {
            this.leaveCommittingBlock();
        }
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) TimeoutException(java.util.concurrent.TimeoutException) RMQJMSSelectorException(com.rabbitmq.jms.util.RMQJMSSelectorException) IllegalStateException(javax.jms.IllegalStateException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) InvalidSelectorException(javax.jms.InvalidSelectorException)

Aggregations

IllegalStateException (javax.jms.IllegalStateException)44 Session (javax.jms.Session)19 Connection (javax.jms.Connection)12 QueueSession (javax.jms.QueueSession)12 Test (org.junit.Test)12 TopicSession (javax.jms.TopicSession)11 XAQueueSession (javax.jms.XAQueueSession)11 XATopicSession (javax.jms.XATopicSession)11 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)11 JMSException (javax.jms.JMSException)10 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 InvalidDestinationException (javax.jms.InvalidDestinationException)5 IOException (java.io.IOException)4 Queue (javax.jms.Queue)3 QueueBrowser (javax.jms.QueueBrowser)3 TopicSubscriber (javax.jms.TopicSubscriber)3 QueueQuery (org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery)3 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)2 TemporaryQueue (javax.jms.TemporaryQueue)2