-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAsyncEventSourceHandler.java
More file actions
72 lines (64 loc) · 1.98 KB
/
AsyncEventSourceHandler.java
File metadata and controls
72 lines (64 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.github.eventsource.client.impl;
import com.github.eventsource.client.EventSourceHandler;
import com.github.eventsource.client.MessageEvent;
import java.util.concurrent.Executor;
public class AsyncEventSourceHandler implements EventSourceHandler {
private final Executor executor;
private final EventSourceHandler eventSourceHandler;
public AsyncEventSourceHandler(Executor executor, EventSourceHandler eventSourceHandler) {
this.executor = executor;
this.eventSourceHandler = eventSourceHandler;
}
@Override
public void onConnect() {
executor.execute(new Runnable() {
@Override
public void run() {
try {
eventSourceHandler.onConnect();
} catch (Exception e) {
onError(e);
}
}
});
}
@Override
public void onMessage(final String event, final MessageEvent message) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
eventSourceHandler.onMessage(event, message);
} catch (Exception e) {
onError(e);
}
}
});
}
@Override
public void onError(final Throwable error) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
eventSourceHandler.onError(error);
} catch (Throwable e) {
e.printStackTrace();
}
}
});
}
@Override
public void onClosed(final boolean willReconnect) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
eventSourceHandler.onClosed(willReconnect);
} catch (Exception e) {
onError(e);
}
}
});
}
}