Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to stop Java HTTP server

Reply
Thread Tools

How to stop Java HTTP server

 
 
Shiladitya
Guest
Posts: n/a
 
      10-13-2010
Hi,
I am using the embedded Sun Java Http server : com.sun.net.httpserver.

I wrote code for setting the executor, adding context and starting it.
This is written in the main thread.
I need a way to call httpServerExe.stop() from this thread.

So the main thread can be like this:


{
httpServerExe.start();
while(!terminated) {
Thread.sleep(4000);
}
httpServerExe.stop();

}

I have set up a handler for a URL (/terminateCommand). So if anyone
send a request to this URL, the terminated flag should be set and main
thread should stop the http server.

But I can't figure out how to set the flag from one of the handlers so
that main thread gets interrupted.

TIA
Shiladitya
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      10-14-2010
On Wed, 13 Oct 2010, Shiladitya wrote:

> I need a way to call httpServerExe.stop() from this thread.
>
> So the main thread can be like this:
>
> httpServerExe.start();
> while(!terminated) {
> Thread.sleep(4000);
> }
> httpServerExe.stop();


That's the wrong way to do it. Rather than sleeping in a loop, this thread
should wait on a monitor, and threads which want to stop the server should
notify that monitor.

The main code would look like:

server.start();
synchronized (terminationLock) {
while (!terminated) terminationLock.wait();
}
server.stop();

Code which wants to stop it can go:

terminated = true;
synchronized (terminationLock) {
terminationLock.notify();
}

> I have set up a handler for a URL (/terminateCommand). So if anyone send
> a request to this URL, the terminated flag should be set and main thread
> should stop the http server.
>
> But I can't figure out how to set the flag from one of the handlers so
> that main thread gets interrupted.


The handler has to have a reference to the place the flag lives. If the
flag is a static variable on a class, then it can go directly to the class
by name:

MainClass.terminated = true;
synchronized (terminationLock) {
MainClass.terminationLock.notify();
}

Although of course it would be better to wrap that in a method:

MainClass.terminate();

class MainClass {
private static boolean terminated;
private static Object terminationLock;
public static void terminate() {
terminated = true;
synchronized (terminationLock) {
terminationLock.notify();
}
}
}

If it's on an object, then you will need to pass a reference to that
object to the handler somehow, perhaps when you construct it.

void main(String... args) {
HttpServer server = HttpServer.create();
ServerController controller = new ServerController(server);
HttpHandler terminationHandler = new TerminationHandler(controller);
server.createContext("/terminate", terminationHandler);
new Thread(controller).start;
}

class ServerController implements Runnable {
private HttpServer server;
private boolean terminated;
private Object terminationLock = new Object();
public ServerController(HttpServer server) {
this.server = server;
}
public void run() {
server.start();
synchronized (terminationLock) {
try {
while (!terminated) terminationLock.wait();
}
catch (InterruptedException e) {}
}
server.stop();
}
public void terminate() {
terminated = true;
synchronized (terminationLock) {
terminationLock.notify();
}
}
}

You could dispense with the terminationLock by using the ServerController
itself to wait and notify on, but i tend to steer way from that, and use
private objects as locks, so that the wait/notify activity of the methods
can't 'leak' across the interface.

An even better way to do this, actually, would be with a
java.util.concurrent.CountDownLatch with a count of one:

class ServerController implements Runnable {
private HttpServer server;
private CountDownLatch latch = new CountDownLatch(1);
public ServerController(HttpServer server) {
this.server = server;
}
public void run() {
server.start();
try {
latch.await();
}
catch (InterruptedException e) {}
server.stop();
}
public void terminate() {
latch.countDown();
}
}

tom

--
you can't feel your stomack with glory -- Czako
 
Reply With Quote
 
 
 
 
Shiladitya
Guest
Posts: n/a
 
      10-14-2010
On Oct 14, 8:12*am, Tom Anderson <(E-Mail Removed)> wrote:
> On Wed, 13 Oct 2010, Shiladitya wrote:
> > I need a way to call httpServerExe.stop() from this thread.

>
> > So the main thread can be like this:

>
> > * * * httpServerExe.start();
> > * * * while(!terminated) {
> > * * * * *Thread.sleep(4000);
> > * * * * }
> > * * * * httpServerExe.stop();

