use of jakarta.ws.rs.client.InvocationCallback in project jaxrs-api by eclipse-ee4j.
the class JaxrsWebTestCase method invoke.
/**
* Invoke the invocation synchronously, or asynchronously
*/
protected Response invoke(Invocation invocation) throws TestFailureException {
Response response = null;
switch(executionType) {
case SYNCHRONOUS:
response = invocation.invoke();
break;
case ASYNCHRONOUS:
int cnt = 0;
try {
final boolean[] buffered = { false };
InvocationCallback<Response> callback = new InvocationCallback<Response>() {
@Override
public void completed(Response res) {
try {
JaxrsWebTestCase.this.response = res;
// buffer before stream is closed
getResponse().getResponseBodyAsString();
buffered[0] = true;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void failed(Throwable throwable) {
throw new RuntimeException(throwable);
}
};
Future<Response> future = invocation.submit(callback);
while (!buffered[0] && cnt++ < 50) {
Thread.sleep(100L);
}
response = future.get();
// response = invocation.submit().get();
} catch (Exception e) {
throw new TestFailureException(e);
}
if (cnt > 49) {
throw new TestFailureException("Invocation callback has not been called within 5 second");
}
}
return response;
}
Aggregations