简体   繁体   中英

Avoid calling the same function over and over again

I have a function that builds a connection to a docker. You don't really need to understand much but all I want is to make sure the IP and port is always the same as in my app (Settings.getSettings())

 public static DockerClient dockerClient() {

    try {

        Settings settings = Settings.getSettings();
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("tcp://" + settings.getDockerIP() + ":" + settings.getDockerPort())
                .withDockerConfig("/home/user/.docker/config.json")
                .build();

        DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
                .withMaxTotalConnections(200)
                .withMaxPerRouteConnections(200);

        DockerClient dockerClient = DockerClientBuilder.getInstance(config)
                .withDockerCmdExecFactory(dockerCmdExecFactory)
                .build();

        return dockerClient;

    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return null;
}

And everytime I want to do something I'm calling this function. Example:

public static void startContainer(String containerName) {
    DockerClient dockerClient = dockerClient();
 ....
  public static List<Image> getImages() {
    List<Image> images = new ArrayList<>(dockerClient().listImagesCmd().withShowAll(true).exec());
 ....

Isn't there a more efficient way to handle this? I just want to make sure that when i execute some command, the connection to the right ip and port is made.

You build config and dockerCmdExecFactory every time which probably doesn't needed. As a solution, you could made both fields instead of local variables. But be careful: you have to think about multithreading, how many instances could be and make sure you always initialize fields before calling dockerClient() . Probably it's not an problems in your case but you have to check it.

If you use Spring or something like this you could want to create bean which encapsulates all this stuff.

Also probably some framework you have in your app has already implemented connection logic. It would be more preferable since inside framework a lot of optimisation stuff you couldn't realize. Connection/socket pool and exception handling as an example. Try to google 'docker client your-framework-name '

Based on my comment, you can create a service that holds the docker client state and check if it changes in order to reload or not:

class DockerClientService {
    private DockerClient dockerClient;
    private String ip;
    private int port;

    DockerClient getClient() {
        Settings settings = Settings.getSettings();
        if (settings.getDockerPort() != port || !settings.getDockerIP().equals(ip)) {
            port = settings.getDockerPort();
            ip = settings.getDockerIP();
            DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("tcp://" + ip + ":" + port)
                .withDockerConfig("/home/user/.docker/config.json")
                .build();

            DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
                .withMaxTotalConnections(200)
                .withMaxPerRouteConnections(200);

            dockerClient = DockerClientBuilder.getInstance(config)
                .withDockerCmdExecFactory(dockerCmdExecFactory)
                .build();
        }
        return dockerClient;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM