1 package org.apache.commons.jelly.tags.quartz;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.commons.jelly.TagSupport;
20 import org.quartz.Scheduler;
21 import org.quartz.SchedulerException;
22 import org.quartz.impl.StdSchedulerFactory;
23
24 /*** Basic support for all tags requiring a Quartz scheduler.
25 *
26 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
27 */
28 public abstract class QuartzTagSupport extends TagSupport
29 {
30 /*** The scheduler variable name in the JellyContext. */
31 public static final String SCHED_VAR_NAME = "org.apache.commons.jelly.quartz.Scheduler";
32
33
34 /*** Retrieve or create a scheduler.
35 *
36 * <p>
37 * If a scheduler has already been created an installed
38 * in the variable {@link #SCHED_VAR_NAME}, then that scheduler
39 * will be returned. Otherwise, a new StdScheduler will be
40 * created, started, and installed. Additionally, a runtime
41 * shutdown hook will be added to cleanly shutdown the scheduler.
42 *
43 * @return The scheduler.
44 *
45 * @throws SchedulerException If there is an error creating the
46 * scheduler.
47 */
48 public Scheduler getScheduler() throws SchedulerException
49 {
50 Scheduler sched = (Scheduler) getContext().getVariable( SCHED_VAR_NAME );
51
52 if ( sched == null )
53 {
54 StdSchedulerFactory factory = new StdSchedulerFactory();
55
56 final Scheduler newSched = factory.getScheduler();
57
58 sched = newSched;
59
60 getContext().setVariable( SCHED_VAR_NAME,
61 newSched );
62
63 Runtime.getRuntime().addShutdownHook(
64 new Thread() {
65 public void run()
66 {
67 try
68 {
69 if ( ! newSched.isShutdown() )
70 {
71 newSched.shutdown();
72 }
73 }
74 catch (SchedulerException e)
75 {
76 e.printStackTrace();
77 }
78 }
79 }
80 );
81 newSched.start();
82 }
83
84
85 return sched;
86 }
87 }
88