Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.alibaba.dubbo.rpc;

import org.apache.dubbo.rpc.AsyncRpcResult;

@Deprecated
public interface Filter extends org.apache.dubbo.rpc.Filter {

Expand All @@ -26,8 +28,18 @@ public interface Filter extends org.apache.dubbo.rpc.Filter {
default org.apache.dubbo.rpc.Result invoke(org.apache.dubbo.rpc.Invoker<?> invoker,
org.apache.dubbo.rpc.Invocation invocation)
throws org.apache.dubbo.rpc.RpcException {
Result.CompatibleResult result = (Result.CompatibleResult) invoke(new Invoker.CompatibleInvoker<>(invoker),
new Invocation.CompatibleInvocation(invocation));
return result.getDelegate();
Result invokeResult = invoke(new Invoker.CompatibleInvoker<>(invoker),
new Invocation.CompatibleInvocation(invocation));

if (invokeResult instanceof Result.CompatibleResult) {
return invokeResult;
}

AsyncRpcResult asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(invocation);
asyncRpcResult.setValue(invokeResult.getValue());
asyncRpcResult.setException(invokeResult.getException());
asyncRpcResult.setObjectAttachments(invokeResult.getObjectAttachments());

return asyncRpcResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.alibaba.dubbo.rpc;

import org.apache.dubbo.rpc.AsyncRpcResult;

import com.alibaba.dubbo.common.URL;

@Deprecated
Expand Down Expand Up @@ -54,9 +56,22 @@ public Class<T> getInterface() {
public org.apache.dubbo.rpc.Result invoke(org.apache.dubbo.rpc.Invocation invocation) throws org.apache.dubbo.rpc.RpcException {
return new Result.CompatibleResult(invoker.invoke(invocation));
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
if (invoker instanceof Invoker) {
Result result = ((Invoker) invoker).invoke(invocation);
if (result instanceof Result.CompatibleResult) {
return result;
} else {
AsyncRpcResult asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(invocation.getOriginal());
asyncRpcResult.setValue(result.getValue());
asyncRpcResult.setException(result.getException());
asyncRpcResult.setObjectAttachments(result.getObjectAttachments());

return new Result.CompatibleResult(asyncRpcResult);
}
}
return new Result.CompatibleResult(invoker.invoke(invocation.getOriginal()));
}

Expand Down
45 changes: 16 additions & 29 deletions dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,7 @@ default Object getObjectAttachment(String key, Object defaultValue) {
return null;
}

abstract class AbstractResult implements Result {

@Override
public void setValue(Object value) {

}

@Override
public org.apache.dubbo.rpc.Result whenCompleteWithContext(BiConsumer<org.apache.dubbo.rpc.Result, Throwable> fn) {
return null;
}

@Override
public <U> CompletableFuture<U> thenApply(Function<org.apache.dubbo.rpc.Result, ? extends U> fn) {
return null;
}

@Override
public org.apache.dubbo.rpc.Result get() throws InterruptedException, ExecutionException {
return null;
}

@Override
public org.apache.dubbo.rpc.Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
}

class CompatibleResult extends AbstractResult {
class CompatibleResult implements Result {
private org.apache.dubbo.rpc.Result delegate;

public CompatibleResult(org.apache.dubbo.rpc.Result result) {
Expand Down Expand Up @@ -177,5 +149,20 @@ public void setAttachment(String key, Object value) {
public void setObjectAttachment(String key, Object value) {
delegate.setObjectAttachment(key, value);
}

@Override
public <U> CompletableFuture<U> thenApply(Function<org.apache.dubbo.rpc.Result, ? extends U> fn) {
return delegate.thenApply(fn);
}

@Override
public org.apache.dubbo.rpc.Result get() throws InterruptedException, ExecutionException {
return delegate.get();
}

@Override
public org.apache.dubbo.rpc.Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return delegate.get(timeout, unit);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc;

import org.apache.dubbo.rpc.AppResponse;

@Deprecated
public class RpcResult extends AppResponse implements com.alibaba.dubbo.rpc.Result {

public RpcResult() {
}

public RpcResult(Object result) {
super(result);
}

public RpcResult(Throwable exception) {
super(exception);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package org.apache.dubbo.filter;

import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;

import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -46,15 +46,23 @@ void testInvokeException() {
}

@Test
void testDefault() {
void testDefault() throws Throwable {
Invoker<FilterTest> invoker = new LegacyInvoker<FilterTest>(null);
Invocation invocation = new LegacyInvocation("bbb");
Result res = myFilter.invoke(invoker, invocation);
System.out.println(res);
org.apache.dubbo.rpc.Invocation invocation = new RpcInvocation(null, "echo", "DemoService", "DemoService", new Class[]{String.class}, new Object[]{"bbb"});
org.apache.dubbo.rpc.Result res = myFilter.invoke(invoker, invocation);
Assertions.assertEquals("alibaba", res.recreate());
}

@Test
void testRecreate() throws Throwable {
Invoker<FilterTest> invoker = new LegacyInvoker<FilterTest>(null);
org.apache.dubbo.rpc.Invocation invocation = new RpcInvocation(null, "echo", "DemoService", "DemoService", new Class[]{String.class}, new Object[]{"cc"});
org.apache.dubbo.rpc.Result res = myFilter.invoke(invoker, invocation);
Assertions.assertEquals("123test", res.recreate());
}

@AfterAll
public static void tear() {
Assertions.assertEquals(2, MyFilter.count);
Assertions.assertEquals(3, MyFilter.count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package org.apache.dubbo.filter;


import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.service.DemoService;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcResult;

public class LegacyInvoker<T> implements Invoker<T> {

Expand Down Expand Up @@ -58,13 +58,13 @@ public boolean isAvailable() {
}

public Result invoke(Invocation invocation) throws RpcException {
AppResponse result = new AppResponse();
RpcResult result = new RpcResult();
if (!hasException) {
result.setValue("alibaba");
} else {
result.setException(new RuntimeException("mocked exception"));
}
return new Result.CompatibleResult(result);
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcResult;


public class MyFilter implements Filter {
Expand All @@ -36,6 +37,10 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
throw new RpcException(new IllegalArgumentException("arg0 illegal"));
}

if (invocation.getArguments()[0].equals("cc")) {
return new RpcResult("123test");
}

Result tmp = invoker.invoke(invocation);
return tmp;
}
Expand Down