jdk/src/share/classes/javax/management/event/FetchingEventRelay.java
changeset 1229 8d219f46ab69
parent 1004 5ba8217eb504
child 1247 b4c26443dee5
--- a/jdk/src/share/classes/javax/management/event/FetchingEventRelay.java	Thu Sep 11 14:58:57 2008 -0700
+++ b/jdk/src/share/classes/javax/management/event/FetchingEventRelay.java	Fri Sep 12 15:17:52 2008 +0200
@@ -31,10 +31,8 @@
 import java.io.IOException;
 import java.io.NotSerializableException;
 import java.util.concurrent.Executor;
-import java.util.concurrent.Executors;
-import java.util.concurrent.RejectedExecutionException;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 import javax.management.MBeanException;
@@ -215,50 +213,47 @@
         this.maxNotifs = maxNotifs;
 
         if (executor == null) {
-            executor = Executors.newSingleThreadScheduledExecutor(
+            ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1,
                     daemonThreadFactory);
-        }
+            stpe.setKeepAliveTime(1, TimeUnit.SECONDS);
+            stpe.allowCoreThreadTimeOut(true);
+            executor = stpe;
+            this.defaultExecutor = stpe;
+        } else
+            this.defaultExecutor = null;
         this.executor = executor;
-        if (executor instanceof ScheduledExecutorService)
-            leaseScheduler = (ScheduledExecutorService) executor;
-        else {
-            leaseScheduler = Executors.newSingleThreadScheduledExecutor(
-                    daemonThreadFactory);
-        }
 
         startSequenceNumber = 0;
         fetchingJob = new MyJob();
     }
 
-    public void setEventReceiver(EventReceiver eventReceiver) {
+    public synchronized void setEventReceiver(EventReceiver eventReceiver) {
         if (logger.traceOn()) {
             logger.trace("setEventReceiver", ""+eventReceiver);
         }
 
         EventReceiver old = this.eventReceiver;
-        synchronized(fetchingJob) {
-            this.eventReceiver = eventReceiver;
-            if (old == null && eventReceiver != null)
-                fetchingJob.resume();
-        }
+        this.eventReceiver = eventReceiver;
+        if (old == null && eventReceiver != null)
+            fetchingJob.resume();
     }
 
     public String getClientId() {
         return clientId;
     }
 
-    public void stop() {
+    public synchronized void stop() {
         if (logger.traceOn()) {
             logger.trace("stop", "");
         }
-        synchronized(fetchingJob) {
-            if (stopped) {
-                return;
-            }
+        if (stopped) {
+            return;
+        }
 
-            stopped = true;
-            clientId = null;
-        }
+        stopped = true;
+        clientId = null;
+        if (defaultExecutor != null)
+            defaultExecutor.shutdown();
     }
 
     private class MyJob extends RepeatedSingletonJob {
@@ -372,10 +367,9 @@
     private final EventClientDelegateMBean delegate;
     private String clientId;
     private boolean stopped = false;
-    private volatile ScheduledFuture<?> leaseRenewalFuture;
 
     private final Executor executor;
-    private final ScheduledExecutorService leaseScheduler;
+    private final ExecutorService defaultExecutor;
     private final MyJob fetchingJob;
 
     private final long timeout;
@@ -385,5 +379,5 @@
             new ClassLogger("javax.management.event",
             "FetchingEventRelay");
     private static final ThreadFactory daemonThreadFactory =
-                    new DaemonThreadFactory("FetchingEventRelay-executor");
+                    new DaemonThreadFactory("JMX FetchingEventRelay executor %d");
 }