Search in sources :

Example 6 with MessageConsumer

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

the class SimpleMessageListenerContainer method createListenerConsumer.

/**
 * Create a MessageConsumer for the given JMS Session,
 * registering a MessageListener for the specified listener.
 * @param session the JMS Session to work on
 * @return the MessageConsumer
 * @throws JMSException if thrown by JMS methods
 * @see #executeListener
 */
protected MessageConsumer createListenerConsumer(final Session session) throws JMSException {
    Destination destination = getDestination();
    if (destination == null) {
        String destinationName = getDestinationName();
        Assert.state(destinationName != null, "No destination set");
        destination = resolveDestinationName(session, destinationName);
    }
    MessageConsumer consumer = createConsumer(session, destination);
    if (this.taskExecutor != null) {
        consumer.setMessageListener(message -> this.taskExecutor.execute(() -> processMessage(message, session)));
    } else {
        consumer.setMessageListener(message -> processMessage(message, session));
    }
    return consumer;
}
Also used : Destination(jakarta.jms.Destination) MessageConsumer(jakarta.jms.MessageConsumer)

Example 7 with MessageConsumer

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

the class SimpleMessageListenerContainer method initializeConsumers.

/**
 * Initialize the JMS Sessions and MessageConsumers for this container.
 * @throws JMSException in case of setup failure
 */
protected void initializeConsumers() throws JMSException {
    // Register Sessions and MessageConsumers.
    synchronized (this.consumersMonitor) {
        if (this.consumers == null) {
            this.sessions = new HashSet<>(this.concurrentConsumers);
            this.consumers = new HashSet<>(this.concurrentConsumers);
            Connection con = getSharedConnection();
            for (int i = 0; i < this.concurrentConsumers; i++) {
                Session session = createSession(con);
                MessageConsumer consumer = createListenerConsumer(session);
                this.sessions.add(session);
                this.consumers.add(consumer);
            }
        }
    }
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session)

Example 8 with MessageConsumer

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

the class JmsTemplate method doSendAndReceive.

/**
 * Send a request message to the given {@link Destination} and block until
 * a reply has been received on a temporary queue created on-the-fly.
 * <p>Return the response message or {@code null} if no message has
 * @throws JMSException if thrown by JMS API methods
 */
@Nullable
protected Message doSendAndReceive(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
    Assert.notNull(messageCreator, "MessageCreator must not be null");
    TemporaryQueue responseQueue = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        Message requestMessage = messageCreator.createMessage(session);
        responseQueue = session.createTemporaryQueue();
        producer = session.createProducer(destination);
        consumer = session.createConsumer(responseQueue);
        requestMessage.setJMSReplyTo(responseQueue);
        if (logger.isDebugEnabled()) {
            logger.debug("Sending created message: " + requestMessage);
        }
        doSend(producer, requestMessage);
        return receiveFromConsumer(consumer, getReceiveTimeout());
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeMessageProducer(producer);
        if (responseQueue != null) {
            responseQueue.delete();
        }
    }
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Message(jakarta.jms.Message) TemporaryQueue(jakarta.jms.TemporaryQueue) MessageProducer(jakarta.jms.MessageProducer) Nullable(org.springframework.lang.Nullable)

Example 9 with MessageConsumer

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

the class SimpleMessageListenerContainerTests method testDestroyClosesConsumersSessionsAndConnectionInThatOrder.

@Test
public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() 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();
    this.container.start();
    this.container.destroy();
    verify(messageConsumer).close();
    verify(session).close();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
    verify(connection).close();
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 10 with MessageConsumer

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

the class SimpleMessageListenerContainerTests method testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse.

@Test
public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() 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);
    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.setAutoStartup(false);
    this.container.afterPropertiesSet();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    context.close();
    verify(connection).setExceptionListener(this.container);
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) ConnectionFactory(jakarta.jms.ConnectionFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Aggregations

MessageConsumer (jakarta.jms.MessageConsumer)11 Session (jakarta.jms.Session)8 Connection (jakarta.jms.Connection)7 Message (jakarta.jms.Message)6 ConnectionFactory (jakarta.jms.ConnectionFactory)5 Test (org.junit.jupiter.api.Test)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 JMSException (jakarta.jms.JMSException)3 Nullable (org.springframework.lang.Nullable)3 ExceptionListener (jakarta.jms.ExceptionListener)2 MessageListener (jakarta.jms.MessageListener)2 MessageProducer (jakarta.jms.MessageProducer)2 TemporaryQueue (jakarta.jms.TemporaryQueue)2 TextMessage (jakarta.jms.TextMessage)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 Assertions.fail (org.assertj.core.api.Assertions.fail)2 BDDMockito.given (org.mockito.BDDMockito.given)2