Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41e74b3

Browse files
test docker java api
1 parent 57bb8d1 commit 41e74b3

File tree

3 files changed

+107
-11
lines changed

3 files changed

+107
-11
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package demo;
2+
3+
import com.github.dockerjava.api.async.ResultCallback;
4+
import java.io.Closeable;
5+
import java.io.IOException;
6+
import java.util.Arrays;
7+
import java.util.concurrent.CountDownLatch;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
/**
12+
* @GitHub : https://github.com/zacscoding
13+
*/
14+
public class NoStreamCallback<A_RES_T> implements ResultCallback<A_RES_T> {
15+
16+
private static final Logger LOGGER = LoggerFactory.getLogger(NoStreamCallback.class);
17+
18+
private Closeable stream;
19+
private boolean closed = false;
20+
private A_RES_T object;
21+
private Throwable firstError = null;
22+
private final CountDownLatch next = new CountDownLatch(1);
23+
24+
25+
@Override
26+
public void onStart(Closeable stream) {
27+
this.stream = stream;
28+
this.closed = false;
29+
}
30+
31+
@Override
32+
public void onNext(A_RES_T object) {
33+
this.object = object;
34+
next.countDown();
35+
onComplete();
36+
}
37+
38+
@Override
39+
public void onError(Throwable throwable) {
40+
if (closed) {
41+
return;
42+
}
43+
44+
if (firstError == null) {
45+
firstError = throwable;
46+
}
47+
48+
try {
49+
LOGGER.error("Error during callback", throwable);
50+
} finally {
51+
try {
52+
close();
53+
} catch (IOException e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
}
58+
59+
@Override
60+
public void onComplete() {
61+
try {
62+
LOGGER.info("onComplete is called..");
63+
close();
64+
} catch (IOException e) {
65+
throw new RuntimeException(e);
66+
}
67+
}
68+
69+
@Override
70+
public void close() throws IOException {
71+
if (!closed) {
72+
closed = true;
73+
if (stream != null) {
74+
LOGGER.info("Try to close stream");
75+
stream.close();
76+
}
77+
}
78+
}
79+
80+
public A_RES_T awaitNext() throws InterruptedException {
81+
next.await();
82+
return object;
83+
}
84+
}

‎java-docker-demo/src/test/java/docker/ConnectTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.dockerjava.api.DockerClient;
44
import com.github.dockerjava.api.model.Container;
55
import demo.DockerClientHelper;
6+
import java.util.Arrays;
67
import java.util.List;
78
import org.junit.Test;
89

@@ -18,8 +19,8 @@ public void connect() throws Exception {
1819
.withStatusFilter("exited").exec();
1920

2021
for (Container container : containers) {
21-
System.out.println(container);
22-
System.out.println(container.getNames());
22+
System.out.println(container.getId());
23+
System.out.println(Arrays.toString(container.getNames()));
2324
}
2425

2526
dockerClient.close();
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package docker;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.github.dockerjava.api.DockerClient;
45
import com.github.dockerjava.api.model.Container;
5-
import com.google.common.base.Strings;
6+
import com.github.dockerjava.api.model.Statistics;
67
import demo.DockerClientHelper;
8+
import demo.NoStreamCallback;
79
import java.util.Arrays;
810
import java.util.HashMap;
911
import java.util.List;
1012
import java.util.Map;
13+
import java.util.concurrent.TimeUnit;
1114
import java.util.stream.Collectors;
1215
import org.junit.After;
1316
import org.junit.Before;
1417
import org.junit.Test;
1518

19+
1620
/**
1721
* @GitHub : https://github.com/zacscoding
1822
*/
1923
public class DockerMetricsTest {
2024

21-
Map<String, String> idNameMap = newHashMap<>();
25+
List<String> containerIds;
2226
DockerClient docker;
2327

2428
@Before
2529
public void setUp() {
2630
this.docker = DockerClientHelper.INSTANCE.getDockerClient();
27-
this.idNameMap = new HashMap<>();
28-
2931
List<Container> containers = docker.listContainersCmd().withShowAll(true).exec();
30-
for (Container container : containers) {
31-
idNameMap.put(container.getId(), Arrays.toString(container.getNames()));
32-
}
32+
this.containerIds = containers.stream().map(container -> container.getId()).collect(Collectors.toList());
3333
}
3434

3535
@After
@@ -38,7 +38,18 @@ public void tearDown() throws Exception {
3838
}
3939

4040
@Test
41-
public void temp() throws Exception {
41+
public void collectMetricsJob() throws Exception {
42+
ObjectMapper objectMapper = new ObjectMapper();
43+
for (int i = 0; i < 5; i++) {
44+
for (String containerId : containerIds) {
45+
46+
NoStreamCallback<Statistics> statsCallback = new NoStreamCallback<>();
47+
48+
docker.statsCmd(containerId).exec(statsCallback);
49+
Statistics stats = statsCallback.awaitNext();
50+
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(stats));
51+
}
52+
TimeUnit.SECONDS.sleep(10L);
53+
}
4254
}
43-
4455
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /