Skip to content

Commit 0d70e08

Browse files
committed
exceptions thrown by @scheduled methods will be propagated to a registered ErrorHandler (SPR-7723)
1 parent 0319095 commit 0d70e08

File tree

6 files changed

+135
-72
lines changed

6 files changed

+135
-72
lines changed

org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -31,7 +31,7 @@
3131
import org.springframework.core.Ordered;
3232
import org.springframework.core.annotation.AnnotationUtils;
3333
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
34-
import org.springframework.scheduling.support.MethodInvokingRunnable;
34+
import org.springframework.scheduling.support.ScheduledMethodRunnable;
3535
import org.springframework.util.Assert;
3636
import org.springframework.util.ReflectionUtils;
3737
import org.springframework.util.ReflectionUtils.MethodCallback;
@@ -103,16 +103,7 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
103103
"Only void-returning methods may be annotated with @Scheduled.");
104104
Assert.isTrue(method.getParameterTypes().length == 0,
105105
"Only no-arg methods may be annotated with @Scheduled.");
106-
MethodInvokingRunnable runnable = new MethodInvokingRunnable();
107-
runnable.setTargetObject(bean);
108-
runnable.setTargetMethod(method.getName());
109-
runnable.setArguments(new Object[0]);
110-
try {
111-
runnable.prepare();
112-
}
113-
catch (Exception ex) {
114-
throw new IllegalStateException("failed to prepare task", ex);
115-
}
106+
Runnable runnable = new ScheduledMethodRunnable(bean, method);
116107
boolean processedSchedule = false;
117108
String errorMessage = "Exactly one of 'cron', 'fixedDelay', or 'fixedRate' is required.";
118109
String cron = annotation.cron();

org.springframework.context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package org.springframework.scheduling.config;
1818

19+
import org.w3c.dom.Element;
20+
import org.w3c.dom.Node;
21+
import org.w3c.dom.NodeList;
22+
1923
import org.springframework.beans.factory.config.RuntimeBeanReference;
2024
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
2125
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2226
import org.springframework.beans.factory.support.ManagedMap;
2327
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
2428
import org.springframework.beans.factory.xml.ParserContext;
2529
import org.springframework.util.StringUtils;
26-
import org.w3c.dom.Element;
27-
import org.w3c.dom.Node;
28-
import org.w3c.dom.NodeList;
2930

3031
/**
3132
* Parser for the 'scheduled-tasks' element of the scheduling namespace.
@@ -70,7 +71,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
7071
}
7172

7273
RuntimeBeanReference runnableBeanRef = new RuntimeBeanReference(
73-
this.createRunnableBean(ref, method, taskElement, parserContext));
74+
createRunnableBean(ref, method, taskElement, parserContext));
7475
String cronAttribute = taskElement.getAttribute("cron");
7576
if (StringUtils.hasText(cronAttribute)) {
7677
cronTaskMap.put(runnableBeanRef, cronAttribute);
@@ -108,9 +109,9 @@ private boolean isScheduledElement(Node node, ParserContext parserContext) {
108109

109110
private String createRunnableBean(String ref, String method, Element taskElement, ParserContext parserContext) {
110111
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
111-
"org.springframework.scheduling.support.MethodInvokingRunnable");
112-
builder.addPropertyReference("targetObject", ref);
113-
builder.addPropertyValue("targetMethod", method);
112+
"org.springframework.scheduling.support.ScheduledMethodRunnable");
113+
builder.addConstructorArgReference(ref);
114+
builder.addConstructorArgValue(method);
114115
// Extract the source of the current task
115116
builder.getRawBeanDefinition().setSource(parserContext.extractSource(taskElement));
116117
String generatedName = parserContext.getReaderContext().generateBeanName(builder.getRawBeanDefinition());

org.springframework.context/src/main/java/org/springframework/scheduling/support/DelegatingErrorHandlingRunnable.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.scheduling.support;
1818

19+
import java.lang.reflect.UndeclaredThrowableException;
20+
1921
import org.springframework.util.Assert;
2022
import org.springframework.util.ErrorHandler;
2123

@@ -50,6 +52,9 @@ public void run() {
5052
try {
5153
this.delegate.run();
5254
}
55+
catch (UndeclaredThrowableException ex) {
56+
this.errorHandler.handleError(ex.getUndeclaredThrowable());
57+
}
5358
catch (Throwable ex) {
5459
this.errorHandler.handleError(ex);
5560
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.support;
18+
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
21+
import java.lang.reflect.UndeclaredThrowableException;
22+
23+
import org.springframework.util.ReflectionUtils;
24+
25+
/**
26+
* Variation of {@link MethodInvokingRunnable} meant to be used for processing
27+
* of no-arg scheduled methods. Propagates user exceptions to the caller,
28+
* assuming that an error strategy for Runnables is in place.
29+
*
30+
* @author Juergen Hoeller
31+
* @since 3.0.6
32+
*/
33+
public class ScheduledMethodRunnable implements Runnable {
34+
35+
private final Object target;
36+
37+
private final Method method;
38+
39+
40+
public ScheduledMethodRunnable(Object target, Method method) {
41+
this.target = target;
42+
this.method = method;
43+
}
44+
45+
public ScheduledMethodRunnable(Object target, String methodName) throws NoSuchMethodException {
46+
this.target = target;
47+
this.method = target.getClass().getMethod(methodName);
48+
}
49+
50+
51+
public Object getTarget() {
52+
return this.target;
53+
}
54+
55+
public Method getMethod() {
56+
return this.method;
57+
}
58+
59+
60+
public void run() {
61+
try {
62+
this.method.invoke(this.target);
63+
}
64+
catch (InvocationTargetException ex) {
65+
ReflectionUtils.rethrowRuntimeException(ex.getTargetException());
66+
}
67+
catch (IllegalAccessException ex) {
68+
throw new UndeclaredThrowableException(ex);
69+
}
70+
}
71+
72+
}

