Search in sources :

Example 6 with MessageProducer

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied.

@Test
void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
    Queue destination = mock(Queue.class);
    TextMessage sentTextMessage = mock(TextMessage.class);
    // correlation ID is queried when response is being created...
    given(sentTextMessage.getJMSCorrelationID()).willReturn(null);
    given(sentTextMessage.getJMSMessageID()).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);
    Session session = mock(Session.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);
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    adapter.onMessage(sentTextMessage, session);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(messageProducer).send(responseTextMessage);
    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) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 7 with MessageProducer

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

the class MessagingMessageListenerAdapterTests method replyPayloadNoDestination.

@Test
public void replyPayloadNoDestination() 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("Response")).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadNoDestination", Message.class);
    listener.setDefaultResponseDestination(replyDestination);
    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) Queue(jakarta.jms.Queue) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 8 with MessageProducer

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

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

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

the class JmsTransactionManagerTests method testTransactionCommitWithMessageProducer.

@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
    Destination dest = new StubQueue();
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Session session = mock(Session.class);
    MessageProducer producer = mock(MessageProducer.class);
    final Message message = mock(Message.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    given(session.createProducer(dest)).willReturn(producer);
    given(session.getTransacted()).willReturn(true);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.send(dest, sess -> message);
    tm.commit(ts);
    verify(producer).send(message);
    verify(session).commit();
    verify(producer).close();
    verify(session).close();
    verify(con).close();
}
Also used : Destination(jakarta.jms.Destination) ConnectionFactory(jakarta.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) StubQueue(org.springframework.jms.StubQueue) MessageProducer(jakarta.jms.MessageProducer) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

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