>
> That's the wrong way to do it. Rather than sleeping in a loop, this thread
> should wait on a monitor, and threads which want to stop the server should
> notify that monitor.
>
> The main code would look like:
>
> server.start();
> synchronized (terminationLock) {
> * * * * while (!terminated) terminationLock.wait();}
>
> server.stop();
>
> Code which wants to stop it can go:
>
> terminated = true;
> synchronized (terminationLock) {
> * * * * terminationLock.notify();
>
> }
> > I have set up a handler for a URL (/terminateCommand). So if anyone send
> > a request to this URL, the terminated flag should be set and main thread
> > should stop the http server.

>
> > But I can't figure out how to set the flag from one of the handlers so
> > that main thread gets interrupted.

>
> The handler has to have a reference to the place the flag lives. If the
> flag is a static variable on a class, then it can go directly to the class
> by name:
>
> MainClass.terminated = true;
> synchronized (terminationLock) {
> * * * * MainClass.terminationLock.notify();
>
> }
>
> Although of course it would be better to wrap that in a method:
>
> MainClass.terminate();
>
> class MainClass {
> * * * * private static boolean terminated;
> * * * * private static Object terminationLock;
> * * * * public static void terminate() {
> * * * * * * * * terminated = true;
> * * * * * * * * synchronized (terminationLock) {
> * * * * * * * * * * * * terminationLock.notify();
> * * * * * * * * }
> * * * * }
>
> }
>
> If it's on an object, then you will need to pass a reference to that
> object to the handler somehow, perhaps when you construct it.
>
> void main(String... args) {
> * * * * HttpServer server = HttpServer.create();
> * * * * ServerController controller = new ServerController(server);
> * * * * HttpHandler terminationHandler = new TerminationHandler(controller);
> * * * * server.createContext("/terminate", terminationHandler);
> * * * * new Thread(controller).start;
>
> }
>
> class ServerController implements Runnable {
> * * * * private HttpServer server;
> * * * * private boolean terminated;
> * * * * private Object terminationLock = new Object();
> * * * * public ServerController(HttpServer server) {
> * * * * * * * * this.server = server;
> * * * * }
> * * * * public void run() {
> * * * * * * * * server.start();
> * * * * * * * * synchronized (terminationLock) {
> * * * * * * * * * * * * try {
> * * * * * * * * * * * * * * * * while (!terminated) terminationLock.wait();
> * * * * * * * * * * * * }
> * * * * * * * * * * * * catch (InterruptedException e) {}
> * * * * * * * * }
> * * * * * * * * server.stop();
> * * * * }
> * * * * public void terminate() {
> * * * * * * * * terminated = true;
> * * * * * * * * synchronized (terminationLock) {
> * * * * * * * * * * * * terminationLock.notify();
> * * * * * * * * }
> * * * * }
>
> }
>
> You could dispense with the terminationLock by using the ServerController
> itself to wait and notify on, but i tend to steer way from that, and use
> private objects as locks, so that the wait/notify activity of the methods
> can't 'leak' across the interface.
>
> An even better way to do this, actually, would be with a
> java.util.concurrent.CountDownLatch with a count of one:
>
> class ServerController implements Runnable {
> * * * * private HttpServer server;
> * * * * private CountDownLatch latch = new CountDownLatch(1);
> * * * * public ServerController(HttpServer server) {
> * * * * * * * * this.server = server;
> * * * * }
> * * * * public void run() {
> * * * * * * * * server.start();
> * * * * * * * * try {
> * * * * * * * * * * * * latch.await();
> * * * * * * * * }
> * * * * * * * * catch (InterruptedException e) {}
> * * * * * * * * server.stop();
> * * * * }
> * * * * public void terminate() {
> * * * * * * * * latch.countDown();
> * * * * }
>
> }
>
> tom
>
> --
> you can't feel your stomack with glory -- Czako


That was very helpful Tom. I have tested it out and it works nicely !
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
Can I change the response URL to http://server/page.XLS instead of http://server/page.ASPX ? guillermojco@gmail.com ASP .Net 3 04-26-2007 04:49 AM
How to stop java service with command line parameter '-stop'? Will Java 1 11-02-2004 03:32 PM
how can i simulate my pc as http server by writing java code to send the http response by socketstream to some pc in the internet?! mike Java 5 09-21-2004 02:04 PM



Advertisments