Search in sources :

Example 21 with Message

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException.

@Test
void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException() throws Exception {
    Queue destination = mock(Queue.class);
    final TextMessage sentTextMessage = mock(TextMessage.class);
    // correlation ID is queried when response is being created...
    given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
    // Reply-To is queried when response is being created...
    given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
    TextMessage responseTextMessage = mock(TextMessage.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    willThrow(new JMSException("Doe!")).given(messageProducer).send(responseTextMessage);
    final QueueSession session = mock(QueueSession.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    given(session.createProducer(destination)).willReturn(messageProducer);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() -> adapter.onMessage(sentTextMessage, session)).withCauseExactlyInstanceOf(JMSException.class);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(messageProducer).close();
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) JMSException(jakarta.jms.JMSException) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 22 with Message

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateWhenReturnTypeIsNotAJMSMessageAndNoMessageConverterIsSupplied.

@Test
void testWithResponsiveMessageDelegateWhenReturnTypeIsNotAJMSMessageAndNoMessageConverterIsSupplied() throws Exception {
    final TextMessage sentTextMessage = mock(TextMessage.class);
    final Session session = mock(Session.class);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    adapter.setMessageConverter(null);
    assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() -> adapter.onMessage(sentTextMessage, session)).withCauseExactlyInstanceOf(MessageConversionException.class);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 23 with Message

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

the class SimpleMessageListenerContainerTests method testTaskExecutorCorrectlyInvokedWhenSpecified.

@Test
public void testTaskExecutorCorrectlyInvokedWhenSpecified() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    final Session session = mock(Session.class);
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    given(session.getTransacted()).willReturn(false);
    given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE);
    Connection connection = mock(Connection.class);
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final TestMessageListener listener = new TestMessageListener();
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(listener);
    this.container.setTaskExecutor(task -> {
        listener.executorInvoked = true;
        assertThat(listener.listenerInvoked).isFalse();
        task.run();
        assertThat(listener.listenerInvoked).isTrue();
    });
    this.container.afterPropertiesSet();
    this.container.start();
    final Message message = mock(Message.class);
    messageConsumer.sendMessage(message);
    assertThat(listener.executorInvoked).isTrue();
    assertThat(listener.listenerInvoked).isTrue();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 24 with Message

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

the class SimpleMessageListenerContainerTests method testRegisteredErrorHandlerIsInvokedOnException.

@Test
public void testRegisteredErrorHandlerIsInvokedOnException() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    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);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(false);
    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);
    final IllegalStateException theException = new IllegalStateException("intentional test failure");
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener((SessionAwareMessageListener<Message>) (Message message, @Nullable Session session1) -> {
        throw theException;
    });
    ErrorHandler errorHandler = mock(ErrorHandler.class);
    this.container.setErrorHandler(errorHandler);
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
    verify(errorHandler).handleError(theException);
}
Also used : ErrorHandler(org.springframework.util.ErrorHandler) ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 25 with Message

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException.

@Test
void testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException() throws Exception {
    final TextMessage message = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    willThrow(new IllegalArgumentException("Doe!")).given(delegate).handleMessage(message);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() -> adapter.onMessage(message, session));
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Aggregations

Message (jakarta.jms.Message)30 Test (org.junit.jupiter.api.Test)23 TextMessage (jakarta.jms.TextMessage)19 Session (jakarta.jms.Session)18 BytesMessage (jakarta.jms.BytesMessage)13 ObjectMessage (jakarta.jms.ObjectMessage)13 ConnectionFactory (jakarta.jms.ConnectionFactory)9 Connection (jakarta.jms.Connection)8 QueueSession (jakarta.jms.QueueSession)7 MapMessage (jakarta.jms.MapMessage)6 MessageConsumer (jakarta.jms.MessageConsumer)6 MessageProducer (jakarta.jms.MessageProducer)6 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)6 JMSException (jakarta.jms.JMSException)5 Queue (jakarta.jms.Queue)4 Nullable (org.springframework.lang.Nullable)4 ExceptionListener (jakarta.jms.ExceptionListener)3 HashSet (java.util.HashSet)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 StubQueue (org.springframework.jms.StubQueue)3