01: /*
02:
03: Derby - Class org.apache.derby.impl.services.timer.SingletonTimerFactory
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to you under the Apache License, Version 2.0
09: (the "License"); you may not use this file except in compliance with
10: the License. You may obtain a copy of the License at
11:
12: http://www.apache.org/licenses/LICENSE-2.0
13:
14: Unless required by applicable law or agreed to in writing, software
15: distributed under the License is distributed on an "AS IS" BASIS,
16: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: See the License for the specific language governing permissions and
18: limitations under the License.
19:
20: */
21:
22: package org.apache.derby.impl.services.timer;
23:
24: import org.apache.derby.iapi.services.timer.TimerFactory;
25: import org.apache.derby.iapi.services.monitor.ModuleControl;
26:
27: import org.apache.derby.iapi.error.StandardException;
28:
29: import java.util.Timer;
30: import java.util.Properties;
31:
32: /**
33: * This class implements the TimerFactory interface.
34: * It creates a singleton Timer instance.
35: *
36: * The class implements the ModuleControl interface,
37: * because it needs to cancel the Timer at system shutdown.
38: *
39: * @see TimerFactory
40: * @see ModuleControl
41: */
42: public class SingletonTimerFactory implements TimerFactory,
43: ModuleControl {
44: /**
45: * Singleton Timer instance.
46: */
47: private Timer singletonTimer;
48:
49: /**
50: * Initializes this TimerFactory with a singleton Timer instance.
51: */
52: public SingletonTimerFactory() {
53: /**
54: * Even though we implement the ModuleControl interface,
55: * we initialize the object here rather than in boot, since
56: * a) We avoid synchronizing access to singletonTimer later
57: * b) We don't need any properties
58: */
59: singletonTimer = new Timer(true); // Run as daemon
60: }
61:
62: /**
63: * Returns a Timer object that can be used for adding TimerTasks
64: * that cancel executing statements.
65: *
66: * Implements the TimerFactory interface.
67: *
68: * @return a Timer object for cancelling statements.
69: *
70: * @see TimerFactory
71: */
72: public Timer getCancellationTimer() {
73: return singletonTimer;
74: }
75:
76: /**
77: * Currently does nothing, singleton Timer instance is initialized
78: * in the constructor.
79: *
80: * Implements the ModuleControl interface.
81: *
82: * @see ModuleControl
83: */
84: public void boot(boolean create, Properties properties)
85: throws StandardException {
86: // Do nothing, instance already initialized in constructor
87: }
88:
89: /**
90: * Cancels the singleton Timer instance.
91: *
92: * Implements the ModuleControl interface.
93: *
94: * @see ModuleControl
95: */
96: public void stop() {
97: singletonTimer.cancel();
98: }
99: }
|