14

Below is the code to start Grizzly Http Server. If I press any key the server stops. Is there any way to keep it alive.

Jetty has join() method, which will not exit the main program. Is there anything similar to that in Grizzly also.

public static void main(String args){



ResourceConfig rc = new PackagesResourceConfig("com.test.resources");

        HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
        logger.info(String.format("Jersey app started with WADL available at "
                        + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
                        BASE_URI, BASE_URI));

        System.in.read();
        httpServer.stop();

        }

As per the above code, if you hit any key the server stops. I want to keep it running. I will kill the process when I actually want to stop the server. The main method should not terminate.

Thanks

Aditi
  • 1,188
  • 2
  • 16
  • 44
user2017420
  • 141
  • 1
  • 3

4 Answers4

26

I use a shutdown hook. Here's a code example:

public class ExampleServer {
private static final Logger logger = LoggerFactory
        .getLogger(ExampleServer.class);

public static void main(String[] args) throws IOException {
    new Server().doMain(args);
}

public void doMain(String[] args) throws IOException {
    logger.info("Initiliazing Grizzly server..");
    // set REST services packages
    ResourceConfig resourceConfig = new PackagesResourceConfig(
            "pt.lighthouselabs.services");

    // instantiate server
    final HttpServer server = GrizzlyServerFactory.createHttpServer(
            "http://localhost:8080", resourceConfig);

    // register shutdown hook
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            logger.info("Stopping server..");
            server.stop();
        }
    }, "shutdownHook"));

    // run
    try {
        server.start();
        logger.info("Press CTRL^C to exit..");
        Thread.currentThread().join();
    } catch (Exception e) {
        logger.error(
                "There was an error while starting Grizzly HTTP server.", e);
    }
}

}
Paulo Pires
  • 371
  • 2
  • 10
2

Try something like:

    try {
        server.start();
        Thread.currentThread().join();
    } catch (Exception ioe) {
        System.err.println(ioe);
    } finally {
        try {
            server.stop();
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }
rlubke
  • 965
  • 5
  • 14
1

The server stops because you call the httpServer.stop() method after the input stream. When the execution reachs the System.in.read(); it hangs till you enter a letter and then moves on to the server stop.

You can just comment httpServer.stop() because that code example is exactly to hang up the server when a key is pressed.

But if you want to create a Webserver instance I would suggest that you run a Thread in main() that starts an instance of the Grizzly Webserver.

nakib
  • 4,304
  • 2
  • 28
  • 39
0

UPDATE 2022/11/23

The .stop() function has been deprecated, it's now safer to use .shutdown()

ATTENTION

If you are using WELD or other CDI container you should shutdown it

    final Weld weld = new Weld();
    weld.initialize();

    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.packages("loc.java.my.res.pack");

    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:9000/"),
                resourceConfig);

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            logger.info("Stopping server..");
            server.shutdown();//.stop() has been deprecated
            weld.shutdown();//check your CDI container shutdown function
        }
    }, "shutdownHook"));

    // run
    try {
        server.start();
        logger.info("Press CTRL^C to exit..");
        Thread.currentThread().join();
    } catch (Exception e) {
        logger.error(
                "There was an error while starting Grizzly HTTP server.", e);
    }
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55