@GET public void getList(@Suspended final AsyncResponse ar) throws NamingException { ar.setTimeoutHandler(new TimeoutHandler() { @Override public void handleTimeout(AsyncResponse ar) { ar.resume("Operation timed out"); } }); ar.setTimeout(4000, TimeUnit.MILLISECONDS); ar.register(new MyCompletionCallback()); ar.register(new MyConnectionCallback()); ManagedThreadFactory threadFactory = (ManagedThreadFactory) new InitialContext() .lookup("java:comp/DefaultManagedThreadFactory"); Executors.newSingleThreadExecutor(threadFactory).submit(new Runnable() { @Override public void run() { try { Thread.sleep(3000); ar.resume(response[0]); } catch (InterruptedException ex) { } } }); }
asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
@Path("/callback") public class AsyncCallback { @POST @Consumes(MediaType.APPLICATION_JSON) public void postWithAsync(@Suspended AsyncResponse asyncResponse, SomeObject object) { asyncResponse.register(new CompletionCallback() { @Override public void onComplete(Throwable error) { if (error == null) { System.out.println("Processing new Request"); } else { System.out.println("Exception in Request handling"); } } }); Response response = Response.ok("Success").build(); // Carry on like nothing happened asyncResponse.resume(response); } }
private void configResponse(final AsyncResponse asyncResponse) { asyncResponse.register((CompletionCallback)throwable -> { if (throwable == null) { log.info("CompletionCallback-onComplete: OK"); } else { log.info("CompletionCallback-onComplete: ERROR: " + throwable.getMessage()); } }); asyncResponse.register((ConnectionCallback)disconnected -> { //Status.GONE=410 log.info("ConnectionCallback-onDisconnect"); disconnected.resume(Response.status(Response.Status.GONE).entity("disconnect!").build()); }); asyncResponse.setTimeoutHandler(r -> { //Status.SERVICE_UNAVAILABLE=503 log.info("TIMEOUT"); r.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Operation time out.").build()); }); asyncResponse.setTimeout(TIMEOUT, TimeUnit.SECONDS); }
@GET @Path("books/notfound/unmapped") @Produces("text/plain") public void handleContinuationRequestNotFoundUnmapped(@Suspended AsyncResponse response) { response.register(new CallbackImpl()); resumeSuspendedNotFoundUnmapped(response); }
@GET @Produces( "text/plain" ) public void poll( @Suspended final AsyncResponse response, @NotNull @HeaderParam( SharedConstants.CONNECTION_ID_HEADER ) final String sessionId, @NotNull @QueryParam( SharedConstants.RECEIVE_SEQUENCE_PARAM ) final int rxSequence ) { response.setTimeout( getPollTime(), TimeUnit.SECONDS ); response.register( (ConnectionCallback) this::doDisconnect ); response.setTimeoutHandler( this::doTimeout ); try { final String data = poll( sessionId, rxSequence ); if ( null != data ) { resume( response, data ); } else { _requests.put( response, new SuspendedRequest( sessionId, rxSequence, response ) ); } } catch ( final Exception e ) { handleException( sessionId, response, e ); } }
@GET @Path("books/unmappedFromFilter") @Produces("text/plain") public void handleContinuationRequestUnmappedFromFilter(@Suspended AsyncResponse response) { response.register(new CallbackImpl()); response.resume(Response.ok().build()); }
asyncResponse.register((CompletionCallback) throwable -> notificationService.unsubscribe(Collections.singleton(pair.getLeft()))); } else { if (!asyncResponse.isDone()) {
@GET @Path("books/notfound") @Produces("text/plain") public void handleContinuationRequestNotFound(@Suspended AsyncResponse response) { response.register(new CallbackImpl()); resumeSuspendedNotFound(response); }
@GET @Path("/books/defaulttimeout") public void getBookDescriptionWithTimeout(@Suspended AsyncResponse async) { async.register(new CallbackImpl()); async.setTimeout(2000, TimeUnit.MILLISECONDS); }
@GET @Path("/disconnect") public void handleClientDisconnects(@Suspended AsyncResponse response) { response.setTimeout(0, TimeUnit.SECONDS); response.register(new ConnectionCallback() { @Override public void onDisconnect(AsyncResponse disconnected) { System.out.println("ConnectionCallback: onDisconnect, client disconnects"); } }); try { Thread.sleep(3000); } catch (InterruptedException ex) { // ignore } response.resume(books.values().toString()); }
asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));
asyncResponse.register((CompletionCallback) throwable -> commandService.sendUnsubscribeRequest(Collections.singleton(pair.getLeft()))); } else { if (!asyncResponse.isDone()) {
asyncResponse.register((CompletionCallback) throwable -> { try { commandService.sendUnsubscribeRequest(Collections.singleton(future.get().getLeft()));