Search in sources :

Example 1 with TemporaryQueue

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

the class JmsTemplateTests method doTestSendAndReceive.

private void doTestSendAndReceive(boolean explicitDestination, boolean useDefaultDestination, long timeout) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(this.queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    template.setReceiveTimeout(timeout);
    Session localSession = getLocalSession();
    TemporaryQueue replyDestination = mock(TemporaryQueue.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(localSession.createProducer(this.queue)).willReturn(messageProducer);
    given(localSession.createTemporaryQueue()).willReturn(replyDestination);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer);
    TextMessage request = mock(TextMessage.class);
    MessageCreator messageCreator = mock(MessageCreator.class);
    given(messageCreator.createMessage(localSession)).willReturn(request);
    TextMessage reply = mock(TextMessage.class);
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(reply);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(reply);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(reply);
    }
    Message message = null;
    if (useDefaultDestination) {
        message = template.sendAndReceive(messageCreator);
    } else if (explicitDestination) {
        message = template.sendAndReceive(this.queue, messageCreator);
    } else {
        message = template.sendAndReceive(destinationName, messageCreator);
    }
    // replyTO set on the request
    verify(request).setJMSReplyTo(replyDestination);
    assertThat(message).as("Reply message not received").isSameAs(reply);
    verify(this.connection).start();
    verify(this.connection).close();
    verify(localSession).close();
    verify(messageConsumer).close();
    verify(messageProducer).close();
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Message(jakarta.jms.Message) TextMessage(jakarta.jms.TextMessage) TemporaryQueue(jakarta.jms.TemporaryQueue) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session)

Example 2 with TemporaryQueue

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

the class JmsTemplate method doSendAndReceive.

/**
 * Send a request message to the given {@link Destination} and block until
 * a reply has been received on a temporary queue created on-the-fly.
 * <p>Return the response message or {@code null} if no message has
 * @throws JMSException if thrown by JMS API methods
 */
@Nullable
protected Message doSendAndReceive(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
    Assert.notNull(messageCreator, "MessageCreator must not be null");
    TemporaryQueue responseQueue = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        Message requestMessage = messageCreator.createMessage(session);
        responseQueue = session.createTemporaryQueue();
        producer = session.createProducer(destination);
        consumer = session.createConsumer(responseQueue);
        requestMessage.setJMSReplyTo(responseQueue);
        if (logger.isDebugEnabled()) {
            logger.debug("Sending created message: " + requestMessage);
        }
        doSend(producer, requestMessage);
        return receiveFromConsumer(consumer, getReceiveTimeout());
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeMessageProducer(producer);
        if (responseQueue != null) {
            responseQueue.delete();
        }
    }
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Message(jakarta.jms.Message) TemporaryQueue(jakarta.jms.TemporaryQueue) MessageProducer(jakarta.jms.MessageProducer) Nullable(org.springframework.lang.Nullable)

Aggregations

Message (jakarta.jms.Message)2 MessageConsumer (jakarta.jms.MessageConsumer)2 MessageProducer (jakarta.jms.MessageProducer)2 TemporaryQueue (jakarta.jms.TemporaryQueue)2 Session (jakarta.jms.Session)1 TextMessage (jakarta.jms.TextMessage)1 Nullable (org.springframework.lang.Nullable)1