Re: [Simpleweb-Support] aren't "Dispacher-n" threads already asynchronous?
Brought to you by:
niallg
From: Niall G. <gal...@ya...> - 2012-07-07 09:37:07
|
Asynchronous can mean a lot of things, if you are happy for it to work like a servlet engine then you have done enough. The "asynchronous" part of Simple means that no object is tied to the servicing thread, this means callbacks can be done. If you do not use callbacks then you do not need to use it. --- On Sat, 7/7/12, -=}\*/{=- <rui...@gm...> wrote: From: -=}\*/{=- <rui...@gm...> Subject: Re: [Simpleweb-Support] aren't "Dispacher-n" threads already asynchronous? To: "Simple support and user issues" <sim...@li...> Received: Saturday, 7 July, 2012, 2:17 AM i'm not sure that i understood... that start method i mention in the first email does not do that?... i have the server working, answer the requests, sits as a daemon listening to the desired ports and makes new threads "Dispatcher-n" for each request... isn't that being asynchronous? what confuses me is, that the tutorial has a chapter dedicated to that... as if the first example that i followed was not asynchronous... it seams to be, it logs several threads running in parallel... i just want to implement this the right way... and avoid having threads 3 threads spawn when i can have only 2... never mind the error codes, exception, etc...short and simple: does this code bellow creates a daemon that spawns "Dispatch-0" threads asynchronously? connection = new SocketConnection(this); for (int port: ports) connection.connect(new InetSocketAddress(port)); if yes, can i take the Timed class out of the picture and do the timing control in handle method ran by the "Dispatch-n" Thread? if not, why not? what happens then? how can i do it? [thank you]r. On 7 July 2012 07:30, Kai Schutte <sch...@gm...> wrote: In a nearly every setup of Simple, you want to start Simple, then run a Daemon thread to wait on / query requests... how you handle error codes & warning is completely up to you. -Kai On Sat, Jul 7, 2012 at 6:47 AM, -=}\*/{=- <rui...@gm...> wrote: hi again, i just finished this code and is working fine... but is this the "best" approach? bellow is the inner code of my container class...i'm creating 2 threads, one (Timed) that times the runner (Runner), but if the "Dispatcher-n" thread is already asynchronous maybe makes more sense to use it directly and move Timed.run() code to the handle method... []r. private class Runner implements Runnable { final InnerQuest quest; final Sponse sponse; boolean handled = false; Runner(InnerQuest iq, Sponse s) { this.quest = iq; this.sponse = s; } @Override public void run() { try { // cascade through contexts for (Context c: contexts) if (c.accepts(quest)) if (c.handle(quest, sponse)) { handled = true; return; } else quest.resetCascade(); } catch (Exception e) { quest.logT(e, EM_UNHANDLE); } } void afterRun() { // if not handled... if (!handled) // ... send 404 http error sponse.sendHTTPError(Sponse.SC_404, Sponse.SCD_404, quest); } void finish() { sponse.finish(quest); // log time quest.logI("! %1$sms: %2$s%3$s", System.currentTimeMillis() - quest.creationTime, quest.getHeader(Quest.H_Host), quest.getUrlPath()); // merge quest logger if exists if (quest.hasOwnLogger()) logI(quest.logger.toString()); } } private class Timed implements Runnable { private Runner runner; private String namePostfix; public Timed(Runner r) { this.runner = r; } void start() { namePostfix = String.valueOf(timedCount++); new Thread(this, "TimedRunner-".concat(namePostfix)).start(); } @Override public void run() { Thread thread = new Thread(runner, "Runner-".concat(namePostfix)); Quest quest = runner.quest; thread.start(); try { thread.join(questTimeout); } catch (InterruptedException ex) { quest.logT(ex, EM_INTERRUPTED); } finally { if (thread.isAlive()) { thread.interrupt(); quest.logE(EM_TIMEOUT); runner.sponse.sendHTTPInternalError( "request handling timed out", quest); } else if (!thread.isInterrupted()) // runner not interrupted... runner.afterRun(); runner.finish(); runner = null; timedCount--; } } } protected void handle(InnerQuest quest, Sponse sponse) { Runner runner = new Runner(quest, sponse); if (questTimeout > 0) new Timed(runner).start(); else { timedCount++; runner.run(); runner.afterRun(); runner.finish(); timedCount--; } } ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Simpleweb-Support mailing list Sim...@li... https://lists.sourceforge.net/lists/listinfo/simpleweb-support ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Simpleweb-Support mailing list Sim...@li... https://lists.sourceforge.net/lists/listinfo/simpleweb-support -----Inline Attachment Follows----- ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ -----Inline Attachment Follows----- _______________________________________________ Simpleweb-Support mailing list Sim...@li... https://lists.sourceforge.net/lists/listinfo/simpleweb-support |