Java Reference
In-Depth Information
...
}
}
Here we're back to our familiar
OrderResource
again. We have a
ytdReport()
method that
calculates buying patterns for a specific product for the year to date. We want to allow only
one of these requests to execute at a time, as the calculation is extremely expensive. We set
up a single-threaded
java.util.concurrent.Executor
in the
OrderResource
constructor.
The
ytdReport()
method queues up a
Runnable
in this
Executor
that generates the report
and sends it back to the client. If the
Executor
is currently busy generating a report, the re-
quest is queued up and executed after that report is finished.
Wrapping Up
In this chapter, we discussed how you can use JAX-RS asynchronously both on the client
and server side. On the client, you can execute one or more requests in the background and
either poll for their response, or receive a callback. On the server, we saw that you can sus-
pend requests so that a different thread can handle response processing. This is a great way to
scale specific kinds of applications.
Chapter 27
walks you through a bunch of code examples
that show most of these features in action.
[
13
]
For more information, see
http://www.websocket.org
.
[
14
]
For more information, see
http://www.w3.org/TR/2011/WD-eventsource-20110208
.