01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.quartz;
18:
19: import org.quartz.Job;
20: import org.quartz.JobExecutionContext;
21: import org.quartz.JobExecutionException;
22:
23: import org.springframework.util.Assert;
24:
25: /**
26: * Simple Quartz {@link org.quartz.Job} adapter that delegates to a
27: * given {@link java.lang.Runnable} instance.
28: *
29: * <p>Typically used in combination with property injection on the
30: * Runnable instance, receiving parameters from the Quartz JobDataMap
31: * that way instead of via the JobExecutionContext.
32: *
33: * @author Juergen Hoeller
34: * @since 2.0
35: * @see SpringBeanJobFactory
36: * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
37: */
38: public class DelegatingJob implements Job {
39:
40: private final Runnable delegate;
41:
42: /**
43: * Create a new DelegatingJob.
44: * @param delegate the Runnable implementation to delegate to
45: */
46: public DelegatingJob(Runnable delegate) {
47: Assert.notNull(delegate, "Delegate must not be null");
48: this .delegate = delegate;
49: }
50:
51: /**
52: * Return the wrapped Runnable implementation.
53: */
54: public final Runnable getDelegate() {
55: return this .delegate;
56: }
57:
58: /**
59: * Delegates execution to the underlying Runnable,
60: * converting any Exception thrown to a Quartz JobExecutionException
61: * (as required by the Job contract).
62: */
63: public void execute(JobExecutionContext context)
64: throws JobExecutionException {
65: try {
66: this .delegate.run();
67: } catch (Exception ex) {
68: throw new JobExecutionException(ex);
69: }
70: }
71:
72: }
|