Skip to content

Commit

Permalink
Merge pull request #16 from ShaneMcC/onClosed
Browse files Browse the repository at this point in the history
Add onClosed event handler.
  • Loading branch information
jkodumal authored Jan 25, 2017
2 parents ab02c93 + 8195085 commit 1f09ffc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/launchdarkly/eventsource/AsyncEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/launchdarkly/eventsource/EventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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) {
Expand Down Expand Up @@ -154,6 +161,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) {
Expand Down

0 comments on commit 1f09ffc

Please sign in to comment.