org.springframework.context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -16,15 +16,16 @@
1616

1717
package org.springframework.scheduling.annotation;
1818

19-
import static org.junit.Assert.assertEquals;
20-
19+
import java.io.IOException;
2120
import java.lang.annotation.ElementType;
2221
import java.lang.annotation.Retention;
2322
import java.lang.annotation.RetentionPolicy;
2423
import java.lang.annotation.Target;
24+
import java.lang.reflect.Method;
2525
import java.util.Map;
2626
import java.util.Properties;
2727

28+
import static org.junit.Assert.*;
2829
import org.junit.Test;
2930

3031
import org.springframework.beans.DirectFieldAccessor;
@@ -34,12 +35,12 @@
3435
import org.springframework.beans.factory.support.RootBeanDefinition;
3536
import org.springframework.context.support.StaticApplicationContext;
3637
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
37-
import org.springframework.scheduling.support.MethodInvokingRunnable;
38+
import org.springframework.scheduling.support.ScheduledMethodRunnable;
3839

3940
/**
4041
* @author Mark Fisher
42+
* @author Juergen Hoeller
4143
*/
42-
@SuppressWarnings({"unchecked", "unused"})
4344
public class ScheduledAnnotationBeanPostProcessorTests {
4445

4546
@Test
@@ -58,11 +59,11 @@ public void fixedDelayTask() {
5859
Map<Runnable, Long> fixedDelayTasks = (Map<Runnable, Long>)
5960
new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
6061
assertEquals(1, fixedDelayTasks.size());
61-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) fixedDelayTasks.keySet().iterator().next();
62-
Object targetObject = runnable.getTargetObject();
63-
String targetMethod = runnable.getTargetMethod();
62+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) fixedDelayTasks.keySet().iterator().next();
63+
Object targetObject = runnable.getTarget();
64+
Method targetMethod = runnable.getMethod();
6465
assertEquals(target, targetObject);
65-
assertEquals("fixedDelay", targetMethod);
66+
assertEquals("fixedDelay", targetMethod.getName());
6667
assertEquals(new Long(5000), fixedDelayTasks.values().iterator().next());
6768
}
6869

@@ -82,16 +83,16 @@ public void fixedRateTask() {
8283
Map<Runnable, Long> fixedRateTasks = (Map<Runnable, Long>)
8384
new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
8485
assertEquals(1, fixedRateTasks.size());
85-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) fixedRateTasks.keySet().iterator().next();
86-
Object targetObject = runnable.getTargetObject();
87-
String targetMethod = runnable.getTargetMethod();
86+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) fixedRateTasks.keySet().iterator().next();
87+
Object targetObject = runnable.getTarget();
88+
Method targetMethod = runnable.getMethod();
8889
assertEquals(target, targetObject);
89-
assertEquals("fixedRate", targetMethod);
90+
assertEquals("fixedRate", targetMethod.getName());
9091
assertEquals(new Long(3000), fixedRateTasks.values().iterator().next());
9192
}
9293

