Search in sources :

Example 1 with ReactiveMethodHandler

use of com.alibaba.rsocket.rpc.ReactiveMethodHandler in project alibaba-rsocket-broker by alibaba.

the class RSocketResponderSupport method localRequestChannel.

@SuppressWarnings("ReactiveStreamsNullableInLambdaInTransform")
public Flux<Payload> localRequestChannel(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, @Nullable MessageAcceptMimeTypesMetadata messageAcceptMimeTypesMetadata, Payload signal, Flux<Payload> payloads) {
    try {
        ReactiveMethodHandler methodHandler = localServiceCaller.getInvokeMethod(routing.getService(), routing.getMethod());
        if (methodHandler != null) {
            Object result;
            if (methodHandler.getParamCount() == 1) {
                Flux<Object> paramFlux = payloads.map(payload -> {
                    return encodingFacade.decodeResult(dataEncodingMetadata.getRSocketMimeType(), payload.data(), methodHandler.getInferredClassForParameter(0));
                });
                Object lastParam = methodHandler.getReactiveAdapter().fromPublisher(paramFlux, methodHandler.getLastParamType());
                result = methodHandler.invoke(lastParam);
            } else {
                Object paramFirst = encodingFacade.decodeResult(dataEncodingMetadata.getRSocketMimeType(), signal.data(), methodHandler.getParameterTypes()[0]);
                Flux<Object> paramFlux = payloads.map(payload -> {
                    return encodingFacade.decodeResult(dataEncodingMetadata.getRSocketMimeType(), payload.data(), methodHandler.getInferredClassForParameter(1));
                });
                Object lastParam = methodHandler.getReactiveAdapter().fromPublisher(paramFlux, methodHandler.getLastParamType());
                result = methodHandler.invoke(paramFirst, lastParam);
            }
            if (result instanceof Mono) {
                result = Flux.from((Mono<?>) result);
            } else {
                result = methodHandler.getReactiveAdapter().toFlux(result);
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(messageAcceptMimeTypesMetadata, dataEncodingMetadata.getRSocketMimeType(), methodHandler);
            // result return
            return ((Flux<?>) result).map(object -> encodingFacade.encodingResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, getCompositeMetadataWithEncoding(resultEncodingType.getType())));
        } else {
            return Flux.error(new InvalidException(RsocketErrorCode.message("RST-201404", routing.getService(), routing.getMethod())));
        }
    } catch (Exception e) {
        log.error(RsocketErrorCode.message("RST-200500"), e);
        return Flux.error(new InvalidException(RsocketErrorCode.message("RST-900500", e.getMessage())));
    }
}
Also used : Logger(org.slf4j.Logger) RsocketErrorCode(com.alibaba.rsocket.observability.RsocketErrorCode) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) HashMap(java.util.HashMap) InvalidException(io.rsocket.exceptions.InvalidException) Unpooled(io.netty.buffer.Unpooled) Nullable(org.jetbrains.annotations.Nullable) Flux(reactor.core.publisher.Flux) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) ByteBuf(io.netty.buffer.ByteBuf) Payload(io.rsocket.Payload) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) Map(java.util.Map) com.alibaba.rsocket.metadata(com.alibaba.rsocket.metadata) LocalReactiveServiceCaller(com.alibaba.rsocket.rpc.LocalReactiveServiceCaller) AbstractRSocket(com.alibaba.rsocket.AbstractRSocket) RSocketEncodingFacade(com.alibaba.rsocket.encoding.RSocketEncodingFacade) ByteBufPayload(io.rsocket.util.ByteBufPayload) Mono(reactor.core.publisher.Mono) InvalidException(io.rsocket.exceptions.InvalidException) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) InvalidException(io.rsocket.exceptions.InvalidException)

Example 2 with ReactiveMethodHandler

use of com.alibaba.rsocket.rpc.ReactiveMethodHandler in project alibaba-rsocket-broker by alibaba.

the class RSocketResponderSupport method localFireAndForget.

