Skip to content

Commit e54c9d4

Browse files
committed
Fine-tuned JCA MessageEndpoint exception logging and propagation
Issue: SPR-16717 (cherry picked from commit 8e1ecec)
1 parent ac19c69 commit e54c9d4

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,6 +80,7 @@ private class JmsMessageEndpoint extends AbstractMessageEndpoint implements Mess
8080

8181
@Override
8282
public void onMessage(Message message) {
83+
Throwable endpointEx = null;
8384
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
8485
if (applyDeliveryCalls) {
8586
try {
@@ -93,10 +94,12 @@ public void onMessage(Message message) {
9394
messageListener.onMessage(message);
9495
}
9596
catch (RuntimeException ex) {
97+
endpointEx = ex;
9698
onEndpointException(ex);
9799
throw ex;
98100
}
99101
catch (Error err) {
102+
endpointEx = err;
100103
onEndpointException(err);
101104
throw err;
102105
}
@@ -106,15 +109,17 @@ public void onMessage(Message message) {
106109
afterDelivery();
107110
}
108111
catch (ResourceException ex) {
109-
throw new JmsResourceException(ex);
112+
if (endpointEx == null) {
113+
throw new JmsResourceException(ex);
114+
}
110115
}
111116
}
112117
}
113118
}
114119

115120
@Override
116121
protected ClassLoader getEndpointClassLoader() {
117-
return messageListener.getClass().getClassLoader();
122+
return getMessageListener().getClass().getClassLoader();
118123
}
119124
}
120125

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -250,6 +250,7 @@ protected final boolean hasBeforeDeliveryBeenCalled() {
250250
*/
251251
protected final void onEndpointException(Throwable ex) {
252252
this.transactionDelegate.setRollbackOnly();
253+
logger.debug("Transaction marked as rollback-only after endpoint exception", ex);
253254
}
254255

255256
/**
@@ -268,6 +269,7 @@ public void afterDelivery() throws ResourceException {
268269
this.transactionDelegate.endTransaction();
269270
}
270271
catch (Throwable ex) {
272+
logger.warn("Failed to complete transaction after endpoint delivery", ex);
271273
throw new ApplicationServerInternalException("Failed to complete transaction", ex);
272274
}
273275
}
@@ -279,7 +281,7 @@ public void release() {
279281
this.transactionDelegate.endTransaction();
280282
}
281283
catch (Throwable ex) {
282-
logger.error("Could not complete unfinished transaction on endpoint release", ex);
284+
logger.warn("Could not complete unfinished transaction on endpoint release", ex);
283285
}
284286
}
285287
}
@@ -298,11 +300,10 @@ private class TransactionDelegate {
298300
private boolean rollbackOnly;
299301

300302
public TransactionDelegate(XAResource xaResource) {
301-
if (xaResource == null) {
302-
if (transactionFactory != null && !transactionFactory.supportsResourceAdapterManagedTransactions()) {
303-
throw new IllegalStateException("ResourceAdapter-provided XAResource is required for " +
304-
"transaction management. Check your ResourceAdapter's configuration.");
305-
}
303+
if (xaResource == null && transactionFactory != null &&
304+
!transactionFactory.supportsResourceAdapterManagedTransactions()) {
305+
throw new IllegalStateException("ResourceAdapter-provided XAResource is required for " +
306+
"transaction management. Check your ResourceAdapter's configuration.");
306307
}
307308
this.xaResource = xaResource;
308309
}

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -96,24 +96,21 @@ private class GenericMessageEndpoint extends AbstractMessageEndpoint implements
9696

9797
@Override
9898
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
99+
Throwable endpointEx = null;
99100
boolean applyDeliveryCalls = !hasBeforeDeliveryBeenCalled();
100101
if (applyDeliveryCalls) {
101102
try {
102103
beforeDelivery(null);
103104
}
104105
catch (ResourceException ex) {
105-
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
106-
throw ex;
107-
}
108-
else {
109-
throw new InternalResourceException(ex);
110-
}
106+
throw adaptExceptionIfNecessary(methodInvocation, ex);
111107
}
112108
}
113109
try {
114110
return methodInvocation.proceed();
115111
}
116112
catch (Throwable ex) {
113+
endpointEx = ex;
117114
onEndpointException(ex);
118115
throw ex;
119116
}
@@ -123,17 +120,23 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
123120
afterDelivery();
124121
}
125122
catch (ResourceException ex) {
126-
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
127-
throw ex;
128-
}
129-
else {
130-
throw new InternalResourceException(ex);
123+
if (endpointEx == null) {
124+
throw adaptExceptionIfNecessary(methodInvocation, ex);
131125
}
132126
}
133127
}
134128
}
135129
}
136130

131+
private Exception adaptExceptionIfNecessary(MethodInvocation methodInvocation, ResourceException ex) {
132+
if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) {
133+
return ex;
134+
}
135+
else {
136+
return new InternalResourceException(ex);
137+
}
138+
}
139+
137140
@Override
138141
protected ClassLoader getEndpointClassLoader() {
139142
return messageListener.getClass().getClassLoader();

0 commit comments

Comments
 (0)