Search in sources :

Example 1 with TextMessage

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

the class ArtemisAutoConfigurationTests method embeddedWithPersistentMode.

@Test
void embeddedWithPersistentMode(@TempDir Path temp) throws IOException {
    File dataDirectory = Files.createTempDirectory(temp, null).toFile();
    final String messageId = UUID.randomUUID().toString();
    // Start the server and post a message to some queue
    this.contextRunner.withUserConfiguration(EmptyConfiguration.class).withPropertyValues("spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataDirectory.getAbsolutePath()).run((context) -> context.getBean(JmsTemplate.class).send("TestQueue", (session) -> session.createTextMessage(messageId))).run((context) -> {
        // Start the server again and check if our message is still here
        JmsTemplate jmsTemplate2 = context.getBean(JmsTemplate.class);
        jmsTemplate2.setReceiveTimeout(1000L);
        Message message = jmsTemplate2.receive("TestQueue");
        assertThat(message).isNotNull();
        assertThat(((TextMessage) message).getText()).isEqualTo(messageId);
    });
}
Also used : JMSConfigurationImpl(org.apache.activemq.artemis.jms.server.config.impl.JMSConfigurationImpl) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) JMSQueueConfiguration(org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration) TopicConfiguration(org.apache.activemq.artemis.jms.server.config.TopicConfiguration) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) TopicConfigurationImpl(org.apache.activemq.artemis.jms.server.config.impl.TopicConfigurationImpl) Path(java.nio.file.Path) BindingQueryResult(org.apache.activemq.artemis.core.server.BindingQueryResult) Message(jakarta.jms.Message) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) Files(java.nio.file.Files) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) JMSConfiguration(org.apache.activemq.artemis.jms.server.config.JMSConfiguration) IOException(java.io.IOException) JmsAutoConfiguration(org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration) UUID(java.util.UUID) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) NettyConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory) JMSQueueConfigurationImpl(org.apache.activemq.artemis.jms.server.config.impl.JMSQueueConfigurationImpl) AssertableApplicationContext(org.springframework.boot.test.context.assertj.AssertableApplicationContext) TempDir(org.junit.jupiter.api.io.TempDir) TextMessage(jakarta.jms.TextMessage) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) Bean(org.springframework.context.annotation.Bean) EmbeddedActiveMQ(org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ) Message(jakarta.jms.Message) TextMessage(jakarta.jms.TextMessage) JmsTemplate(org.springframework.jms.core.JmsTemplate) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) File(java.io.File) TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 2 with TextMessage

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

the class JmsMessagingTemplateTests method convertAndSendDefaultDestinationName.