protected Mono<Void> localFireAndForget(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, Payload payload) {
    ReactiveMethodHandler methodHandler = localServiceCaller.getInvokeMethod(routing.getService(), routing.getMethod());
    if (methodHandler != null) {
        if (methodHandler.isAsyncReturn()) {
            try {
                return methodHandler.getReactiveAdapter().toMono(invokeLocalService(methodHandler, dataEncodingMetadata, payload));
            } catch (Exception e) {
                ReferenceCountUtil.safeRelease(payload);
                log.error(RsocketErrorCode.message("RST-200500"), e);
                return Mono.error(e);
            }
        } else {
            return Mono.create((sink) -> {
                try {
                    invokeLocalService(methodHandler, dataEncodingMetadata, payload);
                    sink.success();
                } catch (Exception e) {
                    log.error(RsocketErrorCode.message("RST-200500"), e);
                    sink.error(e);
                }
            });
        }
    } else {
        ReferenceCountUtil.safeRelease(payload);
        return Mono.error(new InvalidException(RsocketErrorCode.message("RST-201404", routing.getService(), routing.getMethod())));
    }
}
Also used : InvalidException(io.rsocket.exceptions.InvalidException) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) InvalidException(io.rsocket.exceptions.InvalidException)

Example 3 with ReactiveMethodHandler

use of com.alibaba.rsocket.rpc.ReactiveMethodHandler in project alibaba-rsocket-broker by alibaba.

the class RSocketResponderSupport method localRequestResponse.

protected Mono<Payload> localRequestResponse(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, @Nullable MessageAcceptMimeTypesMetadata messageAcceptMimeTypesMetadata, Payload payload) {
    try {
        ReactiveMethodHandler methodHandler = localServiceCaller.getInvokeMethod(routing.getService(), routing.getMethod());
        if (methodHandler != null) {
            Object result;
            if (methodHandler.isAsyncReturn()) {
                result = invokeLocalService(methodHandler, dataEncodingMetadata, payload);
            } else {
                result = Mono.create((sink) -> {
                    try {
                        Object resultObj = invokeLocalService(methodHandler, dataEncodingMetadata, payload);
                        if (resultObj == null) {
                            sink.success();
                        } else if (resultObj instanceof Mono) {
                            Mono<Object> monoObj = (Mono<Object>) resultObj;
                            monoObj.doOnError(sink::error).doOnNext(sink::success).thenEmpty(Mono.fromRunnable(sink::success)).subscribe();
                        } else {
                            sink.success(resultObj);
                        }
                    } catch (Exception e) {
                        sink.error(e);
                    }
                });
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(messageAcceptMimeTypesMetadata, dataEncodingMetadata.getRSocketMimeType(), methodHandler);
            Mono<Object> monoResult;
            if (result instanceof Mono) {
                monoResult = (Mono) result;
            } else {
                monoResult = methodHandler.getReactiveAdapter().toMono(result);
            }
            return monoResult.map(object -> encodingFacade.encodingResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, getCompositeMetadataWithEncoding(resultEncodingType.getType())));
        } else {
            ReferenceCountUtil.safeRelease(payload);
            return Mono.error(new InvalidException(RsocketErrorCode.message("RST-201404", routing.getService(), routing.getMethod())));
        }
    } catch (Exception e) {
        log.error(RsocketErrorCode.message("RST-200500"), e);
        ReferenceCountUtil.safeRelease(payload);
        return Mono.error(new InvalidException(RsocketErrorCode.message("RST-900500", e.getMessage())));
    }
}
Also used : Logger(org.slf4j.Logger) RsocketErrorCode(com.alibaba.rsocket.observability.RsocketErrorCode) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) HashMap(java.util.HashMap) InvalidException(io.rsocket.exceptions.InvalidException) Unpooled(io.netty.buffer.Unpooled) Nullable(org.jetbrains.annotations.Nullable) Flux(reactor.core.publisher.Flux) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) ByteBuf(io.netty.buffer.ByteBuf) Payload(io.rsocket.Payload) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) Map(java.util.Map) com.alibaba.rsocket.metadata(com.alibaba.rsocket.metadata) LocalReactiveServiceCaller(com.alibaba.rsocket.rpc.LocalReactiveServiceCaller) AbstractRSocket(com.alibaba.rsocket.AbstractRSocket) RSocketEncodingFacade(com.alibaba.rsocket.encoding.RSocketEncodingFacade) ByteBufPayload(io.rsocket.util.ByteBufPayload) Mono(reactor.core.publisher.Mono) InvalidException(io.rsocket.exceptions.InvalidException) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) InvalidException(io.rsocket.exceptions.InvalidException)

