com.yandex.ydb.core.grpc.ServerStreamToObserver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ydb-sdk-jdbc-uberjar Show documentation
Show all versions of ydb-sdk-jdbc-uberjar Show documentation
JDBC client implementation over Table client, single jar
The newest version!
package com.yandex.ydb.core.grpc;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import com.yandex.ydb.core.rpc.StreamObserver;
import io.grpc.ClientCall;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.stub.ClientCallStreamObserver;
/**
* @author Sergey Polovko
*/
public class ServerStreamToObserver extends ClientCall.Listener {
private final StreamObserver observer;
private final CallToStreamObserverAdapter adapter;
private final Consumer errorHandler;
public ServerStreamToObserver(StreamObserver observer, ClientCall call) {
this.observer = observer;
this.adapter = new CallToStreamObserverAdapter<>(call);
this.errorHandler = null;
}
public ServerStreamToObserver(StreamObserver observer, ClientCall call, Consumer errorHandler) {
this.observer = observer;
this.adapter = new CallToStreamObserverAdapter<>(call);
this.errorHandler = errorHandler;
}
@Override
public void onHeaders(Metadata headers) {
}
@Override
public void onMessage(RespT message) {
try {
observer.onNext(message);
// request delivery of the next inbound message.
adapter.request(1);
} catch (Exception ex) {
adapter.cancel(ex.getMessage(), ex);
}
}
@Override
public void onClose(Status status, @Nullable Metadata trailers) {
if (status.isOk()) {
observer.onCompleted();
} else {
observer.onError(GrpcStatuses.toStatus(status));
if (errorHandler != null) {
errorHandler.accept(status);
}
}
}
@Override
public void onReady() {
}
private static final class CallToStreamObserverAdapter extends ClientCallStreamObserver {
private final ClientCall call;
CallToStreamObserverAdapter(ClientCall call) {
this.call = call;
}
@Override
public void onNext(T value) {
call.sendMessage(value);
}
@Override
public void onError(Throwable t) {
call.cancel("Cancelled by client with StreamObserver.onError()", t);
}
@Override
public void onCompleted() {
call.halfClose();
}
@Override
public boolean isReady() {
return call.isReady();
}
@Override
public void setOnReadyHandler(Runnable onReadyHandler) {
throw new IllegalStateException("Cannot alter onReadyHandler after call started");
}
@Override
public void disableAutoInboundFlowControl() {
throw new IllegalStateException("Cannot disable auto flow control call started");
}
@Override
public void request(int count) {
call.request(count);
}
@Override
public void setMessageCompression(boolean enable) {
call.setMessageCompression(enable);
}
@Override
public void cancel(@Nullable String message, @Nullable Throwable cause) {
call.cancel(message, cause);
}
}
}