Skip to content

Commit dac0be3

Browse files
author
Marcus Linke
committed
Modify stats implementation according to events command
1 parent 09f4f85 commit dac0be3

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public Void call() throws Exception {
7979
// event from the docker server or the connection is terminated.
8080
// therefore we want to check before getting an event (to prevent a blocking operation
8181
// and after the event to make sure that the eventCallback is still interested in getting notified.
82-
while (eventCallback.isReceiving() &&
82+
while (eventCallback.isReceiving() &&
8383
jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed() &&
8484
eventCallback.isReceiving()) {
8585
try {
@@ -93,15 +93,14 @@ public Void call() throws Exception {
9393
} catch (Exception e) {
9494
eventCallback.onException(e);
9595
} finally {
96-
// call onCompletion before close because of https://github.com/docker-java/docker-java/issues/196
96+
if (response != null) {
97+
response.close();
98+
}
9799
try {
98100
eventCallback.onCompletion(numEvents);
99101
} catch (Exception e) {
100102
eventCallback.onException(e);
101103
}
102-
if (response != null) {
103-
response.close();
104-
}
105104
}
106105

107106
return null;

src/main/java/com/github/dockerjava/jaxrs/StatsCmdExec.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424

2525
public class StatsCmdExec extends AbstrDockerCmdExec<StatsCmd, ExecutorService> implements StatsCmd.Exec {
2626
private static final Logger LOGGER = LoggerFactory.getLogger(StatsCmdExec.class);
27-
27+
2828
public StatsCmdExec(WebTarget baseResource) {
2929
super(baseResource);
3030
}
3131

3232
@Override
3333
protected ExecutorService execute(StatsCmd command) {
3434
ExecutorService executorService = Executors.newSingleThreadExecutor();
35-
35+
3636
WebTarget webResource = getBaseResource().path("/containers/{id}/stats")
3737
.resolveTemplate("id", command.getContainerId());
3838

@@ -41,7 +41,7 @@ protected ExecutorService execute(StatsCmd command) {
4141
executorService.submit(eventNotifier);
4242
return executorService;
4343
}
44-
44+
4545
private static class StatsNotifier implements Callable<Void> {
4646
private static final JsonFactory JSON_FACTORY = new JsonFactory();
4747
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@@ -62,29 +62,42 @@ public static StatsNotifier create(StatsCallback statsCallback, WebTarget webTar
6262

6363
@Override
6464
public Void call() throws Exception {
65-
int numStats=0;
65+
int numEvents = 0;
6666
Response response = null;
6767
try {
6868
response = webTarget.request().get(Response.class);
69-
InputStream inputStream = new WrappedResponseInputStream(response);
69+
InputStream inputStream = new WrappedResponseInputStream(
70+
response);
7071
JsonParser jp = JSON_FACTORY.createParser(inputStream);
71-
while (jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed() && statsCallback.isReceiving()) {
72-
statsCallback.onStats(OBJECT_MAPPER.readValue(jp, Statistics.class));
73-
numStats++;
72+
// The following condition looks strange but jp.nextToken() will block until there is an
73+
// event from the docker server or the connection is terminated.
74+
// therefore we want to check before getting an event (to prevent a blocking operation
75+
// and after the event to make sure that the eventCallback is still interested in getting notified.
76+
while (statsCallback.isReceiving() &&
77+
jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed() &&
78+
statsCallback.isReceiving()) {
79+
try {
80+
statsCallback.onStats(OBJECT_MAPPER.readValue(jp,
81+
Statistics.class));
82+
} catch (Exception e) {
83+
statsCallback.onException(e);
84+
}
85+
numEvents++;
7486
}
75-
statsCallback.onCompletion(numStats);
76-
LOGGER.info("Finished collecting stats");
77-
return null ;
78-
}
79-
catch(Throwable t) {
80-
statsCallback.onException(t);
81-
}
82-
finally {
87+
} catch (Exception e) {
88+
statsCallback.onException(e);
89+
} finally {
8390
if (response != null) {
8491
response.close();
8592
}
93+
try {
94+
statsCallback.onCompletion(numEvents);
95+
} catch (Exception e) {
96+
statsCallback.onException(e);
97+
}
8698
}
87-
return null ;
99+
100+
return null;
88101
}
89102
}
90103
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testStatsStreaming() throws InterruptedException, IOException {
5757

5858
CountDownLatch countDownLatch = new CountDownLatch(NUM_STATS);
5959
StatsCallbackTest statsCallback = new StatsCallbackTest(countDownLatch);
60-
60+
6161
String containerName = "generated_" + new SecureRandom().nextInt();
6262

6363
CreateContainerResponse container = dockerClient
@@ -74,15 +74,15 @@ public void testStatsStreaming() throws InterruptedException, IOException {
7474

7575
countDownLatch.await(3, TimeUnit.SECONDS);
7676
boolean gotStats = statsCallback.gotStats();
77-
77+
7878
LOG.info("Stop stats collection");
7979
executorService.shutdown();
8080
statsCallback.close();
8181

8282
LOG.info("Stopping container");
8383
dockerClient.stopContainerCmd(container.getId()).exec();
8484
dockerClient.removeContainerCmd(container.getId()).exec();
85-
85+
8686
LOG.info("Completed test");
8787
assertTrue(gotStats, "Expected true");
8888

@@ -125,7 +125,7 @@ public void onCompletion(int numStats) {
125125
public boolean isReceiving() {
126126
return isReceiving.get();
127127
}
128-
128+
129129
public boolean gotStats() {
130130
return gotStats;
131131
}

0 commit comments

Comments
 (0)