Search in sources :

Example 66 with Session

use of jakarta.jms.Session 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 67 with Session

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

the class MessagingMessageListenerAdapterTests method replyPayloadToQueue.

@Test
public void replyPayloadToQueue() throws JMSException {
    Session session = mock(Session.class);
    Queue replyDestination = mock(Queue.class);
    given(session.createQueue("queueOut")).willReturn(replyDestination);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage("Response")).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToQueue", Message.class);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session).createQueue("queueOut");
    verify(session).createTextMessage("Response");
    verify(messageProducer).send(responseMessage);
    verify(messageProducer).close();
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 68 with Session

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

the class MessagingMessageListenerAdapterTests method replyWithFullQoS.

@Test
public void replyWithFullQoS() throws JMSException {
    Session session = mock(Session.class);
    Queue replyDestination = mock(Queue.class);
    given(session.createQueue("queueOut")).willReturn(replyDestination);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage("Response")).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToQueue", Message.class);
    QosSettings settings = new QosSettings(DeliveryMode.NON_PERSISTENT, 6, 6000);
    listener.setResponseQosSettings(settings);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session).createQueue("queueOut");
    verify(session).createTextMessage("Response");
    verify(messageProducer).send(responseMessage, DeliveryMode.NON_PERSISTENT, 6, 6000);
    verify(messageProducer).close();
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) QosSettings(org.springframework.jms.support.QosSettings) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 69 with Session

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

the class MessagingMessageListenerAdapterTests method incomingMessageUsesMessageConverter.

@Test
public void incomingMessageUsesMessageConverter() throws JMSException {
    jakarta.jms.Message jmsMessage = mock(jakarta.jms.Message.class);
    Session session = mock(Session.class);
    MessageConverter messageConverter = mock(MessageConverter.class);
    given(messageConverter.fromMessage(jmsMessage)).willReturn("FooBar");
    MessagingMessageListenerAdapter listener = getSimpleInstance("simple", Message.class);
    listener.setMessageConverter(messageConverter);
    listener.onMessage(jmsMessage, session);
    verify(messageConverter, times(1)).fromMessage(jmsMessage);
    assertThat(sample.simples.size()).isEqualTo(1);
    assertThat(sample.simples.get(0).getPayload()).isEqualTo("FooBar");
}
Also used : MappingJackson2MessageConverter(org.springframework.jms.support.converter.MappingJackson2MessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 70 with Session

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

the class MessagingMessageListenerAdapterTests method replyPayloadToDestination.

@Test
public void replyPayloadToDestination() throws JMSException {
    Session session = mock(Session.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage("Response")).willReturn(responseMessage);
    given(session.createProducer(sharedReplyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToDestination", Message.class);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session, times(0)).createQueue(anyString());
    verify(session).createTextMessage("Response");
    verify(messageProducer).send(responseMessage);
    verify(messageProducer).close();
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) MessageProducer(jakarta.jms.MessageProducer) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Session (jakarta.jms.Session)84 Test (org.junit.jupiter.api.Test)71 Connection (jakarta.jms.Connection)30 Message (jakarta.jms.Message)27 TextMessage (jakarta.jms.TextMessage)26 StubTextMessage (org.springframework.jms.StubTextMessage)22 ConnectionFactory (jakarta.jms.ConnectionFactory)21 JMSException (jakarta.jms.JMSException)20 MessageProducer (jakarta.jms.MessageProducer)19 Destination (jakarta.jms.Destination)18 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 Queue (jakarta.jms.Queue)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 BDDMockito.given (org.mockito.BDDMockito.given)12 Mockito.mock (org.mockito.Mockito.mock)12 Mockito.verify (org.mockito.Mockito.verify)12 StubQueue (org.springframework.jms.StubQueue)12 ObjectMessage (jakarta.jms.ObjectMessage)11 QueueSession (jakarta.jms.QueueSession)11 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)11