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);
}
}
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);
}
}
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();
}
}
}
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);
}
}
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();
}
Aggregations