Skip to content

Commit cdbd58c

Browse files
committed
Added additional callback methods to EventCallback
1 parent 5ad41ae commit cdbd58c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/main/java/com/github/dockerjava/api/command/EventCallback.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
*/
88
public interface EventCallback {
99
public void onEvent(Event event);
10+
public void onException(Throwable throwable);
11+
public void onCompletion(int numEvents);
1012
}

src/main/java/com/github/dockerjava/api/model/EventNotifier.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,26 @@ public static EventNotifier create(EventCallback eventCallback, WebTarget webTar
3535

3636
@Override
3737
public Void call() throws Exception {
38-
Response response = webTarget.request().get(Response.class);
39-
InputStream inputStream = response.readEntity(InputStream.class);
38+
int numEvents=0;
39+
Response response = null;
4040
try {
41+
response = webTarget.request().get(Response.class);
42+
InputStream inputStream = response.readEntity(InputStream.class);
4143
JsonParser jp = JSON_FACTORY.createParser(inputStream);
4244
while (jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed()) {
4345
eventCallback.onEvent(OBJECT_MAPPER.readValue(jp, Event.class));
46+
numEvents++;
4447
}
45-
} finally {
48+
}
49+
catch(Exception e) {
50+
eventCallback.onException(e);
51+
}
52+
finally {
4653
if (response != null) {
4754
response.close();
4855
}
4956
}
57+
eventCallback.onCompletion(numEvents);
5058
return null;
5159
}
5260
}

src/test/java/com/github/dockerjava/core/command/EventsCmdImplTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,15 @@ public void onEvent(Event event) {
110110
LOG.info("Received event #{}: {}", countDownLatch.getCount(), event);
111111
countDownLatch.countDown();
112112
}
113+
114+
@Override
115+
public void onException(Throwable throwable) {
116+
LOG.error("Error occurred: {}", throwable.getMessage());
117+
}
118+
119+
@Override
120+
public void onCompletion(int numEvents) {
121+
LOG.info("Number of events received: {}", numEvents);
122+
}
113123
}
114124
}

0 commit comments

Comments
 (0)