Browse Source

Fine-tuned JCA MessageEndpoint exception logging and propagation

Issue: SPR-16717

(cherry picked from commit 8e1ecec)
pull/1876/head
Juergen Hoeller 8 years ago
parent
commit
e54c9d4a93
  1. 11
      spring-jms/src/main/java/org/springframework/jms/listener/endpoint/JmsMessageEndpointFactory.java
  2. 15
      spring-tx/src/main/java/org/springframework/jca/endpoint/AbstractMessageEndpointFactory.java
  3. 27
      spring-tx/src/main/java/org/springframework/jca/endpoint/GenericMessageEndpointFactory.java

11
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.
@ -80,6 +80,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -80,6 +80,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
@Override
public void onMessage(Message message) {
Throwable endpointEx = null;
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
if (applyDeliveryCalls) {
try {
@ -93,10 +94,12 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -93,10 +94,12 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
messageListener.onMessage(message);
}
catch (RuntimeException ex) {
endpointEx = ex;
onEndpointException(ex);
throw ex;
}
catch (Error err) {
endpointEx = err;
onEndpointException(err);
throw err;
}
@ -106,7 +109,9 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -106,7 +109,9 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
afterDelivery();
}
catch (ResourceException ex) {
throw new JmsResourceException(ex);
if (endpointEx == null) {
throw new JmsResourceException(ex);
}
}
}
}
@ -114,7 +119,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory { @@ -114,7 +119,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
@Override
protected ClassLoader getEndpointClassLoader() {
return messageListener.getClass().getClassLoader();
return getMessageListener().getClass().getClassLoader();
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@ -250,6 +250,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -250,6 +250,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
*/
protected final void onEndpointException(Throwable ex) {
this.transactionDelegate.setRollbackOnly();
logger.debug("Transaction marked as rollback-only after endpoint exception", ex);
}
/**
@ -268,6 +269,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -268,6 +269,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);
}
}
@ -279,7 +281,7 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -279,7 +281,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);
}
}
}
@ -298,11 +300,10 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF @@ -298,11 +300,10 @@ public abstract class AbstractMessageEndpointFactory implements MessageEndpointF
private boolean rollbackOnly;
public TransactionDelegate(XAResource xaResource) {
if (xaResource == null) {
if (transactionFactory != null && !transactionFactory.supportsResourceAdapterManagedTransactions()) {
throw new IllegalStateException("ResourceAdapter-provided XAResource is required for " +
"transaction management. Check your ResourceAdapter's configuration.");
}
if (xaResource == null && transactionFactory != null &&
!transactionFactory.supportsResourceAdapterManagedTransactions()) {
throw new IllegalStateException("ResourceAdapter-provided XAResource is required for " +
"transaction management. Check your ResourceAdapter's configuration.");
}
this.xaResource = xaResource;
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@ -96,24 +96,21 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor @@ -96,24 +96,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;
}
@ -123,17 +120,23 @@ public class GenericMessageEndpointFactory extends AbstractMessageEndpointFactor @@ -123,17 +120,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 messageListener.getClass().getClassLoader();

Loading…
Cancel
Save