Example 4 with ReactiveMethodHandler

use of com.alibaba.rsocket.rpc.ReactiveMethodHandler in project alibaba-rsocket-broker by alibaba.

the class RSocketResponderSupport method localRequestStream.

protected Flux<Payload> localRequestStream(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, @Nullable MessageAcceptMimeTypesMetadata messageAcceptMimeTypesMetadata, Payload payload) {
    try {
        ReactiveMethodHandler methodHandler = localServiceCaller.getInvokeMethod(routing.getService(), routing.getMethod());
        if (methodHandler != null) {
            Object result = invokeLocalService(methodHandler, dataEncodingMetadata, payload);
            Flux<Object> fluxResult;
            if (result instanceof Flux) {
                fluxResult = (Flux<Object>) result;
            } else {
                fluxResult = methodHandler.getReactiveAdapter().toFlux(result);
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(messageAcceptMimeTypesMetadata, dataEncodingMetadata.getRSocketMimeType(), methodHandler);
            return fluxResult.map(object -> encodingFacade.encodingResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, getCompositeMetadataWithEncoding(resultEncodingType.getType())));
        } else {
            ReferenceCountUtil.safeRelease(payload);
            return Flux.error(new InvalidException(RsocketErrorCode.message("RST-201404", routing.getService(), routing.getMethod())));
        }
    } catch (Exception e) {
        log.error(RsocketErrorCode.message("RST-200500"), e);
        ReferenceCountUtil.safeRelease(payload);
        return Flux.error(new InvalidException(RsocketErrorCode.message("RST-900500", e.getMessage())));
    }
}
Also used : Logger(org.slf4j.Logger) RsocketErrorCode(com.alibaba.rsocket.observability.RsocketErrorCode) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) HashMap(java.util.HashMap) InvalidException(io.rsocket.exceptions.InvalidException) Unpooled(io.netty.buffer.Unpooled) Nullable(org.jetbrains.annotations.Nullable) Flux(reactor.core.publisher.Flux) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) ByteBuf(io.netty.buffer.ByteBuf) Payload(io.rsocket.Payload) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) Map(java.util.Map) com.alibaba.rsocket.metadata(com.alibaba.rsocket.metadata) LocalReactiveServiceCaller(com.alibaba.rsocket.rpc.LocalReactiveServiceCaller) AbstractRSocket(com.alibaba.rsocket.AbstractRSocket) RSocketEncodingFacade(com.alibaba.rsocket.encoding.RSocketEncodingFacade) ByteBufPayload(io.rsocket.util.ByteBufPayload) Flux(reactor.core.publisher.Flux) InvalidException(io.rsocket.exceptions.InvalidException) ReactiveMethodHandler(com.alibaba.rsocket.rpc.ReactiveMethodHandler) InvalidException(io.rsocket.exceptions.InvalidException)

Aggregations

ReactiveMethodHandler (com.alibaba.rsocket.rpc.ReactiveMethodHandler)4 InvalidException (io.rsocket.exceptions.InvalidException)4 AbstractRSocket (com.alibaba.rsocket.AbstractRSocket)3 RSocketEncodingFacade (com.alibaba.rsocket.encoding.RSocketEncodingFacade)3 com.alibaba.rsocket.metadata (com.alibaba.rsocket.metadata)3 RsocketErrorCode (com.alibaba.rsocket.observability.RsocketErrorCode)3 LocalReactiveServiceCaller (com.alibaba.rsocket.rpc.LocalReactiveServiceCaller)3 ByteBuf (io.netty.buffer.ByteBuf)3 Unpooled (io.netty.buffer.Unpooled)3 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)3 Payload (io.rsocket.Payload)3 ByteBufPayload (io.rsocket.util.ByteBufPayload)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Nullable (org.jetbrains.annotations.Nullable)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3