use of org.kin.rsocket.core.metadata.MessageMimeTypeMetadata in project kin-rsocket-broker by huangjianqin.
the class RSocketResponderHandlerSupport method localRequestChannel.
/**
* 本地调用服务接口方法并针对RequestChannel Frame Type场景定制额外逻辑
*/
@SuppressWarnings("ReactiveStreamsNullableInLambdaInTransform")
protected Flux<Payload> localRequestChannel(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload signal, Flux<Payload> payloads) {
String service = routing.getService();
String handler = routing.getHandler();
try {
ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
if (methodInvoker != null) {
Object result;
if (methodInvoker.getParamCount() == 1) {
Flux<Object> paramFlux = payloads.map(payload -> Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), payload.data(), methodInvoker.getInferredClassForParameter(0)));
result = methodInvoker.invoke(paramFlux);
} else {
Object paramFirst = Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), signal.data(), methodInvoker.getParameterTypes()[0]);
Flux<Object> paramFlux = payloads.map(payload -> Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), payload.data(), methodInvoker.getInferredClassForParameter(1)));
result = methodInvoker.invoke(paramFirst, paramFlux);
}
// composite data for return value
RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
// result return
return ReactiveObjAdapter.INSTANCE.toFlux(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
} else {
return Flux.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
}
} catch (Exception e) {
error(failCallLog(service, handler), e);
// release
ReferenceCountUtil.safeRelease(signal);
payloads.subscribe(ReferenceCountUtil::safeRelease);
return Flux.error(new InvalidException(failCallTips(service, handler, e)));
}
}
use of org.kin.rsocket.core.metadata.MessageMimeTypeMetadata in project kin-rsocket-broker by huangjianqin.
the class RSocketResponderHandlerSupport method localRequestResponse.
/**
* 本地调用服务接口方法并针对RequestResponse Frame Type场景定制额外逻辑
*/
protected Mono<Payload> localRequestResponse(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload payload) {
String service = routing.getService();
String handler = routing.getHandler();
try {
ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
if (methodInvoker != null) {
Object result;
if (methodInvoker.isAsyncReturn()) {
result = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
} else {
result = Mono.create((sink) -> {
try {
Object resultObj = invokeServiceMethod(methodInvoker, 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) {
error(failCallLog(service, handler), e);
sink.error(e);
}
});
}
// composite data for return value
RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
return ReactiveObjAdapter.INSTANCE.toMono(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
} else {
ReferenceCountUtil.safeRelease(payload);
return Mono.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
}
} catch (Exception e) {
error(failCallLog(service, handler), e);
ReferenceCountUtil.safeRelease(payload);
return Mono.error(new InvalidException(failCallTips(service, handler, e)));
}
}
use of org.kin.rsocket.core.metadata.MessageMimeTypeMetadata in project kin-rsocket-broker by huangjianqin.
the class RSocketResponderHandlerSupport method localRequestStream.
/**
* 本地调用服务接口方法并针对RequestStream Frame Type场景定制额外逻辑
*/
protected Flux<Payload> localRequestStream(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload payload) {
String service = routing.getService();
String handler = routing.getHandler();
try {
ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
if (methodInvoker != null) {
Object result = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
// composite data for return value
RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
return ReactiveObjAdapter.INSTANCE.toFlux(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
} else {
ReferenceCountUtil.safeRelease(payload);
return Flux.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
}
} catch (Exception e) {
error(failCallLog(service, handler), e);
ReferenceCountUtil.safeRelease(payload);
return Flux.error(new InvalidException(failCallTips(service, handler, e)));
}
}
Aggregations