users@grizzly.java.net

Re: Keep server running without using System.in.read()

From: Alex Davies-Moore <a_at_devork.com>
Date: Fri, 27 Jun 2014 15:50:28 +0100

I've done something like this in the past which works for me. Start the
server with:

 (new Server()).run()


public class Server {

    private static final Logger LOGGER =
Logger.getLogger(Server.class.getName());

    /** used to sync the server thread */
    private static final Object MUTEX = new Object();


    public void run() {

        HttpServer server = null;

        try {
            server = build();
            server.start();
        } catch (Exception e) {
            LOGGER.severe("Unable to start server: " + e.getMessage());
            System.exit(-1);
        }

        LOGGER.info("Server Started: " + new Date());

        Runtime.getRuntime().addShutdownHook(new ShutdownHandler());
        synchronized (MUTEX) {
            try {
                MUTEX.wait();
            } catch (InterruptedException e) {
                // ignores
            }
        }

        GrizzlyFuture<HttpServer> future = server.shutdown();
        try {
            future.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException | TimeoutException |
ExecutionException e) {
            LOGGER.warning("Ignored error thrown during shutdown: " +
e.getMessage());
        }


        LOGGER.info("Server stopped: " + new Date());
    }

    private HttpServer build() throws Exception {

        HttpServer server = null;

        // configure etc
        return server;
    }


    private static class ShutdownHandler extends Thread {


        @Override
        public void run() {
            LOGGER.info("Server stopping: " + new Date());

            synchronized (MUTEX) {
                MUTEX.notifyAll();
            }
        }
    }
}



*Alex Davies-Moore / Developer*

25 Wayman Road / Corfe Mullen / Dorset / BH21 3PL / UK
P: +44 (0) 1202 697 394 / M: +44 (0) 7961 629 399 / T: @schoenobates

Devork Limited is a limited company registered in England and Wales
Registered number: 07699891
Registered office: 25 Wayman Road, Corfe Mullen, Dorset, BH21 3PL

This message and any files transmitted with it are the property of Devork
Ltd, are confidential, and are intended solely for the use of the person or
entity to whom this e-mail is addressed. If you are not one of the named
recipient(s) or otherwise have reason to believe that you have received
this message in error, please contact the sender and delete this message
immediately from your computer. Any other use, retention, dissemination,
forwarding, printing, or copying of this e-mail is strictly prohibited.


On 27 June 2014 06:33, LongkerDandy <longkerdandy_at_gmail.com> wrote:

> Thanks for your insight, JM
> Very helpful.
>
> Regards
> LongkerDandy
>
>
> On Wed, Jun 25, 2014 at 11:57 PM, Johan Maasing <johan_at_zoom.nu> wrote:
>
>> Well, you should ask yourself, how do I want to manage the server (i.e.
>> send it a 'stop' command)?
>> One way is to use System.in as in the example. Another way I have used is
>> something like:
>>
>> this.transport.bind...
>> final AtomicBoolean keepRunning = new AtomicBoolean(true) ;
>> while (keepRunning.get()) {
>> try {
>> Thread.sleep(5000) ;
>> } catch (InterruptedException e) {
>> keepRunning.set(false) ;
>> }
>> }
>>
>> Then expose a method through JMX that sets the 'keepRunning' to false.
>> Which means I can for example start JConsole, attach to the server JVM and
>> invoke my stop method from JConsole.
>>
>> Cheers,
>> JM
>>
>>
>> 2014-06-25 16:45 GMT+02:00 LongkerDandy <longkerdandy_at_gmail.com>:
>>
>> Hi
>>>
>>>
>>> I've a TCP Server based on Grizzly, and I followed the examples start
>>> the server with codes like:
>>>
>>> this.transport.bind(this.host, this.port);
>>> this.transport.start();
>>> System.in.read();
>>>
>>> I wondered if there is a better way to keep the thread runing without
>>> using System.in.read().
>>> It doesn't seem to be a elegant way for this job, and easily run into
>>> trouble with carefuless mistake.
>>>
>>> Regards
>>> LongkerDandy
>>>
>>
>>
>