java - Restlet stop server after exactly one request is handled -
i want use restlet create local server. server should receive exaclty 1 request (besides favicon), , after request handled should shut down. don't want use system.exit
, want shut down properly.
as can see in code commented line when assume request handled properly. can't tell server stop there.
how can tell server stop waiting requests @ point? got working 2 issues i'd solve.
- i exceptions when stop server within request
the
response
sent client not shown if stop server within requestpublic class main { public static void main(string[] args){ server serv = null; restlet restlet = new restlet() { @override public void handle(request request, response response) { if(!request.tostring().contains("favicon")){ system.out.println("do stuff"); response.setentity("request handled", mediatype.text_plain); //stop server after first request handled. //so program should shut down here //if use serv.stop() here (besides it's not final) //i'd exceptions , user wouldn't see response } } }; // avoid conflicts other java containers listening on 8080! try { serv = new server(protocol.http, 8182, restlet); serv.start(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }
i figured out way it. add onsent
event response, , shut down server here.
public class main { private server serv; public main(){ run(); } public void run(){ restlet restlet = new restlet() { @override public void handle(request request, response response) { response.setentity("request handled", mediatype.text_plain); if(!request.tostring().contains("favicon")){ system.out.println("do stuff"); response.setonsent(new uniform() { @override public void handle(request req, response res) { try { serv.stop();//stop server } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } } }; // avoid conflicts other java containers listening on 8080! try { serv = new server(protocol.http, 8182, restlet); serv.start(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void main(string[] args){ new main(); } }
Comments
Post a Comment