KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > impl > WaitTimerImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.lockmanager.impl;
5
6 import com.tc.logging.TCLogger;
7 import com.tc.logging.TCLogging;
8 import com.tc.object.lockmanager.api.WaitTimer;
9 import com.tc.object.lockmanager.api.WaitTimerCallback;
10 import com.tc.object.tx.WaitInvocation;
11 import com.tc.object.tx.WaitInvocation.Signature;
12 import com.tc.util.Assert;
13 import com.tc.util.TCTimerImpl;
14
15 import java.util.Timer JavaDoc;
16 import java.util.TimerTask JavaDoc;
17
18 /**
19  * Manages timed lock waits
20  *
21  * @author teck
22  */

23 public class WaitTimerImpl implements WaitTimer {
24   private static final TCLogger logger = TCLogging.getLogger(WaitTimer.class);
25
26   private final Timer timer = new TCTimerImpl("DSO Lock Object.wait() timer", true);
27   private boolean shutdown = false;
28
29   public WaitTimerImpl() {
30     super();
31   }
32
33   public Timer getTimer() {
34     return timer;
35   }
36
37   public TimerTask JavaDoc scheduleTimer(WaitTimerCallback callback, WaitInvocation call, Object JavaDoc callbackObject) {
38     final Signature signature = call.getSignature();
39
40     if (signature == WaitInvocation.NO_ARGS) {
41       return null;
42     } else if (signature == WaitInvocation.LONG) {
43       if (call.getMillis() == 0) { return null; }
44     } else if (signature == WaitInvocation.LONG_INT) {
45       if ((call.getMillis() == 0) && (call.getNanos() == 0)) { return null; }
46     } else {
47       throw Assert.failure("unknown wait signature: " + signature);
48     }
49
50     final TimerTask JavaDoc rv = new TaskImpl(callback, call, callbackObject);
51
52     if (signature == WaitInvocation.LONG_INT) {
53       // logger.warn("Nanosecond granualarity not yet supported, adding 1ms to wait time instead");
54
timer.schedule(rv, call.getMillis() + 1);
55     } else {
56       timer.schedule(rv, call.getMillis());
57     }
58     call.mark();
59
60     return rv;
61   }
62
63   private static class TaskImpl extends TimerTask JavaDoc {
64
65     private final WaitTimerCallback callback;
66     private final Object JavaDoc callbackObject;
67     private final WaitInvocation call;
68
69     TaskImpl(WaitTimerCallback callback, WaitInvocation call, Object JavaDoc callbackObject) {
70       this.callback = callback;
71       this.call = call;
72       this.callbackObject = callbackObject;
73     }
74
75     public void run() {
76       try {
77         callback.waitTimeout(callbackObject);
78       } catch (Exception JavaDoc e) {
79         logger.error("Error processing wait timeout for " + callbackObject, e );
80       }
81     }
82
83     public boolean cancel() {
84       call.adjust();
85       return super.cancel();
86     }
87   }
88
89   public synchronized void shutdown() {
90     if (shutdown) return;
91     shutdown = true;
92     this.timer.cancel();
93   }
94
95
96 }
97
Popular Tags