Search in sources :

Example 6 with JMSException

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

the class JmsTemplate method executeLocal.

/**
 * A variant of {@link #execute(SessionCallback, boolean)} that explicitly
 * creates a non-transactional {@link Session}. The given {@link SessionCallback}
 * does not participate in an existing transaction.
 */
@Nullable
private <T> T executeLocal(SessionCallback<T> action, boolean startConnection) throws JmsException {
    Assert.notNull(action, "Callback object must not be null");
    Connection con = null;
    Session session = null;
    try {
        con = createConnection();
        session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        if (startConnection) {
            con.start();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing callback on JMS Session: " + session);
        }
        return action.doInJms(session);
    } catch (JMSException ex) {
        throw convertJmsAccessException(ex);
    } finally {
        JmsUtils.closeSession(session);
        ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), startConnection);
    }
}
Also used : Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) Nullable(org.springframework.lang.Nullable)

Example 7 with JMSException

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

the class MessageListenerAdapter method invokeListenerMethod.

/**
 * Invoke the specified listener method.
 * @param methodName the name of the listener method
 * @param arguments the message arguments to be passed in
 * @return the result returned from the listener method
 * @throws JMSException if thrown by JMS API methods
 * @see #getListenerMethodName
 * @see #buildListenerArguments
 */
@Nullable
protected Object invokeListenerMethod(String methodName, Object[] arguments) throws JMSException {
    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(getDelegate());
        methodInvoker.setTargetMethod(methodName);
        methodInvoker.setArguments(arguments);
        methodInvoker.prepare();
        return methodInvoker.invoke();
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof JMSException) {
            throw (JMSException) targetEx;
        } else {
            throw new ListenerExecutionFailedException("Listener method '" + methodName + "' threw exception", targetEx);
        }
    } catch (Throwable ex) {
        throw new ListenerExecutionFailedException("Failed to invoke target method '" + methodName + "' with arguments " + ObjectUtils.nullSafeToString(arguments), ex);
    }
}
Also used : JMSException(jakarta.jms.JMSException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodInvoker(org.springframework.util.MethodInvoker) Nullable(org.springframework.lang.Nullable)

Example 8 with JMSException

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

the class DefaultMessageListenerContainer method refreshConnectionUntilSuccessful.

/**
 * Refresh the underlying Connection, not returning before an attempt has been
 * successful. Called in case of a shared Connection as well as without shared
 * Connection, so either needs to operate on the shared Connection or on a
 * temporary Connection that just gets established for validation purposes.
 * <p>The default implementation retries until it successfully established a
 * Connection, for as long as this message listener container is running.
 * Applies the specified recovery interval between retries.
 * @see #setRecoveryInterval
 * @see #start()
 * @see #stop()
 */
protected void refreshConnectionUntilSuccessful() {
    BackOffExecution execution = this.backOff.start();
    while (isRunning()) {
        try {
            if (sharedConnectionEnabled()) {
                refreshSharedConnection();
            } else {
                Connection con = createConnection();
                JmsUtils.closeConnection(con);
            }
            logger.debug("Successfully refreshed JMS Connection");
            break;
        } catch (Exception ex) {
            if (ex instanceof JMSException) {
                invokeExceptionListener((JMSException) ex);
            }
            StringBuilder msg = new StringBuilder();
            msg.append("Could not refresh JMS Connection for destination '");
            msg.append(getDestinationDescription()).append("' - retrying using ");
            msg.append(execution).append(". Cause: ");
            msg.append(ex instanceof JMSException ? JmsUtils.buildExceptionMessage((JMSException) ex) : ex.getMessage());
            if (logger.isDebugEnabled()) {
                logger.error(msg, ex);
            } else {
                logger.error(msg);
            }
        }
        if (!applyBackOffTime(execution)) {
            logger.error("Stopping container for destination '" + getDestinationDescription() + "': back-off policy does not allow for further attempts.");
            stop();
        }
    }
}
Also used : BackOffExecution(org.springframework.util.backoff.BackOffExecution) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) JmsException(org.springframework.jms.JmsException) JMSException(jakarta.jms.JMSException)

Example 9 with JMSException

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

the class JmsMessagingTemplateTests method createJmsTextMessage.

private jakarta.jms.Message createJmsTextMessage(String payload) {
    try {
        StubTextMessage jmsMessage = new StubTextMessage(payload);
        jmsMessage.setStringProperty("foo", "bar");
        return jmsMessage;
    } catch (JMSException e) {
        throw new IllegalStateException("Should not happen", e);
    }
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) StubTextMessage(org.springframework.jms.StubTextMessage) JMSException(jakarta.jms.JMSException)

Example 10 with JMSException

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

the class JmsTransactionManagerTests method testLazyTransactionalSession.

@Test
public void testLazyTransactionalSession() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    tm.setLazyResourceRetrieval(true);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTemplate jt = new JmsTemplate(cf);
    jt.execute((SessionCallback<Void>) sess -> {
        assertThat(session).isSameAs(sess);
        return null;
    });
    tm.commit(ts);
    verify(session).commit();
    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)

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