Search in sources :

Example 11 with MessageConsumer

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

the class JmsTemplateTests method doTestReceive.

private void doTestReceive(boolean explicitDestination, boolean useDefaultDestination, boolean testConverter, boolean clientAcknowledge, boolean messageSelector, boolean noLocal, 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);
        }
    }
    if (noLocal) {
        template.setPubSubNoLocal(true);
    }
    template.setReceiveTimeout(timeout);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    String selectorString = "selector";
    given(this.session.createConsumer(this.queue, messageSelector ? selectorString : null)).willReturn(messageConsumer);
    if (!useTransactedTemplate() && !useTransactedSession()) {
        given(this.session.getAcknowledgeMode()).willReturn(clientAcknowledge ? Session.CLIENT_ACKNOWLEDGE : Session.AUTO_ACKNOWLEDGE);
    }
    TextMessage textMessage = mock(TextMessage.class);
    if (testConverter) {
        given(textMessage.getText()).willReturn("Hello World!");
    }
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(textMessage);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(textMessage);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(textMessage);
    }
    Message message = null;
    String textFromMessage = null;
    if (useDefaultDestination) {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(selectorString) : template.receiveAndConvert());
        } else {
            message = (messageSelector ? template.receiveSelected(selectorString) : template.receive());
        }
    } else if (explicitDestination) {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(this.queue, selectorString) : template.receiveAndConvert(this.queue));
        } else {
            message = (messageSelector ? template.receiveSelected(this.queue, selectorString) : template.receive(this.queue));
        }
    } else {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) : template.receiveAndConvert(destinationName));
        } else {
            message = (messageSelector ? template.receiveSelected(destinationName, selectorString) : template.receive(destinationName));
        }
    }
    if (testConverter) {
        assertThat(textFromMessage).as("Message text should be equal").isEqualTo("Hello World!");
    } else {
        assertThat(textMessage).as("Messages should refer to the same object").isEqualTo(message);
    }
    verify(this.connection).start();
    verify(this.connection).close();
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    verify(this.session).close();
    if (!useTransactedSession() && clientAcknowledge) {
        verify(textMessage).acknowledge();
    }
    verify(messageConsumer).close();
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Message(jakarta.jms.Message) TextMessage(jakarta.jms.TextMessage) TextMessage(jakarta.jms.TextMessage)

Aggregations

MessageConsumer (jakarta.jms.MessageConsumer)11 Session (jakarta.jms.Session)8 Connection (jakarta.jms.Connection)7 Message (jakarta.jms.Message)6 ConnectionFactory (jakarta.jms.ConnectionFactory)5 Test (org.junit.jupiter.api.Test)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 JMSException (jakarta.jms.JMSException)3 Nullable (org.springframework.lang.Nullable)3 ExceptionListener (jakarta.jms.ExceptionListener)2 MessageListener (jakarta.jms.MessageListener)2 MessageProducer (jakarta.jms.MessageProducer)2 TemporaryQueue (jakarta.jms.TemporaryQueue)2 TextMessage (jakarta.jms.TextMessage)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 Assertions.fail (org.assertj.core.api.Assertions.fail)2 BDDMockito.given (org.mockito.BDDMockito.given)2