1 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 ; 16 import java.util.TimerTask ; 17 18 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 scheduleTimer(WaitTimerCallback callback, WaitInvocation call, Object 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 rv = new TaskImpl(callback, call, callbackObject); 51 52 if (signature == WaitInvocation.LONG_INT) { 53 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 { 64 65 private final WaitTimerCallback callback; 66 private final Object callbackObject; 67 private final WaitInvocation call; 68 69 TaskImpl(WaitTimerCallback callback, WaitInvocation call, Object 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 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 |