Search in sources :

Example 16 with MessageProducer

use of jakarta.jms.MessageProducer 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)

Example 17 with MessageProducer

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

the class MessagingMessageListenerAdapterTests method replyPayloadToTopic.

@Test
public void replyPayloadToTopic() throws JMSException {
    Session session = mock(Session.class);
    Topic replyDestination = mock(Topic.class);
    given(session.createTopic("topicOut")).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", "replyPayloadToTopic", Message.class);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session).createTopic("topicOut");
    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) Topic(jakarta.jms.Topic) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 18 with MessageProducer

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

the class JmsTemplateTests method testConverter.

@Test
void testConverter() throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    template.setMessageConverter(new SimpleMessageConverter());
    String s = "Hello world";
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    given(this.session.createProducer(this.queue)).willReturn(messageProducer);
    given(this.session.createTextMessage("Hello world")).willReturn(textMessage);
    template.convertAndSend(this.queue, s);
    verify(messageProducer).send(textMessage);
    verify(messageProducer).close();
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    verify(this.session).close();
    verify(this.connection).close();
}
Also used : SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 19 with MessageProducer

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

the class JmsTemplateTests method doTestJmsException.

protected void doTestJmsException(JMSException original, Class<? extends JmsException> thrownExceptionClass) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    template.setMessageConverter(new SimpleMessageConverter());
    String s = "Hello world";
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    reset(this.session);
    given(this.session.createProducer(this.queue)).willReturn(messageProducer);
    given(this.session.createTextMessage("Hello world")).willReturn(textMessage);
    willThrow(original).given(messageProducer).send(textMessage);
    assertThatExceptionOfType(thrownExceptionClass).isThrownBy(() -> template.convertAndSend(this.queue, s)).withCause(original);
    verify(messageProducer).close();
    verify(this.session).close();
    verify(this.connection).close();
}
Also used : SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage)

Aggregations

MessageProducer (jakarta.jms.MessageProducer)19 TextMessage (jakarta.jms.TextMessage)15 Session (jakarta.jms.Session)13 Test (org.junit.jupiter.api.Test)13 Queue (jakarta.jms.Queue)10 Message (jakarta.jms.Message)9 StubTextMessage (org.springframework.jms.StubTextMessage)7 Destination (jakarta.jms.Destination)5 MessageConsumer (jakarta.jms.MessageConsumer)5 TemporaryQueue (jakarta.jms.TemporaryQueue)5 QosSettings (org.springframework.jms.support.QosSettings)5 Connection (jakarta.jms.Connection)4 ConnectionFactory (jakarta.jms.ConnectionFactory)4 JMSException (jakarta.jms.JMSException)4 Message (org.springframework.messaging.Message)4 DeliveryMode (jakarta.jms.DeliveryMode)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 List (java.util.List)3 Context (javax.naming.Context)3