Search in sources :

Example 41 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class JmsInvokerClientInterceptor method doExecuteRequest.

/**
	 * Actually execute the given request, sending the invoker request message
	 * to the specified target queue and waiting for a corresponding response.
	 * <p>The default implementation is based on standard JMS send/receive,
	 * using a {@link javax.jms.TemporaryQueue} for receiving the response.
	 * @param session the JMS Session to use
	 * @param queue the resolved target Queue to send to
	 * @param requestMessage the JMS Message to send
	 * @return the RemoteInvocationResult object
	 * @throws JMSException in case of JMS failure
	 */
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
    TemporaryQueue responseQueue = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        responseQueue = session.createTemporaryQueue();
        producer = session.createProducer(queue);
        consumer = session.createConsumer(responseQueue);
        requestMessage.setJMSReplyTo(responseQueue);
        producer.send(requestMessage);
        long timeout = getReceiveTimeout();
        return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeMessageProducer(producer);
        if (responseQueue != null) {
            responseQueue.delete();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer)

Example 42 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class SimpleMessageListenerContainer method doShutdown.

/**
	 * Destroy the registered JMS Sessions and associated MessageConsumers.
	 */
@Override
protected void doShutdown() throws JMSException {
    synchronized (this.consumersMonitor) {
        if (this.consumers != null) {
            logger.debug("Closing JMS MessageConsumers");
            for (MessageConsumer consumer : this.consumers) {
                JmsUtils.closeMessageConsumer(consumer);
            }
            logger.debug("Closing JMS Sessions");
            for (Session session : this.sessions) {
                JmsUtils.closeSession(session);
            }
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Session(javax.jms.Session)

Example 43 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class SimpleMessageListenerContainerTests method testContextRefreshedEventStartsTheConnectionByDefault.

@Test
public void testContextRefreshedEventStartsTheConnectionByDefault() throws Exception {
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new TestMessageListener());
    this.container.afterPropertiesSet();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ConnectionFactory(javax.jms.ConnectionFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

Example 44 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class JmsTemplateTests method doTestSendAndReceive.

private void doTestSendAndReceive(boolean explicitDestination, boolean useDefaultDestination, long timeout) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    template.setReceiveTimeout(timeout);
    Session localSession = getLocalSession();
    TemporaryQueue replyDestination = mock(TemporaryQueue.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(localSession.createProducer(queue)).willReturn(messageProducer);
    given(localSession.createTemporaryQueue()).willReturn(replyDestination);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer);
    TextMessage request = mock(TextMessage.class);
    MessageCreator messageCreator = mock(MessageCreator.class);
    given(messageCreator.createMessage(localSession)).willReturn(request);
    TextMessage reply = mock(TextMessage.class);
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(reply);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(reply);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(reply);
    }
    Message message = null;
    if (useDefaultDestination) {
        message = template.sendAndReceive(messageCreator);
    } else if (explicitDestination) {
        message = template.sendAndReceive(queue, messageCreator);
    } else {
        message = template.sendAndReceive(destinationName, messageCreator);
    }
    // replyTO set on the request
    verify(request).setJMSReplyTo(replyDestination);
    assertSame("Reply message not received", reply, message);
    verify(connection).start();
    verify(connection).close();
    verify(localSession).close();
    verify(messageConsumer).close();
    verify(messageProducer).close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 45 with MessageConsumer

use of javax.jms.MessageConsumer in project sling by apache.

the class JMSQueueManagerTest method emptyQueue.

private void emptyQueue(String name) throws JMSException {
    dumpQueue(name);
    Connection connection = amqConnectionFactoryService.getConnectionFactory().createConnection();
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(name);
    MessageConsumer consumer = session.createConsumer(queue);
    for (; ; ) {
        Message m = consumer.receive(100);
        if (m == null) {
            LOGGER.info("No more messages in queue {} ", name);
            break;
        }
        LOGGER.info("Got message  {}", m);
        m.acknowledge();
        session.commit();
    }
    boolean shouldFail = false;
    QueueBrowser browser = session.createBrowser(queue);
    for (Enumeration messages = browser.getEnumeration(); messages.hasMoreElements(); ) {
        Message m = (Message) messages.nextElement();
        LOGGER.info("Queued message {} ", m);
        shouldFail = true;
    }
    browser.close();
    if (shouldFail) {
        fail("Queue was not emptied as expected");
    }
    consumer.close();
    session.close();
    connection.stop();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) Message(javax.jms.Message) Connection(javax.jms.Connection) Queue(javax.jms.Queue) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3