Worker threads

5 views
Skip to first unread message

Ad

unread,
Jul 18, 2011, 10:48:45 AM7/18/11
to Deft Web Server
Hi,

I'm interested in using Deft for a project and am looking for some
help since I can't find much documentation. I want to implement a web
server which when it receives a request will spawn off a worker thread
and block whilst it is working. Any additional requests received in
this time should also block on the same thread. Any guidance on how to
do this is appreciated.

Adam

Roger Schildmeijer

unread,
Jul 19, 2011, 2:01:36 AM7/19/11
to deft-we...@googlegroups.com
Hi Adam,

Take a look at https://gist.github.com/1091426. Is that what you had in mind?

Two important things to keep in mind:
* The get (http) method must be annotated with the @Asynchronous annotation (the framework would otherwise finish the http request, and send back the response.)
* The HttpResponse.finish() that you have to invoke explicitly (for a synchronous (http) method this is actually what is done by the framework) must be invoked by the
ioloop thread.


HTH

Roger Schildmeijer

Ad

unread,
Jul 19, 2011, 12:40:47 PM7/19/11
to Deft Web Server
This is exactly what I needed. Thanks for the awesome demo code and
useful tips. I would recommend including this example in the source
code since this was the first place I checked for help.

One other question this leads onto is about scheduling a timeout in
the IOLoop. This would be to run a callback to look over the various
resources which have been allocated (e.g. ExecutorServices) and
garbage collect them. There seems to be various classes related to
timeout scheduling but I can't seem to put it all together. Does
anything like e.g. IOLoop.instance.addTimeout(1000, new
AsyncCallback() {}) exist?

Best,
Adam

On Jul 19, 7:01 am, Roger Schildmeijer <schildmei...@gmail.com> wrote:
> Hi Adam,
>
> Take a look athttps://gist.github.com/1091426. Is that what you had in mind?

Ad

unread,
Jul 20, 2011, 5:03:58 AM7/20/11
to Deft Web Server
I've now solved this question. For other people new to java / deft
here is an example timeout for 1000 ms in the future:

IOLoop.INSTANCE.addTimeout(new Timeout(System.currentTimeMillis() +
1000, new AsyncCallback() {
public void onCallback() {
// do work
}
));

Thanks again for the help Roger.

Adam

Roger Schildmeijer

unread,
Jul 20, 2011, 6:26:55 AM7/20/11
to deft-we...@googlegroups.com

No problem.

Roger Schildmeijer

unread,
Jul 21, 2011, 11:56:44 AM7/21/11
to deft-we...@googlegroups.com
Hi again Adam,

If you have a task that needs to be scheduled and executed periodically (like every second, or fifth or tenth second) you should take a look at the PeriodicCallback class.

// Roger

Reply all
Reply to author
Forward
0 new messages