Skip to content

Commit 0e899db

Browse files
author
Marcus Linke
committed
Merge branch 'events-callback' of
https://github.com/sabre1041/docker-java into sabre1041-events-callback Conflicts: src/main/java/com/github/dockerjava/api/model/EventNotifier.java
2 parents ce770cf + cdbd58c commit 0e899db

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

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/jaxrs/EventsCmdExec.java

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

6262
@Override
6363
public Void call() throws Exception {
64-
Response response = webTarget.request().get(Response.class);
65-
InputStream inputStream = response.readEntity(InputStream.class);
64+
int numEvents=0;
65+
Response response = null;
6666
try {
67+
response = webTarget.request().get(Response.class);
68+
InputStream inputStream = response.readEntity(InputStream.class);
6769
JsonParser jp = JSON_FACTORY.createParser(inputStream);
6870
while (jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed()) {
6971
eventCallback.onEvent(OBJECT_MAPPER.readValue(jp, Event.class));
72+
numEvents++;
7073
}
71-
} finally {
74+
}
75+
catch(Exception e) {
76+
eventCallback.onException(e);
77+
}
78+
finally {
7279
if (response != null) {
7380
response.close();
7481
}
7582
}
83+
eventCallback.onCompletion(numEvents);
7684
return null;
7785
}
7886
}

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

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

0 commit comments

Comments
 (0)