changeset 1443:42c07ecb0ddc

DomainHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 28 Jan 2020 01:16:31 -0700
parents bbc41b305935
children b765f146f4dc
files src/goodjava/webserver/handlers/DomainHandler.java src/luan/modules/ThreadLuan.java
diffstat 2 files changed, 37 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
diff -r bbc41b305935 -r 42c07ecb0ddc src/goodjava/webserver/handlers/DomainHandler.java
--- a/src/goodjava/webserver/handlers/DomainHandler.java	Sat Jan 25 11:27:21 2020 -0500
+++ b/src/goodjava/webserver/handlers/DomainHandler.java	Tue Jan 28 01:16:31 2020 -0700
@@ -8,6 +8,10 @@
 import java.lang.ref.ReferenceQueue;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.ConcurrentHashMap;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 import goodjava.webserver.Handler;
@@ -22,22 +26,33 @@
 		public Handler newHandler(String domain);
 	}
 
-	private static void close(Handler handler) {
-		if( handler instanceof Closeable ) {
-			try {
-				((Closeable)handler).close();
-			} catch(IOException e) {
-				logger.error(handler.toString(),e);
-			}
+	private static class MyTask extends TimerTask {
+		private final Set<Handler> dontGc;
+
+		MyTask(Set<Handler> dontGc) {
+			this.dontGc = dontGc;
+		}
+
+		public void run() {
+			dontGc.clear();
+			logger.info("dontGc.clear()");
 		}
 	}
 
+	private static final long HOUR = 1000L*60*60;
 	private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>();
+	private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
+	private final Timer timer = new Timer();
 
 	private final Factory factory;
 
 	public DomainHandler(Factory factory) {
 		this.factory = factory;
+		timer.schedule(new MyTask(dontGc),HOUR,HOUR);
+	}
+
+	protected void finalize() throws Throwable {
+		timer.cancel();
 	}
 
 	public Response handle(Request request) {
@@ -47,7 +62,10 @@
 		int i = host.indexOf(':');
 		String domain = i == -1 ? host : host.substring(0,i);
 		Handler handler = getHandler(domain);
-		return handler==null ? null : handler.handle(request);
+		if( handler==null )
+			return null;
+		dontGc.add(handler);
+		return handler.handle(request);
 	}
 
 	public Handler getHandler(String domain) {
@@ -78,4 +96,14 @@
 		}
 	}
 
+	private static void close(Handler handler) {
+		if( handler instanceof Closeable ) {
+			try {
+				((Closeable)handler).close();
+			} catch(IOException e) {
+				logger.error(handler.toString(),e);
+			}
+		}
+	}
+
 }
diff -r bbc41b305935 -r 42c07ecb0ddc src/luan/modules/ThreadLuan.java
--- a/src/luan/modules/ThreadLuan.java	Sat Jan 25 11:27:21 2020 -0500
+++ b/src/luan/modules/ThreadLuan.java	Tue Jan 28 01:16:31 2020 -0700
@@ -56,7 +56,7 @@
 
 	private static void cancel(ScheduledFuture sf,String src) {
 		boolean b = sf.cancel(false);
-//		if( !b )
+		if( !sf.isCancelled() )
 			logger.error(src+" cancel="+b+" isCancelled="+sf.isCancelled()+" isDone="+sf.isDone()+" "+sf);
 	}