Search in sources :

Example 1 with JMSException

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

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

the class JmsException method getMessage.

/**
 * Return the detail message, including the message from the linked exception
 * if there is one.
 * @see jakarta.jms.JMSException#getLinkedException()
 */
@Override
@Nullable
public String getMessage() {
    String message = super.getMessage();
    Throwable cause = getCause();
    if (cause instanceof JMSException) {
        Exception linkedEx = ((JMSException) cause).getLinkedException();
        if (linkedEx != null) {
            String linkedMessage = linkedEx.getMessage();
            String causeMessage = cause.getMessage();
            if (linkedMessage != null && (causeMessage == null || !causeMessage.contains(linkedMessage))) {
                message = message + "; nested exception is " + linkedEx;
            }
        }
    }
    return message;
}
Also used : JMSException(jakarta.jms.JMSException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) JMSException(jakarta.jms.JMSException) Nullable(org.springframework.lang.Nullable)

Example 3 with JMSException

use of jakarta.jms.JMSException 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 4 with JMSException

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

the class JmsTransactionManager method doRollback.

@Override
protected void doRollback(DefaultTransactionStatus status) {
    JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
    Session session = txObject.getResourceHolder().getOriginalSession();
    if (session != null) {
        try {
            if (status.isDebug()) {
                logger.debug("Rolling back JMS transaction on Session [" + session + "]");
            }
            session.rollback();
        } catch (JMSException ex) {
            throw new TransactionSystemException("Could not roll back JMS transaction", ex);
        }
    }
}
Also used : JMSException(jakarta.jms.JMSException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) Session(jakarta.jms.Session)

Example 5 with JMSException

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

the class StandardJmsActivationSpecFactory method populateActivationSpecProperties.

/**
 * Populate the given ApplicationSpec object with the settings
 * defined in the given configuration object.
 * <p>This implementation applies all standard JMS settings, but ignores
 * "maxConcurrency" and "prefetchSize" - not supported in standard JCA 1.5.
 * @param bw the BeanWrapper wrapping the ActivationSpec object
 * @param config the configured object holding common JMS settings
 */
protected void populateActivationSpecProperties(BeanWrapper bw, JmsActivationSpecConfig config) {
    String destinationName = config.getDestinationName();
    if (destinationName != null) {
        boolean pubSubDomain = config.isPubSubDomain();
        Object destination = destinationName;
        if (this.destinationResolver != null) {
            try {
                destination = this.destinationResolver.resolveDestinationName(null, destinationName, pubSubDomain);
            } catch (JMSException ex) {
                throw new DestinationResolutionException("Cannot resolve destination name [" + destinationName + "]", ex);
            }
        }
        bw.setPropertyValue("destination", destination);
        bw.setPropertyValue("destinationType", pubSubDomain ? Topic.class.getName() : Queue.class.getName());
    }
    if (bw.isWritableProperty("subscriptionDurability")) {
        bw.setPropertyValue("subscriptionDurability", config.isSubscriptionDurable() ? "Durable" : "NonDurable");
    } else if (config.isSubscriptionDurable()) {
        // Standard JCA 1.5 "subscriptionDurability" apparently not supported...
        throw new IllegalArgumentException("Durable subscriptions not supported by underlying provider");
    }
    if (config.isSubscriptionShared()) {
        throw new IllegalArgumentException("Shared subscriptions not supported for JCA-driven endpoints");
    }
    if (config.getSubscriptionName() != null) {
        bw.setPropertyValue("subscriptionName", config.getSubscriptionName());
    }
    if (config.getClientId() != null) {
        bw.setPropertyValue("clientId", config.getClientId());
    }
    if (config.getMessageSelector() != null) {
        bw.setPropertyValue("messageSelector", config.getMessageSelector());
    }
    applyAcknowledgeMode(bw, config.getAcknowledgeMode());
}
Also used : DestinationResolutionException(org.springframework.jms.support.destination.DestinationResolutionException) JMSException(jakarta.jms.JMSException)

Aggregations

JMSException (jakarta.jms.JMSException)38 Connection (jakarta.jms.Connection)21 Test (org.junit.jupiter.api.Test)20 ConnectionFactory (jakarta.jms.ConnectionFactory)18 Session (jakarta.jms.Session)16 Destination (jakarta.jms.Destination)10 Message (jakarta.jms.Message)10 MessageProducer (jakarta.jms.MessageProducer)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)8 BDDMockito.given (org.mockito.BDDMockito.given)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.times (org.mockito.Mockito.times)8 Mockito.verify (org.mockito.Mockito.verify)8 TransactionStatus (org.springframework.transaction.TransactionStatus)8 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)8 AfterEach (org.junit.jupiter.api.AfterEach)7 StubQueue (org.springframework.jms.StubQueue)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 SessionCallback (org.springframework.jms.core.SessionCallback)7