Skip to content

Commit 0c90737

Browse files
committed
Merge pull request #263 from docker-java/refact-streaming
Refactoring of streaming commands APIs (event, stats, log, attach)
2 parents 8f3de21 + 89a2c04 commit 0c90737

40 files changed

Lines changed: 1014 additions & 489 deletions

src/main/java/com/github/dockerjava/api/DockerClient.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
package com.github.dockerjava.api;
22

3-
import java.io.*;
4-
5-
import com.github.dockerjava.api.command.*;
3+
import java.io.Closeable;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
import com.github.dockerjava.api.async.ResultCallback;
9+
import com.github.dockerjava.api.command.AttachContainerCmd;
10+
import com.github.dockerjava.api.command.AuthCmd;
11+
import com.github.dockerjava.api.command.BuildImageCmd;
12+
import com.github.dockerjava.api.command.CommitCmd;
13+
import com.github.dockerjava.api.command.ContainerDiffCmd;
14+
import com.github.dockerjava.api.command.CopyFileFromContainerCmd;
15+
import com.github.dockerjava.api.command.CreateContainerCmd;
16+
import com.github.dockerjava.api.command.CreateImageCmd;
17+
import com.github.dockerjava.api.command.EventsCmd;
18+
import com.github.dockerjava.api.command.ExecCreateCmd;
19+
import com.github.dockerjava.api.command.ExecStartCmd;
20+
import com.github.dockerjava.api.command.InfoCmd;
21+
import com.github.dockerjava.api.command.InspectContainerCmd;
22+
import com.github.dockerjava.api.command.InspectExecCmd;
23+
import com.github.dockerjava.api.command.InspectImageCmd;
24+
import com.github.dockerjava.api.command.KillContainerCmd;
25+
import com.github.dockerjava.api.command.ListContainersCmd;
26+
import com.github.dockerjava.api.command.ListImagesCmd;
27+
import com.github.dockerjava.api.command.LogContainerCmd;
28+
import com.github.dockerjava.api.command.PauseContainerCmd;
29+
import com.github.dockerjava.api.command.PingCmd;
30+
import com.github.dockerjava.api.command.PullImageCmd;
31+
import com.github.dockerjava.api.command.PushImageCmd;
32+
import com.github.dockerjava.api.command.RemoveContainerCmd;
33+
import com.github.dockerjava.api.command.RemoveImageCmd;
34+
import com.github.dockerjava.api.command.RestartContainerCmd;
35+
import com.github.dockerjava.api.command.SaveImageCmd;
36+
import com.github.dockerjava.api.command.SearchImagesCmd;
37+
import com.github.dockerjava.api.command.StartContainerCmd;
38+
import com.github.dockerjava.api.command.StatsCmd;
39+
import com.github.dockerjava.api.command.StopContainerCmd;
40+
import com.github.dockerjava.api.command.TagImageCmd;
41+
import com.github.dockerjava.api.command.TopContainerCmd;
42+
import com.github.dockerjava.api.command.UnpauseContainerCmd;
43+
import com.github.dockerjava.api.command.VersionCmd;
44+
import com.github.dockerjava.api.command.WaitContainerCmd;
645
import com.github.dockerjava.api.model.AuthConfig;
46+
import com.github.dockerjava.api.model.Event;
47+
import com.github.dockerjava.api.model.Frame;
748
import com.github.dockerjava.api.model.Identifier;
49+
import com.github.dockerjava.api.model.Statistics;
850