9394
@Test
94-
public void cronTask() {
95+
public void cronTask() throws InterruptedException {
9596
StaticApplicationContext context = new StaticApplicationContext();
9697
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
9798
BeanDefinition targetDefinition = new RootBeanDefinition(
@@ -106,12 +107,13 @@ public void cronTask() {
106107
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
107108
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
108109
assertEquals(1, cronTasks.size());
109-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
110-
Object targetObject = runnable.getTargetObject();
111-
String targetMethod = runnable.getTargetMethod();
110+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTasks.keySet().iterator().next();
111+
Object targetObject = runnable.getTarget();
112+
Method targetMethod = runnable.getMethod();
112113
assertEquals(target, targetObject);
113-
assertEquals("cron", targetMethod);
114+
assertEquals("cron", targetMethod.getName());
114115
assertEquals("*/7 * * * * ?", cronTasks.values().iterator().next());
116+
Thread.sleep(10000);
115117
}
116118

117119
@Test
@@ -130,11 +132,11 @@ public void metaAnnotationWithFixedRate() {
130132
Map<Runnable, Long> fixedRateTasks = (Map<Runnable, Long>)
131133
new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
132134
assertEquals(1, fixedRateTasks.size());
133-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) fixedRateTasks.keySet().iterator().next();
134-
Object targetObject = runnable.getTargetObject();
135-
String targetMethod = runnable.getTargetMethod();
135+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) fixedRateTasks.keySet().iterator().next();
136+
Object targetObject = runnable.getTarget();
137+
Method targetMethod = runnable.getMethod();
136138
assertEquals(target, targetObject);
137-
assertEquals("checkForUpdates", targetMethod);
139+
assertEquals("checkForUpdates", targetMethod.getName());
138140
assertEquals(new Long(5000), fixedRateTasks.values().iterator().next());
139141
}
140142

@@ -154,11 +156,11 @@ public void metaAnnotationWithCronExpression() {
154156
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
155157
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
156158
assertEquals(1, cronTasks.size());
157-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
158-
Object targetObject = runnable.getTargetObject();
159-
String targetMethod = runnable.getTargetMethod();
159+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTasks.keySet().iterator().next();
160+
Object targetObject = runnable.getTarget();
161+
Method targetMethod = runnable.getMethod();
160162
assertEquals(target, targetObject);
161-
assertEquals("generateReport", targetMethod);
163+
assertEquals("generateReport", targetMethod.getName());
162164
assertEquals("0 0 * * * ?", cronTasks.values().iterator().next());
163165
}
164166

@@ -184,11 +186,11 @@ public void propertyPlaceholderWithCronExpression() {
184186
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
185187
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
186188
assertEquals(1, cronTasks.size());
187-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
188-
Object targetObject = runnable.getTargetObject();
189-
String targetMethod = runnable.getTargetMethod();
189+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTasks.keySet().iterator().next();
190+
Object targetObject = runnable.getTarget();
191+
Method targetMethod = runnable.getMethod();
190192
assertEquals(target, targetObject);
191-
assertEquals("x", targetMethod);
193+
assertEquals("x", targetMethod.getName());
192194
assertEquals(businessHoursCronExpression, cronTasks.values().iterator().next());
193195
}
194196

@@ -214,11 +216,11 @@ public void propertyPlaceholderForMetaAnnotation() {
214216
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
215217
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
216218
assertEquals(1, cronTasks.size());
217-
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
218-
Object targetObject = runnable.getTargetObject();
219-
String targetMethod = runnable.getTargetMethod();
219+
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTasks.keySet().iterator().next();
220+
Object targetObject = runnable.getTarget();
221+
Method targetMethod = runnable.getMethod();
220222
assertEquals(target, targetObject);
221-
assertEquals("y", targetMethod);
223+
assertEquals("y", targetMethod.getName());
222224
assertEquals(businessHoursCronExpression, cronTasks.values().iterator().next());
223225
}
224226

@@ -267,28 +269,27 @@ public void nonEmptyParamList() {
267269
}
268270

269271

270-
private static class FixedDelayTestBean {
272+
public static class FixedDelayTestBean {
271273

272274
@Scheduled(fixedDelay=5000)
273275
public void fixedDelay() {
274276
}
275-
276277
}
277278

278279

279-
private static class FixedRateTestBean {
280+
public static class FixedRateTestBean {
280281

281282
@Scheduled(fixedRate=3000)
282283
public void fixedRate() {
283284
}
284-
285285
}
286286

287287

288-
private static class CronTestBean {
288+
public static class CronTestBean {
289289

290290
@Scheduled(cron="*/7 * * * * ?")
291-
public void cron() {
291+
public void cron() throws IOException {
292+
throw new IOException("no no no");
292293
}
293294

294295
}

0 commit comments

Comments
 (0)