Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onClosed event handler. #16

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down