@Test
public void convertAndSendDefaultDestinationName() throws JMSException {
    this.messagingTemplate.setDefaultDestinationName("myQueue");
    this.messagingTemplate.convertAndSend("my Payload");
    verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
    TextMessage textMessage = createTextMessage(this.messageCreator.getValue());
    assertThat(textMessage.getText()).isEqualTo("my Payload");
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 3 with TextMessage

use of jakarta.jms.TextMessage 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 4 with TextMessage

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

the class JmsTemplateTests method doTestSendDestination.

/**
 * Common method for testing a send method that uses the MessageCreator
 * callback but with different QOS options.
 * @param ignoreQOS test using default QOS options.
 */
private void doTestSendDestination(boolean explicitDestination, boolean useDefaultDestination, boolean ignoreQOS, boolean disableIdAndTimestamp) 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 (disableIdAndTimestamp) {
        template.setMessageIdEnabled(false);
        template.setMessageTimestampEnabled(false);
    }
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    given(this.session.createProducer(this.queue)).willReturn(messageProducer);
    given(this.session.createTextMessage("just testing")).willReturn(textMessage);
    if (!ignoreQOS) {
        template.setQosSettings(this.qosSettings);
    }
    if (useDefaultDestination) {
        template.send(session -> session.createTextMessage("just testing"));
    } else {
        if (explicitDestination) {
            template.send(this.queue, (MessageCreator) session -> session.createTextMessage("just testing"));
        } else {
            template.send(destinationName, (MessageCreator) session -> session.createTextMessage("just testing"));
        }
    }
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    if (disableIdAndTimestamp) {
        verify(messageProducer).setDisableMessageID(true);
        verify(messageProducer).setDisableMessageTimestamp(true);
    }
    if (ignoreQOS) {
        verify(messageProducer).send(textMessage);
    } else {
        verify(messageProducer).send(textMessage, this.qosSettings.getDeliveryMode(), this.qosSettings.getPriority(), this.qosSettings.getTimeToLive());
    }
    verify(messageProducer).close();
    verify(this.session).close();
    verify(this.connection).close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UncategorizedJmsException(org.springframework.jms.UncategorizedJmsException) JMSException(jakarta.jms.JMSException) TemporaryQueue(jakarta.jms.TemporaryQueue) Queue(jakarta.jms.Queue) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) ResourceAllocationException(org.springframework.jms.ResourceAllocationException) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MessageFormatException(org.springframework.jms.MessageFormatException) Context(javax.naming.Context) ConnectionFactory(jakarta.jms.ConnectionFactory) MessageEOFException(org.springframework.jms.MessageEOFException) Connection(jakarta.jms.Connection) QosSettings(org.springframework.jms.support.QosSettings) InvalidClientIDException(org.springframework.jms.InvalidClientIDException) TransactionAwareConnectionFactoryProxy(org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy) PrintWriter(java.io.PrintWriter) Message(jakarta.jms.Message) JmsException(org.springframework.jms.JmsException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) StringWriter(java.io.StringWriter) JndiDestinationResolver(org.springframework.jms.support.destination.JndiDestinationResolver) JndiTemplate(org.springframework.jndi.JndiTemplate) TransactionInProgressException(org.springframework.jms.TransactionInProgressException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) MessageNotWriteableException(org.springframework.jms.MessageNotWriteableException) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) JmsUtils(org.springframework.jms.support.JmsUtils) TextMessage(jakarta.jms.TextMessage) ConnectionFactoryUtils(org.springframework.jms.connection.ConnectionFactoryUtils) DeliveryMode(jakarta.jms.DeliveryMode) TransactionRolledBackException(org.springframework.jms.TransactionRolledBackException) JmsSecurityException(org.springframework.jms.JmsSecurityException) Mockito.reset(org.mockito.Mockito.reset) InvalidSelectorException(org.springframework.jms.InvalidSelectorException) Mockito.mock(org.mockito.Mockito.mock) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage)

Example 5 with TextMessage

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

the class JmsMessagingTemplateTests method createTextMessage.

protected TextMessage createTextMessage(MessageCreator creator) throws JMSException {
    Session mock = mock(Session.class);
    given(mock.createTextMessage(any())).willAnswer((Answer<TextMessage>) invocation -> new StubTextMessage((String) invocation.getArguments()[0]));
    jakarta.jms.Message message = creator.createMessage(mock);
    verify(mock).createTextMessage(any());
    return (TextMessage) message;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MessagingException(org.springframework.messaging.MessagingException) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) HashMap(java.util.HashMap) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) DestinationResolutionException(org.springframework.jms.support.destination.DestinationResolutionException) Captor(org.mockito.Captor) Session(jakarta.jms.Session) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) StubTextMessage(org.springframework.jms.StubTextMessage) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) Destination(jakarta.jms.Destination) MessageFormatException(jakarta.jms.MessageFormatException) MessagingMessageConverter(org.springframework.jms.support.converter.MessagingMessageConverter) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Message(org.springframework.messaging.Message) GenericMessageConverter(org.springframework.messaging.converter.GenericMessageConverter) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) MessageNotWriteableException(jakarta.jms.MessageNotWriteableException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) Mockito.times(org.mockito.Mockito.times) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TextMessage(jakarta.jms.TextMessage) Writer(java.io.Writer) MessageConverter(org.springframework.jms.support.converter.MessageConverter) MessageBuilder(org.springframework.messaging.support.MessageBuilder) Mockito.mock(org.mockito.Mockito.mock) StubTextMessage(org.springframework.jms.StubTextMessage) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session)

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