use of javax.ws.rs.container.AsyncResponse in project jersey by jersey.
the class ObservableAgentResource method observable.
@GET
public void observable(@Suspended final AsyncResponse async) {
final long time = System.nanoTime();
final Queue<String> errors = new ConcurrentLinkedQueue<>();
Observable.just(new AgentResponse()).zipWith(visited(errors), (response, visited) -> {
response.setVisited(visited);
return response;
}).zipWith(recommended(errors), (response, recommendations) -> {
response.setRecommended(recommendations);
return response;
}).observeOn(Schedulers.io()).subscribe(response -> {
response.setProcessingTime((System.nanoTime() - time) / 1000000);
async.resume(response);
}, async::resume);
}
use of javax.ws.rs.container.AsyncResponse in project jersey by jersey.
the class ChatResource method postMessage.
@POST
@ManagedAsync
public String postMessage(final Message message) throws InterruptedException {
final AsyncResponse asyncResponse = suspended.take();
asyncResponse.resume(message);
return "Sent!";
}
use of javax.ws.rs.container.AsyncResponse in project pulsar by yahoo.
the class HttpDestinationLookupv2Test method testValidateReplicationSettingsOnNamespace.
@Test
public void testValidateReplicationSettingsOnNamespace() throws Exception {
final String property = "my-prop";
final String cluster = "global";
final String ns1 = "ns1";
final String ns2 = "ns2";
Policies policies1 = new Policies();
doReturn(Optional.of(policies1)).when(policiesCache).get(AdminResource.path("policies", property, cluster, ns1));
Policies policies2 = new Policies();
policies2.replication_clusters = Lists.newArrayList("invalid-localCluster");
doReturn(Optional.of(policies2)).when(policiesCache).get(AdminResource.path("policies", property, cluster, ns2));
DestinationLookup destLookup = spy(new DestinationLookup());
doReturn(false).when(destLookup).isRequestHttps();
destLookup.setPulsar(pulsar);
doReturn("null").when(destLookup).clientAppId();
Field uriField = PulsarWebResource.class.getDeclaredField("uri");
uriField.setAccessible(true);
UriInfo uriInfo = mock(UriInfo.class);
uriField.set(destLookup, uriInfo);
doReturn(false).when(config).isAuthorizationEnabled();
AsyncResponse asyncResponse = mock(AsyncResponse.class);
destLookup.lookupDestinationAsync(property, cluster, ns1, "empty-cluster", false, asyncResponse);
ArgumentCaptor<Throwable> arg = ArgumentCaptor.forClass(Throwable.class);
verify(asyncResponse).resume(arg.capture());
assertEquals(arg.getValue().getClass(), RestException.class);
AsyncResponse asyncResponse2 = mock(AsyncResponse.class);
destLookup.lookupDestinationAsync(property, cluster, ns2, "invalid-localCluster", false, asyncResponse2);
ArgumentCaptor<Throwable> arg2 = ArgumentCaptor.forClass(Throwable.class);
verify(asyncResponse2).resume(arg2.capture());
// Should have raised exception for invalid cluster
assertEquals(arg2.getValue().getClass(), RestException.class);
}
use of javax.ws.rs.container.AsyncResponse in project pulsar by yahoo.
the class HttpDestinationLookupv2Test method crossColoLookup.
@Test
public void crossColoLookup() throws Exception {
DestinationLookup destLookup = spy(new DestinationLookup());
doReturn(false).when(destLookup).isRequestHttps();
destLookup.setPulsar(pulsar);
doReturn("null").when(destLookup).clientAppId();
Field uriField = PulsarWebResource.class.getDeclaredField("uri");
uriField.setAccessible(true);
UriInfo uriInfo = mock(UriInfo.class);
uriField.set(destLookup, uriInfo);
URI uri = URI.create("http://localhost:8080/lookup/v2/destination/topic/myprop/usc/ns2/topic1");
doReturn(uri).when(uriInfo).getRequestUri();
doReturn(true).when(config).isAuthorizationEnabled();
AsyncResponse asyncResponse = mock(AsyncResponse.class);
destLookup.lookupDestinationAsync("myprop", "usc", "ns2", "topic1", false, asyncResponse);
ArgumentCaptor<Throwable> arg = ArgumentCaptor.forClass(Throwable.class);
verify(asyncResponse).resume(arg.capture());
assertEquals(arg.getValue().getClass(), WebApplicationException.class);
WebApplicationException wae = (WebApplicationException) arg.getValue();
assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
}
use of javax.ws.rs.container.AsyncResponse in project bisq-api by mrosseel.
the class TradeResource method handlePaymentStatusChange.
private void handlePaymentStatusChange(String tradeId, AsyncResponse asyncResponse, CompletableFuture<Void> completableFuture) {
completableFuture.thenApply(response -> asyncResponse.resume(Response.status(Response.Status.OK).build())).exceptionally(e -> {
final Throwable cause = e.getCause();
final Response.ResponseBuilder responseBuilder;
if (cause instanceof ValidationException) {
responseBuilder = toValidationErrorResponse(cause, 422);
} else if (cause instanceof NotFoundException) {
responseBuilder = toValidationErrorResponse(cause, 404);
} else {
final String message = cause.getMessage();
responseBuilder = Response.status(500);
if (null != message)
responseBuilder.entity(new ValidationErrorMessage(ImmutableList.of(message)));
log.error("Unable to confirm payment started for trade: " + tradeId, cause);
}
return asyncResponse.resume(responseBuilder.build());
});
}
Aggregations