Java Code Examples for javax.jms.ConnectionConsumer#close()

The following examples show how to use javax.jms.ConnectionConsumer#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testCreateConnectionConsumer() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JmsServerSessionPool sessionPool = new JmsServerSessionPool();
        Connection connection = testFixture.establishConnecton(testPeer);

        // No additional Begin calls as there's no Session created for a Connection Consumer
        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlow();

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        testPeer.expectDetach(true, true, true);
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 2
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testOnExceptionFiredOnSessionPoolFailure() throws Exception {
    final CountDownLatch exceptionFired = new CountDownLatch(1);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException exception) {
                exceptionFired.countDown();
            }
        });

        connection.start();

        JmsFailingServerSessionPool sessionPool = new JmsFailingServerSessionPool();

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        assertTrue("Exception should have been fired", exceptionFired.await(5, TimeUnit.SECONDS));

        testPeer.expectDetach(true, true, true);
        testPeer.expectDispositionThatIsReleasedAndSettled();
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 3
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testOnExceptionFiredOnServerSessionFailure() throws Exception {
    final CountDownLatch exceptionFired = new CountDownLatch(1);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException exception) {
                exceptionFired.countDown();
            }
        });

        connection.start();

        JmsServerSessionPool sessionPool = new JmsServerSessionPool(new JmsFailingServerSession());

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        assertTrue("Exception should have been fired", exceptionFired.await(5, TimeUnit.SECONDS));

        testPeer.expectDetach(true, true, true);
        testPeer.expectDispositionThatIsReleasedAndSettled();
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 4
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private void doTestConnectionConsumerDispatchesToSession(boolean startBeforeCreate) throws Exception {
    final CountDownLatch messageArrived = new CountDownLatch(1);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);

        if (startBeforeCreate) {
            connection.start();
        }

        testPeer.expectBegin();

        // Create a session for our ServerSessionPool to use
        Session session = connection.createSession();
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                messageArrived.countDown();
            }
        });
        JmsServerSession serverSession = new JmsServerSession(session);
        JmsServerSessionPool sessionPool = new JmsServerSessionPool(serverSession);

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);
        testPeer.expectDispositionThatIsAcceptedAndSettled();

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        if (!startBeforeCreate) {
            connection.start();
        }

        assertTrue("Message didn't arrive in time", messageArrived.await(10, TimeUnit.SECONDS));

        testPeer.expectDetach(true, true, true);
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 5
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testPauseInOnMessageAndConsumerClosed() throws Exception {
    final CountDownLatch messageArrived = new CountDownLatch(1);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);

        testPeer.expectBegin();

        // Create a session for our ServerSessionPool to use
        Session session = connection.createSession();
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                messageArrived.countDown();

                LOG.trace("Pausing onMessage to check for race on connection consumer close");

                // Pause a bit to see if we race consumer close and our own
                // message accept attempt by the delivering Session.
                try {
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                }

                LOG.trace("Paused onMessage to check for race on connection consumer close");
            }
        });
        JmsServerSession serverSession = new JmsServerSession(session);
        JmsServerSessionPool sessionPool = new JmsServerSessionPool(serverSession);

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);
        testPeer.expectDispositionThatIsAcceptedAndSettled();

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        connection.start();

        assertTrue("Message didn't arrive in time", messageArrived.await(10, TimeUnit.SECONDS));

        testPeer.expectDetach(true, true, true);
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 6
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testNonStartedConnectionConsumerDoesNotDispatch() throws Exception {
    final CountDownLatch messageArrived = new CountDownLatch(1);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);

        testPeer.expectBegin();

        // Create a session for our ServerSessionPool to use
        Session session = connection.createSession();
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                messageArrived.countDown();
            }
        });
        JmsServerSession serverSession = new JmsServerSession(session);
        JmsServerSessionPool sessionPool = new JmsServerSessionPool(serverSession);

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        assertFalse("Message Arrived unexpectedly", messageArrived.await(500, TimeUnit.MILLISECONDS));

        testPeer.expectDetach(true, true, true);
        testPeer.expectDispositionThatIsReleasedAndSettled();
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 7
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testQueuedMessagesAreDrainedToServerSession() throws Exception {
    final int MESSAGE_COUNT = 10;
    final CountDownLatch messagesDispatched = new CountDownLatch(MESSAGE_COUNT);
    final CountDownLatch messagesArrived = new CountDownLatch(MESSAGE_COUNT);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JmsConnection connection = (JmsConnection) testFixture.establishConnecton(testPeer);
        connection.addConnectionListener(new JmsDefaultConnectionListener() {

            @Override
            public void onInboundMessage(JmsInboundMessageDispatch envelope) {
                messagesDispatched.countDown();
            }
        });

        testPeer.expectBegin();

        // Create a session for our ServerSessionPool to use
        Session session = connection.createSession();
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                messagesArrived.countDown();
            }
        });

        JmsServerSession serverSession = new JmsServerSession(session);
        JmsServerSessionPool sessionPool = new JmsServerSessionPool(serverSession);

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent, MESSAGE_COUNT);

        for (int i = 0; i < MESSAGE_COUNT; i++) {
            testPeer.expectDispositionThatIsAcceptedAndSettled();
        }

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        assertTrue("Message didn't arrive in time", messagesDispatched.await(10, TimeUnit.SECONDS));
        assertEquals(MESSAGE_COUNT, messagesArrived.getCount());

        connection.start();

        assertTrue("Message didn't arrive in time", messagesArrived.await(10, TimeUnit.SECONDS));

        testPeer.expectDetach(true, true, true);
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 8
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testConsumerRecoversAfterSessionPoolReturnsNullSession() throws Exception {
    final int MESSAGE_COUNT = 10;
    final CountDownLatch messagesDispatched = new CountDownLatch(MESSAGE_COUNT);
    final CountDownLatch messagesArrived = new CountDownLatch(MESSAGE_COUNT);

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JmsConnection connection = (JmsConnection) testFixture.establishConnecton(testPeer);
        connection.addConnectionListener(new JmsDefaultConnectionListener() {

            @Override
            public void onInboundMessage(JmsInboundMessageDispatch envelope) {
                messagesDispatched.countDown();
            }
        });

        testPeer.expectBegin();

        // Create a session for our ServerSessionPool to use
        Session session = connection.createSession();
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                messagesArrived.countDown();
            }
        });

        JmsServerSession serverSession = new JmsServerSession(session);
        JmsServerSessionPoolFirstAttemptGetsNull sessionPool = new JmsServerSessionPoolFirstAttemptGetsNull(serverSession);

        // Now the Connection consumer arrives and we give it a message
        // to be dispatched to the server session.
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent, MESSAGE_COUNT);

        for (int i = 0; i < MESSAGE_COUNT; i++) {
            testPeer.expectDispositionThatIsAcceptedAndSettled();
        }

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        assertTrue("Message didn't arrive in time", messagesDispatched.await(10, TimeUnit.SECONDS));
        assertEquals(MESSAGE_COUNT, messagesArrived.getCount());

        connection.start();

        assertTrue("Message didn't arrive in time", messagesArrived.await(10, TimeUnit.SECONDS));

        testPeer.expectDetach(true, true, true);
        consumer.close();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 9
