Skip to content

Instantly share code, notes, and snippets.

@bryanstephens
Created June 19, 2015 21:47
Show Gist options
  • Save bryanstephens/5d4e802c2be1e49ff661 to your computer and use it in GitHub Desktop.
Save bryanstephens/5d4e802c2be1e49ff661 to your computer and use it in GitHub Desktop.
Docker-java event thread that is still blocking
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.EventCallback;
import com.github.dockerjava.api.model.Event;
import com.github.dockerjava.core.DockerClientBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by bryan on 6/19/15.
*/
public class EventThreadHangingTest {
public static void main(String[] args) throws InterruptedException {
DockerClient dockerClient = DockerClientBuilder.getInstance().build();
ExecutorService executorServiceThatWillStillBeBlocking = dockerClient.eventsCmd(getEventCallback()).exec();
executorServiceThatWillStillBeBlocking.shutdown();
shutdownExecutor(executorServiceThatWillStillBeBlocking);
System.out.printf("executorServiceThatWillStillBeBlocking.isShutdown() = %s, executorServiceThatWillStillBeBlocking.isTerminated() = %s\n",
executorServiceThatWillStillBeBlocking.isShutdown(),
executorServiceThatWillStillBeBlocking.isTerminated());
}
private static void shutdownExecutor(ExecutorService executorService) throws InterruptedException {
if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.printf("The executor service is still running, I'm just continuing to run because I timed out.\n");
} else {
System.out.printf("The executor service has shutdown.\n");
}
}
private static EventCallback getEventCallback() {
return new EventCallback() {
@Override
public void onEvent(Event event) {
System.out.printf("Processing an event: %s\n", event);
}
@Override
public void onException(Throwable throwable) {
System.out.printf("Exception thrown: %s\n", throwable);
}
@Override
public void onCompletion(int numEvents) {
System.out.printf("Processed %s number of events\n", numEvents);
}
@Override
public boolean isReceiving() {
return true;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment