diff --git a/src/main/java/com/launchdarkly/eventsource/AsyncEventHandler.java b/src/main/java/com/launchdarkly/eventsource/AsyncEventHandler.java index da66e30..75f1e43 100644 --- a/src/main/java/com/launchdarkly/eventsource/AsyncEventHandler.java +++ b/src/main/java/com/launchdarkly/eventsource/AsyncEventHandler.java @@ -27,6 +27,18 @@ public void run() { }); } + public void onClosed() { + executor.execute(new Runnable() { + public void run() { + try { + eventSourceHandler.onClosed(); + } catch (Exception e) { + onError(e); + } + } + }); + } + public void onComment(final String comment) { executor.execute(new Runnable() { public void run() { diff --git a/src/main/java/com/launchdarkly/eventsource/EventHandler.java b/src/main/java/com/launchdarkly/eventsource/EventHandler.java index 1735ce5..820e28d 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventHandler.java +++ b/src/main/java/com/launchdarkly/eventsource/EventHandler.java @@ -2,6 +2,7 @@ public interface EventHandler { void onOpen() throws Exception; + void onClosed() throws Exception; void onMessage(String event, MessageEvent messageEvent) throws Exception; void onComment(String comment) throws Exception; void onError(Throwable t); diff --git a/src/main/java/com/launchdarkly/eventsource/EventSource.java b/src/main/java/com/launchdarkly/eventsource/EventSource.java index cebc179..e23001a 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventSource.java +++ b/src/main/java/com/launchdarkly/eventsource/EventSource.java @@ -83,6 +83,13 @@ public void close() throws IOException { if (currentState == SHUTDOWN) { return; } + if (currentState == ReadyState.OPEN) { + try { + handler.onClosed(); + } catch (Exception e) { + handler.onError(e); + } + } executor.shutdownNow(); if (client != null) { @@ -150,6 +157,13 @@ private void connect() { if (call != null) { call.cancel(); } + if (currentState == ReadyState.OPEN) { + try { + handler.onClosed(); + } catch (Exception e) { + handler.onError(e); + } + } } } } catch (RejectedExecutionException ignored) {