Source File: ConnectionConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testRemotelyCloseConnectionConsumer() throws Exception {
    final String BREAD_CRUMB = "ErrorMessage";

    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        final CountDownLatch connectionError = new CountDownLatch(1);
        JmsServerSessionPool sessionPool = new JmsServerSessionPool();
        JmsConnection connection = (JmsConnection) testFixture.establishConnecton(testPeer);
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException exception) {
                connectionError.countDown();
            }
        });

        // Create a consumer, then remotely end it afterwards.
        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlow();
        testPeer.remotelyDetachLastOpenedLinkOnLastOpenedSession(true, true, AmqpError.RESOURCE_DELETED, BREAD_CRUMB);

        Queue queue = new JmsQueue("myQueue");
        ConnectionConsumer consumer = connection.createConnectionConsumer(queue, null, sessionPool, 100);

        // Verify the consumer gets marked closed
        testPeer.waitForAllHandlersToComplete(1000);
        assertTrue("consumer never closed.", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisfied() throws Exception {
                try {
                    consumer.getServerSessionPool();
                } catch (IllegalStateException jmsise) {
                    LOG.debug("Error reported from consumer.getServerSessionPool()", jmsise);
                    if (jmsise.getCause() != null) {
                        String message = jmsise.getCause().getMessage();
                        return message.contains(AmqpError.RESOURCE_DELETED.toString()) &&
                               message.contains(BREAD_CRUMB);
                    } else {
                        return false;
                    }
                }
                return false;
            }
        }, 10000, 10));

        assertTrue("Consumer closed callback didn't trigger", connectionError.await(5, TimeUnit.SECONDS));

        // Try closing it explicitly, should effectively no-op in client.
        // The test peer will throw during close if it sends anything.
        consumer.close();

        testPeer.expectClose();
        connection.close();
    }
}