Browse Source

Fine-tuned JCA MessageEndpoint exception logging and propagation

Issue: SPR-16717

(cherry picked from commit 8e1ecec)
pull/1798/head
Juergen Hoeller 8 years ago
parent
commit
8f7e5e7c1a
  1. 8
      spring-jms/src/main/java/org/springframework/jms/listener/endpoint/JmsMessageEndpointFactory.java
  2. 6
      spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java
  3. 29
      spring-tx/src/main/java/org/springframework/jca/endpoint/GenericMessageEndpointFactory.java

8
spring-jms/src/main/java/org/springframework/jms/listener/endpoint/JmsMessageEndpointFactory.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -84,6 +84,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -84,6 +84,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
@Override
public void onMessage(Message message) {
Throwable endpointEx = null;
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
if (applyDeliveryCalls) {
try {
@ -97,6 +98,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -97,6 +98,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
getMessageListener().onMessage(message);
}
catch (RuntimeException | Error ex) {
endpointEx = ex;
onEndpointException(ex);
throw ex;
}
@ -106,7 +108,9 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -106,7 +108,9 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
afterDelivery();
}
catch (ResourceException ex) {
throw new JmsResourceException(ex);
if (endpointEx == null) {
throw new JmsResourceException(ex);
}
}
}
}

6
spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java

@ -269,9 +269,10 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -269,9 +269,10 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
* endpoint throwing an exception.
* @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");
this.transactionDelegate.setRollbackOnly();
logger.debug("Transaction marked as rollback-only after endpoint exception", ex);
}
/**
@ -291,6 +292,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -291,6 +292,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
this.transactionDelegate.endTransaction();
}
catch (Throwable ex) {
logger.warn("Failed to complete transaction after endpoint delivery", ex);
throw new ApplicationServerInternalException("Failed to complete transaction", ex);
}
}
@ -303,7 +305,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -303,7 +305,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
this.transactionDelegate.endTransaction();
}
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);
}
}
}

29
spring-tx/src/main/java/org/springframework/jca/endpoint/GenericMessageEndpointFactory.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -108,24 +108,21 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor @@ -108,24 +108,21 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Throwable endpointEx = null;
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
if (applyDeliveryCalls) {
try {
beforeDelivery(null);
}
catch (ResourceException ex) {
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
throw ex;
}
else {
throw new InternalResourceException(ex);
}
throw adaptExceptionIfNecessary(methodInvocation, ex);
}
}
try {
return methodInvocation.proceed();
}
catch (Throwable ex) {
endpointEx = ex;
onEndpointException(ex);
throw ex;
}
@ -135,17 +132,23 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor @@ -135,17 +132,23 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor
afterDelivery();
}
catch (ResourceException ex) {
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
throw ex;
}
else {
throw new InternalResourceException(ex);
if (endpointEx == null) {
throw adaptExceptionIfNecessary(methodInvocation, ex);
}
}
}
}
}
private Exception adaptExceptionIfNecessary(MethodInvocation methodInvocation, ResourceException ex) {
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
return ex;
}
else {
return new InternalResourceException(ex);
}
}
@Override
protected ClassLoader getEndpointClassLoader() {
return getMessageListener().getClass().getClassLoader();
@ -164,7 +167,7 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor @@ -164,7 +167,7 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor
@SuppressWarnings("serial")
public static class InternalResourceException extends RuntimeException {
protected InternalResourceException(ResourceException cause) {
public InternalResourceException(ResourceException cause) {
super(cause);
}
}

Loading…
Cancel
Save