Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.accidia.protoserver.misc;
 
 
import static com.google.common.base.Preconditions.checkArgument;
public class AsyncResponses {
    public static final Logger logger = LoggerFactory.getLogger(AsyncResponses.class);
    public static void addTimeoutHandler(final AsyncResponse asyncResponselong timeoutfinal TimeUnit timeUnit) {
        .debug("addTimeoutHandler(asyncResponse)");
        checkArgument(asyncResponse != null"null asyncResponse");
        checkArgument(timeUnit != null"null timeUnit");
        asyncResponse.setTimeoutHandler(new TimeoutHandler() {
            @Override
            public void handleTimeout(final AsyncResponse asyncResponse) {
                asyncResponse.resume(Response.status(..).entity("operation timeout").build());
            }
        });
        asyncResponse.setTimeout(timeouttimeUnit);
    }
    public static void addCompletionCallback(final AsyncResponse asyncResponse) {
        .debug("addCompletionCallback(asyncResponse)");
        checkArgument(asyncResponse != null"null asyncResponse");
        asyncResponse.register(new CompletionCallback() {
            @Override
            public void onComplete(final Throwable throwable) {
                if (throwable == null) {
                    .debug("completed");
                } else {
                    .warn("exception thrown: "throwable);
                }
            }
        });
    }
    public static <ResponseType> void addCallbackForListenableFuture(final AsyncResponse asyncResponse,
                                                                     final ListenableFuture<ResponseType> future) {
        .debug("addCallbackForListenableFuture(asyncResponse,future)");
        checkArgument(asyncResponse != null"null asyncResponse");
        checkArgument(future != null"null future");
        Futures.addCallback(futurenew FutureCallback<ResponseType>() {
            @Override
            public void onSuccess(final ResponseType response) {
                asyncResponse.resume(response);
            }
            @Override
            public void onFailure(final Throwable throwable) {
                asyncResponse.resume(throwable);
            }
        });
    }
New to GrepCode? Check out our FAQ X