jdk/src/share/classes/com/sun/jmx/event/LeaseRenewer.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     1 /*
       
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.jmx.event;
       
    27 
       
    28 import com.sun.jmx.remote.util.ClassLogger;
       
    29 import java.util.concurrent.Callable;
       
    30 import java.util.concurrent.ScheduledExecutorService;
       
    31 import java.util.concurrent.ScheduledFuture;
       
    32 import java.util.concurrent.TimeUnit;
       
    33 
       
    34 /**
       
    35  *
       
    36  * @author sjiang
       
    37  */
       
    38 public class LeaseRenewer {
       
    39     public LeaseRenewer(ScheduledExecutorService scheduler, Callable<Long> doRenew) {
       
    40         if (logger.traceOn()) {
       
    41             logger.trace("LeaseRenewer", "New LeaseRenewer.");
       
    42         }
       
    43 
       
    44         if (doRenew == null) {
       
    45             throw new NullPointerException("Null job to call server.");
       
    46         }
       
    47 
       
    48         this.doRenew = doRenew;
       
    49         nextRenewTime = System.currentTimeMillis();
       
    50 
       
    51         this.scheduler = scheduler;
       
    52         future = this.scheduler.schedule(myRenew, 0, TimeUnit.MILLISECONDS);
       
    53     }
       
    54 
       
    55     public void close() {
       
    56         if (logger.traceOn()) {
       
    57             logger.trace("close", "Close the lease.");
       
    58         }
       
    59 
       
    60         synchronized(lock) {
       
    61             if (closed) {
       
    62                 return;
       
    63             } else {
       
    64                 closed = true;
       
    65             }
       
    66         }
       
    67 
       
    68         try {
       
    69             future.cancel(false); // not interrupt if running
       
    70         } catch (Exception e) {
       
    71             // OK
       
    72             if (logger.debugOn()) {
       
    73                 logger.debug("close", "Failed to cancel the leasing job.", e);
       
    74             }
       
    75         }
       
    76     }
       
    77 
       
    78     public boolean closed() {
       
    79         synchronized(lock) {
       
    80             return closed;
       
    81         }
       
    82     }
       
    83 
       
    84     // ------------------------------
       
    85     // private
       
    86     // ------------------------------
       
    87     private final Runnable myRenew = new Runnable() {
       
    88         public void run() {
       
    89             synchronized(lock) {
       
    90                 if (closed()) {
       
    91                     return;
       
    92                 }
       
    93             }
       
    94 
       
    95             long next = nextRenewTime - System.currentTimeMillis();
       
    96             if (next < MIN_MILLIS) {
       
    97                 try {
       
    98                     if (logger.traceOn()) {
       
    99                         logger.trace("myRenew-run", "");
       
   100                     }
       
   101                     next = doRenew.call().longValue();
       
   102 
       
   103                 } catch (Exception e) {
       
   104                     logger.fine("myRenew-run", "Failed to renew lease", e);
       
   105                     close();
       
   106                 }
       
   107 
       
   108                 if (next > 0 && next < Long.MAX_VALUE) {
       
   109                     next = next/2;
       
   110                     next = (next < MIN_MILLIS) ? MIN_MILLIS : next;
       
   111                 } else {
       
   112                     close();
       
   113                 }
       
   114             }
       
   115 
       
   116             nextRenewTime = System.currentTimeMillis() + next;
       
   117 
       
   118             if (logger.traceOn()) {
       
   119                 logger.trace("myRenew-run", "Next leasing: "+next);
       
   120             }
       
   121 
       
   122             synchronized(lock) {
       
   123                 if (!closed) {
       
   124                     future = scheduler.schedule(this, next, TimeUnit.MILLISECONDS);
       
   125                 }
       
   126             }
       
   127         }
       
   128     };
       
   129 
       
   130     private final Callable<Long> doRenew;
       
   131     private ScheduledFuture<?> future;
       
   132     private boolean closed = false;
       
   133     private long nextRenewTime;
       
   134 
       
   135     private final int[] lock = new int[0];
       
   136 
       
   137     private final ScheduledExecutorService scheduler;
       
   138 
       
   139     private static final long MIN_MILLIS = 50;
       
   140 
       
   141     private static final ClassLogger logger =
       
   142             new ClassLogger("javax.management.event", "LeaseRenewer");
       
   143 }