Re: [Simpleweb-Support] aren't "Dispacher-n" threads already asynchronous?
Brought to you by:
niallg
From: Kai S. <sch...@gm...> - 2012-07-09 00:04:12
|
I personally don't like dealing with constrictive outside resources, even when dealing with high performance things like HTTP handlings. I use Simple to simply turn an HTTP request from network traffic into classes that I could deal with in my own system. The best way to use Simple, and no offense Nial, is to use it as a basic HTTP decoding and processing engine, as well as way to spit your data back out on the web properly. So many other projects, including dozens of commercial products have tried to give you an "web dev/engineering/multithreaded/blabla/forassistance-call-thisnumber. Simple does provide some more advanced components, but unlike most programs than can work as HTTP capable servers, Simple isn't pretentious. It doesn't actually force you into anything, and that's why I love it. Of course, please do feel free to use Nial's extra code, it's really quite great. I used to be locked into Tomcate or a apache style scenario, where my app would be started by them. Now my app starts Simple, as one of dozens of other services :) -Kai PS: Thanks Nial :) On Sat, Jul 7, 2012 at 11:45 AM, -=}\*/{=- <rui...@gm...> wrote: > ok... > > now i'm curious of what you call "callbacks" and "servicing"... (no need > to explain, not today) :P > > took the Timed class away... rewrote it like the code bellow. > > [thank you]r. > > private int timedCount = 0; > > private void runTimed(Runner runner) { > Thread thread = new Thread(runner, > "Runner-".concat(String.valueOf(timedCount++))); > 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(); > runTimed(runner); > else { > timedCount++; > runner.run(); > runner.afterRun(); > runner.finish(); > timedCount--; > } > } > > On 7 July 2012 10:37, Niall Gallagher <gal...@ya...> wrote: > >> 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...<http://mc/compose?to=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...<http://mc/compose?to=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...<http://mc/compose?to=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...<http://mc/compose?to=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...<http://mc/compose?to=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 >> >> > > > ------------------------------------------------------------------------------ > 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 > > |