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())));
}
}
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())));
}
}
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())));
}
}
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())));
}
}
Aggregations