Search in sources :

Example 16 with TextMessage

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

the class MessagingMessageListenerAdapterTests method replyUsesMessageConverterForPayload.

@Test
public void replyUsesMessageConverterForPayload() throws JMSException {
    Session session = mock(Session.class);
    MessageConverter messageConverter = mock(MessageConverter.class);
    given(messageConverter.toMessage("Response", session)).willReturn(new StubTextMessage("Response"));
    Message<String> result = MessageBuilder.withPayload("Response").build();
    MessagingMessageListenerAdapter listener = getSimpleInstance("echo", Message.class);
    listener.setMessageConverter(messageConverter);
    jakarta.jms.Message replyMessage = listener.buildMessage(session, result);
    verify(messageConverter, times(1)).toMessage("Response", session);
    assertThat(replyMessage).as("reply should never be null").isNotNull();
    assertThat(((TextMessage) replyMessage).getText()).isEqualTo("Response");
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MappingJackson2MessageConverter(org.springframework.jms.support.converter.MappingJackson2MessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 17 with TextMessage

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

the class MessagingMessageListenerAdapterTests method testReplyWithJackson.

public TextMessage testReplyWithJackson(String methodName, String replyContent) throws JMSException {
    Queue replyDestination = mock(Queue.class);
    Session session = mock(Session.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage(replyContent)).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", methodName, Message.class);
    MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
    messageConverter.setTargetType(MessageType.TEXT);
    listener.setMessageConverter(messageConverter);
    listener.setDefaultResponseDestination(replyDestination);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session, times(0)).createQueue(anyString());
    verify(session).createTextMessage(replyContent);
    verify(messageProducer).send(responseMessage);
    verify(messageProducer).close();
    return responseMessage;
}
Also used : MappingJackson2MessageConverter(org.springframework.jms.support.converter.MappingJackson2MessageConverter) 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)

Example 18 with TextMessage

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

the class MessagingMessageListenerAdapterTests method replyWithCustomTimeToLive.

@Test
public void replyWithCustomTimeToLive() 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();
    settings.setTimeToLive(6000);
    listener.setResponseQosSettings(settings);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session).createQueue("queueOut");
    verify(session).createTextMessage("Response");
    verify(messageProducer).send(responseMessage, jakarta.jms.Message.DEFAULT_DELIVERY_MODE, jakarta.jms.Message.DEFAULT_PRIORITY, 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 19 with TextMessage

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

the class MappingJackson2MessageConverterTests method toTextMessageWithObject.

@Test
void toTextMessageWithObject() throws Exception {
    converter.setTargetType(MessageType.TEXT);
    TextMessage textMessageMock = mock(TextMessage.class);
    Date toBeMarshalled = new Date();
    given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
    converter.toMessage(toBeMarshalled, sessionMock);
    verify(textMessageMock).setStringProperty("__typeid__", Date.class.getName());
}
Also used : TextMessage(jakarta.jms.TextMessage) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 20 with TextMessage

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

the class MappingJackson2MessageConverterTests method fromTextMessageAsObject.

@Test
void fromTextMessageAsObject() throws Exception {
    TextMessage textMessageMock = mock(TextMessage.class);
    Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
    String text = "{\"foo\":\"bar\"}";
    given(textMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
    given(textMessageMock.getText()).willReturn(text);
    Object result = converter.fromMessage(textMessageMock);
    assertThat(unmarshalled).as("Invalid result").isEqualTo(result);
}
Also used : TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

TextMessage (jakarta.jms.TextMessage)54 Test (org.junit.jupiter.api.Test)47 StubTextMessage (org.springframework.jms.StubTextMessage)22 Session (jakarta.jms.Session)20 MessageProducer (jakarta.jms.MessageProducer)13 Message (jakarta.jms.Message)12 Queue (jakarta.jms.Queue)10 ObjectMessage (jakarta.jms.ObjectMessage)9 Message (org.springframework.messaging.Message)9 BytesMessage (jakarta.jms.BytesMessage)8 Destination (jakarta.jms.Destination)7 QueueSession (jakarta.jms.QueueSession)7 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)5 JMSException (jakarta.jms.JMSException)4 QueueSender (jakarta.jms.QueueSender)4 MessageConsumer (jakarta.jms.MessageConsumer)3 HashMap (java.util.HashMap)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 QosSettings (org.springframework.jms.support.QosSettings)3 ConnectionFactory (jakarta.jms.ConnectionFactory)2