use of jakarta.jms.Session in project spring-framework by spring-projects.
the class MessagingMessageListenerAdapterTests method buildMessageWithStandardMessage.
@Test
public void buildMessageWithStandardMessage() throws JMSException {
Destination replyTo = new Destination() {
};
Message<String> result = MessageBuilder.withPayload("Response").setHeader("foo", "bar").setHeader(JmsHeaders.TYPE, "msg_type").setHeader(JmsHeaders.REPLY_TO, replyTo).build();
Session session = mock(Session.class);
given(session.createTextMessage("Response")).willReturn(new StubTextMessage("Response"));
MessagingMessageListenerAdapter listener = getSimpleInstance("echo", Message.class);
jakarta.jms.Message replyMessage = listener.buildMessage(session, result);
verify(session).createTextMessage("Response");
assertThat(replyMessage).as("reply should never be null").isNotNull();
assertThat(((TextMessage) replyMessage).getText()).isEqualTo("Response");
assertThat(replyMessage.getStringProperty("foo")).as("custom header not copied").isEqualTo("bar");
assertThat(replyMessage.getJMSType()).as("type header not copied").isEqualTo("msg_type");
assertThat(replyMessage.getJMSReplyTo()).as("replyTo header not copied").isEqualTo(replyTo);
}
use of jakarta.jms.Session 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();
}
use of jakarta.jms.Session in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testTaskExecutorCorrectlyInvokedWhenSpecified.
@Test
public void testTaskExecutorCorrectlyInvokedWhenSpecified() throws Exception {
final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
final Session session = mock(Session.class);
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
// no MessageSelector...
given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
given(session.getTransacted()).willReturn(false);
given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE);
Connection connection = mock(Connection.class);
given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
given(connectionFactory.createConnection()).willReturn(connection);
final TestMessageListener listener = new TestMessageListener();
this.container.setConnectionFactory(connectionFactory);
this.container.setDestinationName(DESTINATION_NAME);
this.container.setMessageListener(listener);
this.container.setTaskExecutor(task -> {
listener.executorInvoked = true;
assertThat(listener.listenerInvoked).isFalse();
task.run();
assertThat(listener.listenerInvoked).isTrue();
});
this.container.afterPropertiesSet();
this.container.start();
final Message message = mock(Message.class);
messageConsumer.sendMessage(message);
assertThat(listener.executorInvoked).isTrue();
assertThat(listener.listenerInvoked).isTrue();
verify(connection).setExceptionListener(this.container);
verify(connection).start();
}
use of jakarta.jms.Session in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testDestroyClosesConsumersSessionsAndConnectionInThatOrder.
@Test
public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() throws Exception {
MessageConsumer messageConsumer = mock(MessageConsumer.class);
Session session = mock(Session.class);
// Queue gets created in order to create MessageConsumer for that Destination...
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
// and then the MessageConsumer gets created...
// no MessageSelector...
given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
Connection connection = mock(Connection.class);
// session gets created in order to register MessageListener...
given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
// and the connection is start()ed after the listener is registered...
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
given(connectionFactory.createConnection()).willReturn(connection);
this.container.setConnectionFactory(connectionFactory);
this.container.setDestinationName(DESTINATION_NAME);
this.container.setMessageListener(new TestMessageListener());
this.container.afterPropertiesSet();
this.container.start();
this.container.destroy();
verify(messageConsumer).close();
verify(session).close();
verify(connection).setExceptionListener(this.container);
verify(connection).start();
verify(connection).close();
}
use of jakarta.jms.Session in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testRegisteredErrorHandlerIsInvokedOnException.
@Test
public void testRegisteredErrorHandlerIsInvokedOnException() throws Exception {
final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
Session session = mock(Session.class);
// Queue gets created in order to create MessageConsumer for that Destination...
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
// and then the MessageConsumer gets created...
// no MessageSelector...
given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
// an exception is thrown, so the rollback logic is being applied here...
given(session.getTransacted()).willReturn(false);
Connection connection = mock(Connection.class);
// session gets created in order to register MessageListener...
given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
given(connectionFactory.createConnection()).willReturn(connection);
final IllegalStateException theException = new IllegalStateException("intentional test failure");
this.container.setConnectionFactory(connectionFactory);
this.container.setDestinationName(DESTINATION_NAME);
this.container.setMessageListener((SessionAwareMessageListener<Message>) (Message message, @Nullable Session session1) -> {
throw theException;
});
ErrorHandler errorHandler = mock(ErrorHandler.class);
this.container.setErrorHandler(errorHandler);
this.container.afterPropertiesSet();
this.container.start();
// manually trigger an Exception with the above bad MessageListener...
Message message = mock(Message.class);
// a Throwable from a MessageListener MUST simply be swallowed...
messageConsumer.sendMessage(message);
verify(connection).setExceptionListener(this.container);
verify(connection).start();
verify(errorHandler).handleError(theException);
}
Aggregations