com.github.cherrythefatbunny.reactive.dubbo.extensions.proxyfactory.ReactiveInvokerInvocationHandler Maven / Gradle / Ivy
The newest version!
package com.github.cherrythefatbunny.reactive.dubbo.extensions.proxyfactory;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.proxy.InvokerInvocationHandler;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static com.github.cherrythefatbunny.reactive.dubbo.extensions.Constants.*;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Reactive implementation of InvokerInvocationHandler which actually communicate with the real providers.
* @author cherry
*/
public class ReactiveInvokerInvocationHandler extends InvokerInvocationHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveInvokerInvocationHandler.class);
private final Invoker> invoker;
public ReactiveInvokerInvocationHandler(Invoker> handler) {
super(handler);
this.invoker = handler;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//if the invocation returns a publisher,make a publisher wrapping the real invocation
Class returnType = method.getReturnType();
if (Publisher.class.isAssignableFrom(returnType)) {
RpcInvocation invocation = createInvocation(method, args);
if (Mono.class.isAssignableFrom(returnType)) {
invocation.setAttachment(KEY_PUBLISHER_TYPE,VALUE_PUBLISHER_MONO);
return Mono.create(monoSink -> {
try {
CompletableFuture