Java Reference
In-Depth Information
exceptions from the JAX-RS error exception hierarchy (i.e.,
NotFoundException
or
BadRequestException
). If an exception is thrown while you're unmarshalling the response
to a
Order
, then
ResponseProcessingException
is thrown.
WARNING
You should always make sure that the underlying JAX-RS response is closed. While most JAX-RS
containers will have their
Response
objects implement a
finalize()
method, it is not a good idea
to rely on the garbage collector to clean up your client connections. If you do not clean up your con-
nections, you may end up with intermittent errors that pop up if the underlying
Client
or operating
system has exhausted its limit of allowable open connections.
In fact, if we examine our initial example a bit further, there's a lot of code we have to add to
ensure that we are being good citizens and closing any open
Response
objects. Here's what
the final piece of code would look like:
Client client
=
ClientBuilder
.
newClient
();
Future
<
Response
>
future1
=
client
.
target
(
"http://example.com/service"
)
.
request
()
.
async
().
get
();
Future
<
Order
>
future2
=
null
null
;
try
try
{
future2
=
client
.
target
(
"http://foobar.com/service2"
)
.
request
()
.
async
().
get
(
Order
.
class
);
}
catch
catch
(
Throwable ignored
) {
ignored
.
printStackTrace
();
}
// block until complete
Response res1
=
future1
.
get
();
try
try
{
Customer result1
=
res
.
readEntity
(
Customer
.
class
);
}
catch
catch
(
Throwable ignored
) {
ignored
.
printStackTrace
();
}
finally
finally
{
res1
.
close
();
}
// if we successfully executed 2nd request
iif
(
future2
!=
null
null
) {
// Wait 5 seconds
