org.apache.bookkeeper.clients.utils.RpcUtils Maven / Gradle / Ivy
/*
* Licensed 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
*
* http://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 org.apache.bookkeeper.clients.utils;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import java.util.concurrent.CompletableFuture;
/**
* RPC related utils.
*/
public class RpcUtils {
/**
* Function to create request.
*/
@FunctionalInterface
public interface CreateRequestFunc {
ReqT apply();
}
/**
* Function to process request.
*/
@FunctionalInterface
public interface ProcessRequestFunc {
ListenableFuture process(ServiceT service, ReqT req);
}
/**
* Function to process response.
*/
@FunctionalInterface
public interface ProcessResponseFunc {
void process(RespT resp, CompletableFuture resultFuture);
}
public static boolean isContainerNotFound(Throwable cause) {
if (cause instanceof StatusRuntimeException) {
return Status.NOT_FOUND == ((StatusRuntimeException) cause).getStatus();
} else if (cause instanceof StatusException) {
return Status.NOT_FOUND == ((StatusException) cause).getStatus();
} else {
return false;
}
}
public static void processRpc(
ServiceT service,
CompletableFuture result,
CreateRequestFunc createRequestFunc,
ProcessRequestFunc processRequestFunc,
ProcessResponseFunc processResponseFunc) {
ReqT request = createRequestFunc.apply();
ListenableFuture resultFuture =
processRequestFunc.process(service, request);
Futures.addCallback(
resultFuture,
new FutureCallback() {
@Override
public void onSuccess(RespT resp) {
processResponseFunc.process(resp, result);
}
@Override
public void onFailure(Throwable throwable) {
GrpcUtils.processRpcException(throwable, result);
}
}
, directExecutor());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy