Search in sources :

Example 26 with Connection

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

the class JmsHealthIndicatorTests method jmsBrokerCouldNotRetrieveProviderMetadata.

@Test
void jmsBrokerCouldNotRetrieveProviderMetadata() throws JMSException {
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willThrow(new JMSException("test", "123"));
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    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();
    then(connection).should().close();
}
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)

Example 27 with Connection

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

the class JmsHealthIndicatorTests method jmsBrokerIsUp.

@Test
void jmsBrokerIsUp() throws JMSException {
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
    Health health = indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertThat(health.getDetails().get("provider")).isEqualTo("JMS test provider");
    then(connection).should().close();
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Health(org.springframework.boot.actuate.health.Health) ConnectionMetaData(jakarta.jms.ConnectionMetaData) Connection(jakarta.jms.Connection) Test(org.junit.jupiter.api.Test)

Example 28 with Connection

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

the class JmsHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    try (Connection connection = this.connectionFactory.createConnection()) {
        new MonitoredConnection(connection).start();
        builder.up().withDetail("provider", connection.getMetaData().getJMSProviderName());
    }
}
Also used : Connection(jakarta.jms.Connection)

Example 29 with Connection

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

the class JmsTransactionManagerTests method testTransactionRollback.

@Test
public void testTransactionRollback() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.execute((SessionCallback<Void>) sess -> {
        assertThat(session).isSameAs(sess);
        return null;
    });
    tm.rollback(ts);
    verify(session).rollback();
    verify(session).close();
    verify(con).close();
}
Also used : MessageProducer(jakarta.jms.MessageProducer) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StubQueue(org.springframework.jms.StubQueue) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(jakarta.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 30 with Connection

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

the class JmsTransactionManagerTests method testTransactionCommitWithMessageProducer.

@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
    Destination dest = new StubQueue();
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Session session = mock(Session.class);
    MessageProducer producer = mock(MessageProducer.class);
    final Message message = mock(Message.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    given(session.createProducer(dest)).willReturn(producer);
    given(session.getTransacted()).willReturn(true);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.send(dest, sess -> message);
    tm.commit(ts);
    verify(producer).send(message);
    verify(session).commit();
    verify(producer).close();
    verify(session).close();
    verify(con).close();
}
Also used : Destination(jakarta.jms.Destination) ConnectionFactory(jakarta.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) StubQueue(org.springframework.jms.StubQueue) MessageProducer(jakarta.jms.MessageProducer) Session(jakarta.jms.Session) 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