All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linecorp.armeria.common.thrift.AsyncMethodCallbacks Maven / Gradle / Ivy

Go to download

Asynchronous HTTP/2 RPC/REST client/server library built on top of Java 8, Netty, Thrift and gRPC (armeria-thrift0.9)

The newest version!
/*
 * Copyright 2018 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.linecorp.armeria.common.thrift;

import static java.util.Objects.requireNonNull;

import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;

import org.apache.thrift.async.AsyncMethodCallback;

import com.linecorp.armeria.common.util.CompletionActions;

/**
 * A utility class that bridges the gap between {@link CompletionStage} and {@link AsyncMethodCallback}.
 */
public final class AsyncMethodCallbacks {

    /**
     * Adds a callback that transfers the outcome of the specified {@link CompletionStage} to the specified
     * {@link AsyncMethodCallback}.
     *
     * 
{@code
     * > public class MyThriftService implements ThriftService.AsyncIface {
     * >     @Override
     * >     public void myServiceMethod(AsyncMethodCallback callback) {
     * >         final CompletableFuture future = ...;
     * >         AsyncMethodCallbacks.transfer(future, callback);
     * >     }
     * > }
     * }
*/ public static void transfer(CompletionStage src, AsyncMethodCallback dest) { requireNonNull(src, "src"); requireNonNull(dest, "dest"); src.handle((res, cause) -> { try { if (cause != null) { invokeOnError(dest, cause); } else { dest.onComplete(res); } } catch (Exception e) { CompletionActions.log(e); } return null; }); } /** * Invokes {@link AsyncMethodCallback#onError(Exception)}. If the specified {@code cause} is not an * {@link Exception}, it will be wrapped with a {@link CompletionException}. */ public static void invokeOnError(AsyncMethodCallback callback, Throwable cause) { requireNonNull(callback, "callback"); requireNonNull(cause, "cause"); if (cause instanceof Exception) { callback.onError((Exception) cause); } else { callback.onError(new CompletionException(cause.toString(), cause)); } } private AsyncMethodCallbacks() {} }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy