Search in sources :

Example 6 with ConnectionFactory

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

the class JmsAutoConfigurationTests method testPubSubDomainOverride.

@Test
void testPubSubDomainOverride() {
    this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues("spring.jms.pubSubDomain:false").run((context) -> {
        assertThat(context).hasSingleBean(JmsTemplate.class);
        assertThat(context).hasSingleBean(ConnectionFactory.class);
        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
        ConnectionFactory factory = context.getBean(ConnectionFactory.class);
        assertThat(jmsTemplate).isNotNull();
        assertThat(jmsTemplate.isPubSubDomain()).isFalse();
        assertThat(factory).isNotNull().isEqualTo(jmsTemplate.getConnectionFactory());
    });
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ConnectionFactory(jakarta.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) Test(org.junit.jupiter.api.Test)

Example 7 with ConnectionFactory

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

the class JmsHealthIndicatorTests method jmsBrokerIsDown.

@Test
void jmsBrokerIsDown() throws JMSException {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willThrow(new JMSException("test", "123"));
    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) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Example 8 with ConnectionFactory

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

the class JmsHealthIndicatorTests method whenConnectionStartIsUnresponsiveStatusIsDown.

@Test
void whenConnectionStartIsUnresponsiveStatusIsDown() throws JMSException {
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
    Connection connection = mock(Connection.class);
    UnresponsiveStartAnswer unresponsiveStartAnswer = new UnresponsiveStartAnswer();
    willAnswer(unresponsiveStartAnswer).given(connection).start();
    willAnswer((invocation) -> {
        unresponsiveStartAnswer.connectionClosed();
        return null;
    }).given(connection).close();
    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((String) health.getDetails().get("error")).contains("Connection closed");
}
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 9 with ConnectionFactory

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

the class DelegatingConnectionFactory method obtainTargetConnectionFactory.

private ConnectionFactory obtainTargetConnectionFactory() {
    ConnectionFactory target = getTargetConnectionFactory();
    Assert.state(target != null, "No 'targetConnectionFactory' set");
    return target;
}
Also used : TopicConnectionFactory(jakarta.jms.TopicConnectionFactory) ConnectionFactory(jakarta.jms.ConnectionFactory) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory)

Example 10 with ConnectionFactory

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

the class JmsTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new InvalidIsolationLevelException("JMS does not support an isolation level concept");
    }
    ConnectionFactory connectionFactory = obtainConnectionFactory();
    JmsTransactionObject txObject = (JmsTransactionObject) transaction;
    Connection con = null;
    Session session = null;
    try {
        JmsResourceHolder resourceHolder;
        if (this.lazyResourceRetrieval) {
            resourceHolder = new LazyJmsResourceHolder(connectionFactory);
        } else {
            con = createConnection();
            session = createSession(con);
            if (logger.isDebugEnabled()) {
                logger.debug("Created JMS transaction on Session [" + session + "] from Connection [" + con + "]");
            }
            resourceHolder = new JmsResourceHolder(connectionFactory, con, session);
        }
        resourceHolder.setSynchronizedWithTransaction(true);
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            resourceHolder.setTimeoutInSeconds(timeout);
        }
        txObject.setResourceHolder(resourceHolder);
        TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolder);
    } catch (Throwable ex) {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        throw new CannotCreateTransactionException("Could not create JMS transaction", ex);
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Connection(jakarta.jms.Connection) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) Session(jakarta.jms.Session)

Aggregations

ConnectionFactory (jakarta.jms.ConnectionFactory)60 Test (org.junit.jupiter.api.Test)42 Connection (jakarta.jms.Connection)32 JMSException (jakarta.jms.JMSException)20 Session (jakarta.jms.Session)19 Message (jakarta.jms.Message)15 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)15 TopicConnectionFactory (jakarta.jms.TopicConnectionFactory)15 JmsTemplate (org.springframework.jms.core.JmsTemplate)11 QueueConnection (jakarta.jms.QueueConnection)10 TopicConnection (jakarta.jms.TopicConnection)10 StubQueue (org.springframework.jms.StubQueue)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 BDDMockito.given (org.mockito.BDDMockito.given)9 Mockito.mock (org.mockito.Mockito.mock)9 Mockito.verify (org.mockito.Mockito.verify)9 TransactionStatus (org.springframework.transaction.TransactionStatus)9 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)9 Destination (jakarta.jms.Destination)8 MessageProducer (jakarta.jms.MessageProducer)8