Search in sources :

Example 21 with Connection

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

the class SimpleMessageListenerContainerTests method testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate.

@Test
public void testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate() throws Exception {
    this.container.setSessionTransacted(true);
    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(true);
    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((MessageListener) message -> {
        throw new UnsupportedOperationException();
    });
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    // Session is rolled back 'cos it is transacted...
    verify(session).rollback();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : Message(jakarta.jms.Message) StubQueue(org.springframework.jms.StubQueue) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) JMSException(jakarta.jms.JMSException) MessageListener(jakarta.jms.MessageListener) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Mockito.verify(org.mockito.Mockito.verify) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ErrorHandler(org.springframework.util.ErrorHandler) Assertions.fail(org.assertj.core.api.Assertions.fail) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Nullable(org.springframework.lang.Nullable) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 22 with Connection

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

the class SimpleMessageListenerContainerTests method testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate.

@Test
public void testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate() 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);
    // 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((MessageListener) message -> {
        throw new UnsupportedOperationException();
    });
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final 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();
}
Also used : Message(jakarta.jms.Message) StubQueue(org.springframework.jms.StubQueue) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) JMSException(jakarta.jms.JMSException) MessageListener(jakarta.jms.MessageListener) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Mockito.verify(org.mockito.Mockito.verify) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ErrorHandler(org.springframework.util.ErrorHandler) Assertions.fail(org.assertj.core.api.Assertions.fail) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Nullable(org.springframework.lang.Nullable) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 23 with Connection

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

the class SimpleMessageListenerContainerTests method testCorrectSessionExposedForSessionAwareMessageListenerInvocation.

@Test
public void testCorrectSessionExposedForSessionAwareMessageListenerInvocation() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    final 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);
    given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE);
    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...
    final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final Set<String> failure = new HashSet<>(1);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener((SessionAwareMessageListener<Message>) (Message message, @Nullable Session sess) -> {
        try {
            // Check correct Session passed into SessionAwareMessageListener.
            assertThat(session).isSameAs(sess);
        } catch (Throwable ex) {
            failure.add("MessageListener execution failed: " + ex);
        }
    });
    this.container.afterPropertiesSet();
    this.container.start();
    final Message message = mock(Message.class);
    messageConsumer.sendMessage(message);
    if (!failure.isEmpty()) {
        fail(failure.iterator().next().toString());
    }
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 24 with Connection

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

the class SimpleMessageListenerContainerTests method testRegisteredExceptionListenerIsInvokedOnException.

@Test
public void testRegisteredExceptionListenerIsInvokedOnException() 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);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final JMSException theException = new JMSException(EXCEPTION_MESSAGE);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener((SessionAwareMessageListener<Message>) (Message message, @Nullable Session session1) -> {
        throw theException;
    });
    ExceptionListener exceptionListener = mock(ExceptionListener.class);
    this.container.setExceptionListener(exceptionListener);
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final 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(exceptionListener).onException(theException);
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 25 with Connection

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

the class JmsHealthIndicatorTests method jmsBrokerUsesFailover.

@Test
void jmsBrokerUsesFailover() throws JMSException {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    willThrow(new JMSException("Could not start", "123")).given(connection).start();
    given(connectionFactory.createConnection()).willReturn(connection);
    JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
    Health health = indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat(health.getDetails().get("provider")).isNull();
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Health(org.springframework.boot.actuate.health.Health) ConnectionMetaData(jakarta.jms.ConnectionMetaData) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (jakarta.jms.Connection)52 Test (org.junit.jupiter.api.Test)40 ConnectionFactory (jakarta.jms.ConnectionFactory)31 Session (jakarta.jms.Session)29 JMSException (jakarta.jms.JMSException)22 QueueConnection (jakarta.jms.QueueConnection)21 TopicConnection (jakarta.jms.TopicConnection)21 Message (jakarta.jms.Message)16 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)11 TopicConnectionFactory (jakarta.jms.TopicConnectionFactory)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 Mockito.verify (org.mockito.Mockito.verify)10 StubQueue (org.springframework.jms.StubQueue)10 Destination (jakarta.jms.Destination)9 MessageProducer (jakarta.jms.MessageProducer)9 MessageConsumer (jakarta.jms.MessageConsumer)8 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)8 JmsTemplate (org.springframework.jms.core.JmsTemplate)8