Java Reference
In-Depth Information
try
try
{
Order result2
=
future2
.
get
(
5
,
TimeUnit
.
SECONDS
);
}
catch
catch
(
TimeoutException timeout
) {
System
.
err
.
println
(
"request timed out"
);
}
catch
catch
(
InterruptedException ie
) {
System
.
err
.
println
(
"Request was interrupted"
);
}
catch
catch
(
ExecutionException ee
) {
Throwable cause
=
ee
.
getCause
();
iif
(
cause
instanceof
instanceof
WebApplicationException
) {
(
WebApplicationException
)
wae
= (
WebApplicationException
)
cause
;
wae
.
close
();
}
else
instanceof
ResponseProcessingException
) {
ResponseProcessingException rpe
= (
ResponseProcessingException
)
cause
;
rpe
.
close
();
}
else
else iif
(
cause
instanceof
instanceof
ProcessingException
) {
// handle processing exception
}
else
else iif
(
cause
instanceof
else
{
// unknown
}
}
}
As you can see, there's a few more
try/catch
blocks we need to add to make sure that the
response of each async request is closed.
Using Callbacks
The
AsyncInvoker
interface has an additional callback invocation style. You can register an
object that will be called back when the asynchronous invocation is ready for processing:
package
package
javax
.
ws
.
rs
.
client
;
public
public interface
AsyncInvoker
{
<
T
>
Future
<
T
>
get
(
InvocationCallback
<
T
>
callback
);
<
T
>
Future
<
T
>
post
(
Entity
<?>
entity
,
InvocationCallback
<
T
>
callback
);
<
T
>
Future
<
T
>
put
(
Entity
<?>
entity
,
InvocationCallback
<
T
>
callback
);
<
T
>
Future
<
T
>
delete
(
Entity
<?>
entity
,
InvocationCallback
<
T
>
callback
);
interface
AsyncInvoker
...
}
The
InvocationCallback
interface is a parameterized generic interface and has two simple
methods you have to implement—one for successful responses, the other for failures: