Java Code Examples for javax.jms.Session#setMessageListener()

The following examples show how to use javax.jms.Session#setMessageListener() . 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 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 2
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 3
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 4
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 5
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);
    }
}