From 223b1ead93de237154a86d09d0c540c41cb8f998 Mon Sep 17 00:00:00 2001 From: Jay Boyd Date: Tue, 20 Jan 2015 12:44:25 -0500 Subject: [PATCH] Add shutdown hook for WebSphere and checkError() to detect disconnected client (issue #346) WebSphere won't shutdown a servlet until after a 60 second timeout if there is an instance of the servlet executing a request. Add a shutdown method to enable a hook to notify Hystrix to shutdown. You must invoke this method at app server shutdown, perhaps from another servlet's destroy() method. Also added explicit checkError() in poller loop to check for disconnected client. --- .../HystrixMetricsStreamServlet.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsStreamServlet.java b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsStreamServlet.java index faf07e604..267659256 100644 --- a/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsStreamServlet.java +++ b/hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsStreamServlet.java @@ -63,7 +63,21 @@ public class HystrixMetricsStreamServlet extends HttpServlet { private static AtomicInteger concurrentConnections = new AtomicInteger(0); private static DynamicIntProperty maxConcurrentConnections = DynamicPropertyFactory.getInstance().getIntProperty("hystrix.stream.maxConcurrentConnections", 5); - private volatile boolean isDestroyed = false; + private static volatile boolean isDestroyed = false; + + /** + * WebSphere won't shutdown a servlet until after a 60 second timeout if there is an instance of the servlet executing + * a request. Add this method to enable a hook to notify Hystrix to shutdown. You must invoke this method at + * shutdown, perhaps from some other serverlet's destroy() method. + */ + public static void shutdown() { + isDestroyed = true; + } + + @Override + public void init() throws ServletException { + isDestroyed = false; + } /** * Handle incoming GETs @@ -146,6 +160,11 @@ private void handleRequest(HttpServletRequest request, HttpServletResponse respo // after outputting all the messages we will flush the stream response.flushBuffer(); + // explicitly check for client disconnect - PrintWriter does not throw exceptions + if (response.getWriter().checkError()) { + throw new IOException("io error"); + } + // now wait the 'delay' time Thread.sleep(delay); }