0

I'm wondering how to log information when a server has successfully started. I cannot do this as simple as that:

createServer().start(Exit.NEVER);
System.out.println("Server is running...");

because the instruction createServer().start(Exit.NEVER) doesn't return back. This is a call to external library that uses a method with a loop similar to while(true). I cannot also run the server in a new thread and then log information about successful start because the server may throw exception and hence there was a failure.

public void start () {
        new Thread("Server") {
            @Override
            public void run() {
                try {
                    createServer().start(Exit.NEVER);
                } catch (final IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }.start();

        System.out.println("Server is running...");
}

Last solution I can think of is to wait a couple of second after createServer().start(Exit.NEVER) and then log the successful start as there was no exception thrown. This is not a perfect solution as we can wait for example 5 seconds and the log the successful start but one second later the server may throw exception.

How do I then can tell whether the server has started successfully and hence log this information?

EDIT

The server I'm using is Takes https://github.com/yegor256/takes.

mmichal
  • 23
  • 4
  • 1
    You could use a Countdown latch to signal when the server is up https://stackoverflow.com/questions/184147/countdownlatch-vs-semaphore – Slimu May 24 '22 at 09:22
  • if your server does not have a return value for the start (or a kind of busy inditor for the starting process) - does it have a method you can call to check, whether it is operational? – juwil May 24 '22 at 09:30
  • What is this "server?" What does it do inside that `start(...)` call? Is there no way that you can pre-load an "event" or a "request" that will cause it to call back to your own code from inside the `start(...)` method? – Solomon Slow May 24 '22 at 14:22
  • @Slimu I read about that COurtnDownLatch, but I don;t know how to implement this in my screnario. – mmichal May 25 '22 at 08:31
  • 1
    Would another way looking at it help? The Server class cannot be determined when to start(most probably can't be modified), the alternative way is to create a countdown loop to check an API if it's started, an endpoint like /ping. So the (loop+100)ping(endloop) will run if it's over timeout and exit, else wait till it gets a response to 1 of ping/. – Han May 25 '22 at 09:12
  • Looks like some kind of a web server. Couldn't you have another thread in your program wait a few seconds for the server to start up, and then send it a HEAD request? If anything comes back, then the server probably is up. – Solomon Slow May 25 '22 at 15:28
  • P.S., I don't understand why you think you can't run the server in a new thread. You expressed concern that "the server may throw exception..." OK. So, why not catch the exception and log it? (Your example shows catching the exception and re-throwing it, but throwing from a thread's `run()` method isn't especially useful.) Anyway, I don't know what you'd do after logging the exception. That would be up to you to decide. – Solomon Slow May 25 '22 at 15:29

0 Answers0