Fine-tuned JCA MessageEndpoint exception logging and propagation

Issue: SPR-16717
This commit is contained in:
Juergen Hoeller 2018-04-12 14:46:26 +02:00
parent 7ee6130680
commit 8e1ececd97
3 changed files with 26 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -84,6 +84,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
@Override @Override
public void onMessage(Message message) { public void onMessage(Message message) {
Throwable endpointEx = null;
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled(); boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
if (applyDeliveryCalls) { if (applyDeliveryCalls) {
try { try {
@ -97,6 +98,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
getMessageListener().onMessage(message); getMessageListener().onMessage(message);
} }
catch (RuntimeException | Error ex) { catch (RuntimeException | Error ex) {
endpointEx = ex;
onEndpointException(ex); onEndpointException(ex);
throw ex; throw ex;
} }
@ -106,11 +108,13 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
afterDelivery(); afterDelivery();
} }
catch (ResourceException ex) { catch (ResourceException ex) {
if (endpointEx == null) {
throw new JmsResourceException(ex); throw new JmsResourceException(ex);
} }
} }
} }
} }
}
@Override @Override
protected ClassLoader getEndpointClassLoader() { protected ClassLoader getEndpointClassLoader() {

View File

@ -269,9 +269,10 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
* endpoint throwing an exception. * endpoint throwing an exception.
* @param ex the exception thrown from the concrete endpoint * @param ex the exception thrown from the concrete endpoint
*/ */
protected final void onEndpointException(Throwable ex) { protected void onEndpointException(Throwable ex) {
Assert.state(this.transactionDelegate != null, "Not initialized"); Assert.state(this.transactionDelegate != null, "Not initialized");
this.transactionDelegate.setRollbackOnly(); this.transactionDelegate.setRollbackOnly();
logger.debug("Transaction marked as rollback-only after endpoint exception", ex);
} }
/** /**
@ -291,6 +292,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
this.transactionDelegate.endTransaction(); this.transactionDelegate.endTransaction();
} }
catch (Throwable ex) { catch (Throwable ex) {
logger.warn("Failed to complete transaction after endpoint delivery", ex);
throw new ApplicationServerInternalException("Failed to complete transaction", ex); throw new ApplicationServerInternalException("Failed to complete transaction", ex);
} }
} }
@ -303,7 +305,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
this.transactionDelegate.endTransaction(); this.transactionDelegate.endTransaction();
} }
catch (Throwable ex) { catch (Throwable ex) {
logger.error("Could not complete unfinished transaction on endpoint release", ex); logger.warn("Could not complete unfinished transaction on endpoint release", ex);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -108,24 +108,21 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor
@Override @Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Throwable endpointEx = null;
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled(); boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
if (applyDeliveryCalls) { if (applyDeliveryCalls) {
try { try {
beforeDelivery(null); beforeDelivery(null);
} }
catch (ResourceException ex) { catch (ResourceException ex) {
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) { throw adaptExceptionIfNecessary(methodInvocation, ex);
throw ex;
}
else {
throw new InternalResourceException(ex);
}
} }
} }
try { try {
return methodInvocation.proceed(); return methodInvocation.proceed();
} }
catch (Throwable ex) { catch (Throwable ex) {
endpointEx = ex;
onEndpointException(ex); onEndpointException(ex);
throw ex; throw ex;
} }
@ -135,14 +132,20 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor
afterDelivery(); afterDelivery();
} }
catch (ResourceException ex) { catch (ResourceException ex) {
if (endpointEx == null) {
throw adaptExceptionIfNecessary(methodInvocation, ex);
}
}
}
}
}
private Exception adaptExceptionIfNecessary(MethodInvocation methodInvocation, ResourceException ex) {
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) { if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
throw ex; return ex;
} }
else { else {
throw new InternalResourceException(ex); return new InternalResourceException(ex);
}
}
}
} }
} }
@ -164,7 +167,7 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static class InternalResourceException extends RuntimeException { public static class InternalResourceException extends RuntimeException {
protected InternalResourceException(ResourceException cause) { public InternalResourceException(ResourceException cause) {
super(cause); super(cause);
} }
} }