Search in sources :

Example 1 with Connection

use of jakarta.jms.Connection 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 2 with Connection

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

the class ConnectionFactoryUtils method doGetTransactionalSession.

/**
 * Obtain a JMS Session that is synchronized with the current transaction, if any.
 * @param connectionFactory the JMS ConnectionFactory to bind for
 * (used as TransactionSynchronizationManager key)
 * @param resourceFactory the ResourceFactory to use for extracting or creating
 * JMS resources
 * @param startConnection whether the underlying JMS Connection approach should be
 * started in order to allow for receiving messages. Note that a reused Connection
 * may already have been started before, even if this flag is {@code false}.
 * @return the transactional Session, or {@code null} if none found
 * @throws JMSException in case of JMS failure
 */
@Nullable
public static Session doGetTransactionalSession(ConnectionFactory connectionFactory, ResourceFactory resourceFactory, boolean startConnection) throws JMSException {
    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(resourceFactory, "ResourceFactory must not be null");
    JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager.getResource(connectionFactory);
    if (resourceHolder != null) {
        Session session = resourceFactory.getSession(resourceHolder);
        if (session != null) {
            if (startConnection) {
                Connection con = resourceFactory.getConnection(resourceHolder);
                if (con != null) {
                    con.start();
                }
            }
            return session;
        }
        if (resourceHolder.isFrozen()) {
            return null;
        }
    }
    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        return null;
    }
    JmsResourceHolder resourceHolderToUse = resourceHolder;
    if (resourceHolderToUse == null) {
        resourceHolderToUse = new JmsResourceHolder(connectionFactory);
    }
    Connection con = resourceFactory.getConnection(resourceHolderToUse);
    Session session = null;
    try {
        boolean isExistingCon = (con != null);
        if (!isExistingCon) {
            con = resourceFactory.createConnection();
            resourceHolderToUse.addConnection(con);
        }
        session = resourceFactory.createSession(con);
        resourceHolderToUse.addSession(session, con);
        if (startConnection) {
            con.start();
        }
    } catch (JMSException ex) {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        throw ex;
    }
    if (resourceHolderToUse != resourceHolder) {
        TransactionSynchronizationManager.registerSynchronization(new JmsResourceSynchronization(resourceHolderToUse, connectionFactory, resourceFactory.isSynchedLocalTransactionAllowed()));
        resourceHolderToUse.setSynchronizedWithTransaction(true);
        TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolderToUse);
    }
    return session;
}
Also used : QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Nullable(org.springframework.lang.Nullable)

Example 3 with Connection

use of jakarta.jms.Connection 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)

Example 4 with Connection

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

the class SingleConnectionFactory method createTopicConnection.

@Override
public TopicConnection createTopicConnection() throws JMSException {
    Connection con;
    synchronized (this.connectionMonitor) {
        this.pubSubMode = Boolean.TRUE;
        con = createConnection();
    }
    if (!(con instanceof TopicConnection)) {
        throw new jakarta.jms.IllegalStateException("This SingleConnectionFactory does not hold a TopicConnection but rather: " + con);
    }
    return ((TopicConnection) con);
}
Also used : QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) TopicConnection(jakarta.jms.TopicConnection)

Example 5 with Connection

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

the class SingleConnectionFactory method createQueueConnection.

@Override
public QueueConnection createQueueConnection() throws JMSException {
    Connection con;
    synchronized (this.connectionMonitor) {
        this.pubSubMode = Boolean.FALSE;
        con = createConnection();
    }
    if (!(con instanceof QueueConnection)) {
        throw new jakarta.jms.IllegalStateException("This SingleConnectionFactory does not hold a QueueConnection but rather: " + con);
    }
    return ((QueueConnection) con);
}
Also used : QueueConnection(jakarta.jms.QueueConnection) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection)

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