951
// https://godoc.org/github.com/fsouza/go-dockerclient
1052
public interface DockerClient extends Closeable {
@@ -72,13 +114,13 @@ public interface DockerClient extends Closeable {
72114

73115
public WaitContainerCmd waitContainerCmd(String containerId);
74116

75-
public AttachContainerCmd attachContainerCmd(String containerId);
117+
public AttachContainerCmd attachContainerCmd(String containerId, ResultCallback<Frame> resultCallback);
76118

77119
public ExecStartCmd execStartCmd(String containerId);
78120

79121
public InspectExecCmd inspectExecCmd(String execId);
80122

81-
public LogContainerCmd logContainerCmd(String containerId);
123+
public LogContainerCmd logContainerCmd(String containerId, ResultCallback<Frame> resultCallback);
82124

83125
public CopyFileFromContainerCmd copyFileFromContainerCmd(String containerId, String resource);
84126

@@ -106,9 +148,9 @@ public interface DockerClient extends Closeable {
106148

107149
public UnpauseContainerCmd unpauseContainerCmd(String containerId);
108150

109-
public EventsCmd eventsCmd(EventCallback eventCallback);
151+
public EventsCmd eventsCmd(ResultCallback<Event> resultCallback);
110152

111-
public StatsCmd statsCmd(StatsCallback statsCallback);
153+
public StatsCmd statsCmd(ResultCallback<Statistics> resultCallback);
112154

113155
@Override
114156
public void close() throws IOException;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.dockerjava.api.async;
2+
3+
import java.io.Closeable;
4+
5+
/**
6+
* Result callback
7+
*/
8+
public interface ResultCallback<RES_T> extends Closeable {
9+
/**
10+
* Called when the async processing starts. The passed {@link Closeable} can be used to close/interrupt the
11+
* processing
12+
*/
13+
void onStart(Closeable closeable);
14+
15+
/** Called when an async result event occurs */
16+
void onNext(RES_T object);
17+
18+
/** Called when an exception occurs while processing */
19+
void onError(Throwable throwable);
20+
21+
/** Called when processing was finished either by reaching the end or by aborting it */
22+
void onComplete();
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Created on 17.06.2015
3+
*/
4+
package com.github.dockerjava.api.command;
5+
6+
import com.github.dockerjava.api.async.ResultCallback;
7+
8+
/**
9+
*
10+
*
11+
* @author marcus
12+
*
13+
*/
14+
public interface AsyncDockerCmd<CMD_T extends DockerCmd<RES_T>, A_RES_T, RES_T> extends DockerCmd<RES_T> {
15+
16+
public ResultCallback<A_RES_T> getResultCallback();
17+
18+
public CMD_T withResultCallback(ResultCallback<A_RES_T> resultCallback);
19+
20+
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import com.github.dockerjava.api.DockerClient;
66
import com.github.dockerjava.api.NotFoundException;
7+
import com.github.dockerjava.api.model.Frame;
78

89
/**
910
* Attach to container
10-
*
11+
*
1112
* @param logs
1213
* - true or false, includes logs. Defaults to false.
13-
*
14+
*
1415
* @param followStream
1516
* - true or false, return stream. Defaults to false.
1617
* @param stdout
@@ -20,7 +21,7 @@
2021
* @param timestamps
2122
* - true or false, if true, print timestamps for every log line. Defaults to false.
2223
*/
23-
public interface AttachContainerCmd extends DockerCmd<InputStream> {
24+
public interface AttachContainerCmd extends AsyncDockerCmd<AttachContainerCmd, Frame, Void> {
2425

2526
public String getContainerId();
2627

@@ -64,14 +65,14 @@ public interface AttachContainerCmd extends DockerCmd<InputStream> {
6465

6566
/**
6667
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent connection leaks.
67-
*
68+
*
6869
* @throws NotFoundException
6970
* No such container
7071
*/
7172
@Override
72-
public InputStream exec() throws NotFoundException;
73+
public Void exec() throws NotFoundException;
7374

74-
public static interface Exec extends DockerCmdExec<AttachContainerCmd, InputStream> {
75+
public static interface Exec extends DockerCmdExec<AttachContainerCmd, Void> {
7576
}
7677

77-
}
78+
}

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

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.dockerjava.api.command;
22

3-
import java.util.concurrent.ExecutorService;
3+
import com.github.dockerjava.api.model.Event;
44

55
/**
66
* Get events
@@ -10,7 +10,7 @@
1010
* @param until
1111
* - Stream events until this timestamp
1212
*/
13-
public interface EventsCmd extends DockerCmd<ExecutorService> {
13+
public interface EventsCmd extends AsyncDockerCmd<EventsCmd, Event, Void> {
1414
public EventsCmd withSince(String since);
1515

1616
public EventsCmd withUntil(String until);
@@ -19,10 +19,6 @@ public interface EventsCmd extends DockerCmd<ExecutorService> {
1919

2020
public String getUntil();
2121

22-
public EventCallback getEventCallback();
23-
24-
public EventsCmd withEventCallback(EventCallback eventCallback);
25-
26-
public static interface Exec extends DockerCmdExec<EventsCmd, ExecutorService> {
22+
public static interface Exec extends DockerCmdExec<EventsCmd, Void> {
2723
}
2824
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import com.github.dockerjava.api.DockerClient;
44
import com.github.dockerjava.api.NotFoundException;
5+
import com.github.dockerjava.api.model.Frame;
56

67
import java.io.InputStream;
78

89
/**
910
* Get container logs
10-
*
11+
*
1112
* @param followStream
1213
* - true or false, return stream. Defaults to false.
1314
* @param stdout
@@ -18,12 +19,8 @@
1819
* - true or false, if true, print timestamps for every log line. Defaults to false.
1920
* @param tail
2021
* - `all` or `<number>`, Output specified number of lines at the end of logs
21-
*
22-
* Consider wrapping any input stream you get with a frame reader to make reading frame easier.
23-
*
24-
* @see com.github.dockerjava.core.command.FrameReader
2522
*/
26-
public interface LogContainerCmd extends DockerCmd<InputStream> {
23+
public interface LogContainerCmd extends AsyncDockerCmd<LogContainerCmd, Frame, Void> {
2724

2825
public String getContainerId();
2926

@@ -69,14 +66,14 @@ public interface LogContainerCmd extends DockerCmd<InputStream> {
6966

7067
/**
7168
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent connection leaks.
72-
*
69+
*
7370
* @throws NotFoundException
7471
* No such container
7572
*/
7673
@Override
77-
public InputStream exec() throws NotFoundException;
74+
public Void exec() throws NotFoundException;
7875

79-
public static interface Exec extends DockerCmdExec<LogContainerCmd, InputStream> {
76+
public static interface Exec extends DockerCmdExec<LogContainerCmd, Void> {
8077
}
8178

82-
}
79+
}

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

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package com.github.dockerjava.api.command;
22

3-
import java.util.concurrent.ExecutorService;
3+
import com.github.dockerjava.api.model.Statistics;
44

55
/**
6-
* Get stats
7-
*
6+
* Get container stats. The result of {@link Statistics} is handled asynchronously because the docker remote API will
7+
* block when a container is stopped until the container is up again.
88
*/
9-
public interface StatsCmd extends DockerCmd<ExecutorService> {
9+
public interface StatsCmd extends AsyncDockerCmd<StatsCmd, Statistics, Void> {
1010
public StatsCmd withContainerId(String containerId);
1111

1212
public String getContainerId();
1313

14-
public StatsCmd withStatsCallback(StatsCallback statsCallback);
15-
16-
public StatsCallback getStatsCallback();
17-
18-
public static interface Exec extends DockerCmdExec<StatsCmd, ExecutorService> {
14+
public static interface Exec extends DockerCmdExec<StatsCmd, Void> {
1915
}
20-
2116
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.github.dockerjava.api.model;
22

33
public enum StreamType {
4-
STDIN, STDOUT, STDERR
4+
STDIN, STDOUT, STDERR, RAW
55
}

0 commit comments

Comments
 (0)