1

I have a connection with a Docker Client. The problem is that it runs 2 times per second (it's a thread). It's inefficient to build the same connection every time.

I want to run this function to build the string once and store it in a variable and just return the variable every time it is needed rather than rebuilding the same string over and over. How can I do it?

 public class Docker {

    public static DockerClient dockerClient() {

    DockerClient dockerClient;

    try {

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

        dockerClient = DockerClientBuilder.getInstance(config).build();

        return dockerClient;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • 1
    `DockerClient x = Docker.dockerClient();`...? – Jaroslaw Pawlak Mar 29 '18 at 15:35
  • You've only shown us the `dockerClient` function; if you're asking how to refactor your code that calls the `dockerClient` function repeatedly, because you want to make it only call the `dockerClient` function once, then please show us the code that calls `dockerClient` repeatedly so we can help you fix it. – Chris Martin Mar 29 '18 at 15:49
  • https://stackoverflow.com/questions/726685/yeah-i-know-im-a-simpleton-so-whats-a-singleton – Richard Chambers Mar 29 '18 at 15:52

2 Answers2

1

Use a Singleton pattern:

  • make your dockerClient method private;
  • add a private static DockerClient field;
  • add a public static getClient method that will return the DockerClient field if not null or call your dockerClient method to create it if field is null;

    public class Docker {
    
        private static DockerClient INSTANCE;
    
        public static DockerClient getClient() {
            if (INSTANCE == null) {
                INSTANCE = dockerClient();
            }
    
            return INSTANCE;
        }
    
        private static DockerClient dockerClient() {
            // YOUR IMPLEMENTATION
        }
    }
    

Thus your dockerClient method will only be called once.

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
0

I ended up doing this:

private static DockerClient dockerClient;
static {

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

    dockerClient = DockerClientBuilder.getInstance(config).build();

  } catch (Exception e) {
    e.printStackTrace();
  }
}

public static DockerClient dockerClient() {
    return dockerClient;
}
M. Prokhorov
  • 3,894
  • 25
  • 39