Skip to content

Commit

Permalink
Fix method annotation callback do not work problem (#7754)
Browse files Browse the repository at this point in the history
* fix method annotation callback

* fix method callback annotation do not work issue

* remove log file

* remove static import
  • Loading branch information
codeimport authored May 15, 2021
1 parent ccbcccf commit dee7373
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,28 @@ public MethodConfig(Method method) {

this.setReturn(method.isReturn());

if(!"".equals(method.oninvoke())){
this.setOninvoke(method.oninvoke());
String split = ".";

if (!"".equals(method.oninvoke()) && method.oninvoke().lastIndexOf(split) > 0) {
int index = method.oninvoke().lastIndexOf(split);
String ref = method.oninvoke().substring(0, index);
String methodName = method.oninvoke().substring(index + 1);
this.setOninvoke(ref);
this.setOninvokeMethod(methodName);
}
if(!"".equals(method.onreturn())){
this.setOnreturn(method.onreturn());
if (!"".equals(method.onreturn()) && method.onreturn().lastIndexOf(split) > 0) {
int index = method.onreturn().lastIndexOf(split);
String ref = method.onreturn().substring(0, index);
String methodName = method.onreturn().substring(index + 1);
this.setOnreturn(ref);
this.setOnreturnMethod(methodName);
}
if(!"".equals(method.onthrow())){
this.setOnthrow(method.onthrow());
if (!"".equals(method.onthrow()) && method.onthrow().lastIndexOf(split) > 0) {
int index = method.onthrow().lastIndexOf(split);
String ref = method.onthrow().substring(0, index);
String methodName = method.onthrow().substring(index + 1);
this.setOnthrow(ref);
this.setOnthrowMethod(methodName);
}

if (method.arguments() != null && method.arguments().length != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ public class MethodConfigTest {
private static final int EXECUTES = 5;
private static final boolean DEPERECATED = true;
private static final boolean STICKY = true;
private static final String ONINVOKE = "i";
private static final String ONTHROW = "t";
private static final String ONRETURN = "r";
private static final String ONINVOKE = "instance.i";
private static final String ONTHROW = "instance.t";
private static final String ONRETURN = "instance.r";
private static final String ONINVOKEMETHOD = "i";
private static final String ONTHROWMETHOD = "t";
private static final String ONRETURNMETHOD = "r";
private static final String CACHE = "c";
private static final String VALIDATION = "v";
private static final int ARGUMENTS_INDEX = 24;
Expand All @@ -82,9 +85,9 @@ public void testStaticConstructor() throws NoSuchFieldException {
assertThat(EXECUTES, equalTo(methodConfig.getExecutes().intValue()));
assertThat(DEPERECATED, equalTo(methodConfig.getDeprecated()));
assertThat(STICKY, equalTo(methodConfig.getSticky()));
assertThat(ONINVOKE, equalTo(methodConfig.getOninvoke()));
assertThat(ONTHROW, equalTo(methodConfig.getOnthrow()));
assertThat(ONRETURN, equalTo(methodConfig.getOnreturn()));
assertThat(ONINVOKEMETHOD, equalTo(methodConfig.getOninvokeMethod()));
assertThat(ONTHROWMETHOD, equalTo(methodConfig.getOnthrowMethod()));
assertThat(ONRETURNMETHOD, equalTo(methodConfig.getOnreturnMethod()));
assertThat(CACHE, equalTo(methodConfig.getCache()));
assertThat(VALIDATION, equalTo(methodConfig.getValidation()));
assertThat(ARGUMENTS_INDEX, equalTo(methodConfig.getArguments().get(0).getIndex().intValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ public void testConstructWithReferenceAnnotation() throws NoSuchFieldException {
Assertions.assertEquals(3, (int) ((MethodConfig) referenceConfig.getMethods().get(0)).getActives());
Assertions.assertEquals(5, (int) ((MethodConfig) referenceConfig.getMethods().get(0)).getExecutes());
Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).isAsync());
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOninvoke(), "i");
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnreturn(), "r");
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnthrow(), "t");
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOninvokeMethod(), "i");
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnreturnMethod(), "r");
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnthrowMethod(), "t");
Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getCache(), "c");
}


@Reference(methods = {@Method(name = "sayHello", timeout = 1300, retries = 4, loadbalance = "random", async = true,
actives = 3, executes = 5, deprecated = true, sticky = true, oninvoke = "i", onthrow = "t", onreturn = "r", cache = "c", validation = "v",
actives = 3, executes = 5, deprecated = true, sticky = true, oninvoke = "instance.i", onthrow = "instance.t", onreturn = "instance.r", cache = "c", validation = "v",
arguments = {@Argument(index = 24, callback = true, type = "sss")})})
private InnerTest innerTest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ private void configureConsumerConfig(AnnotationAttributes attributes, ReferenceB
void configureMethodConfig(AnnotationAttributes attributes, ReferenceBean<?> referenceBean) {
Method[] methods = (Method[]) attributes.get("methods");
List<MethodConfig> methodConfigs = MethodConfig.constructMethodConfig(methods);

for (MethodConfig methodConfig : methodConfigs) {
if (!StringUtils.isEmpty(methodConfig.getOninvoke())) {
Object invokeRef = this.applicationContext.getBean((String) methodConfig.getOninvoke());
methodConfig.setOninvoke(invokeRef);
}
if (!StringUtils.isEmpty(methodConfig.getOnreturn())) {
Object returnRef = this.applicationContext.getBean((String) methodConfig.getOnreturn());
methodConfig.setOnreturn(returnRef);
}
if (!StringUtils.isEmpty(methodConfig.getOnthrow())) {
Object throwRef = this.applicationContext.getBean((String) methodConfig.getOnthrow());
methodConfig.setOnthrow(throwRef);
}
}

if (!methodConfigs.isEmpty()) {
referenceBean.setMethods(methodConfigs);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 org.apache.dubbo.config.spring.api;

public interface MethodCallback {
void oninvoke(String request);

void onreturn(String response, String request);

void onthrow(Throwable ex, String request);

String getOnInvoke();

String getOnReturn();

String getOnThrow();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 org.apache.dubbo.config.spring.beans.factory.annotation;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.spring.api.HelloService;
import org.apache.dubbo.config.spring.api.MethodCallback;
import org.apache.dubbo.config.spring.impl.MethodCallbackImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.event.annotation.AfterTestMethod;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(
classes = {
ServiceAnnotationTestConfiguration.class,
MethodConfigCallbackTest.class,
})
@TestPropertySource(properties = {
"packagesToScan = org.apache.dubbo.config.spring.context.annotation.provider",
"consumer.version = ${demo.service.version}",
"consumer.url = dubbo://127.0.0.1:12345?version=2.5.7",
})
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class MethodConfigCallbackTest {

@AfterTestMethod
public void setUp() {
DubboBootstrap.reset();
}

@Bean("referenceAnnotationBeanPostProcessor")
public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor() {
return new ReferenceAnnotationBeanPostProcessor();
}

@Autowired
private ConfigurableApplicationContext context;

@Bean("methodCallback")
public MethodCallback methodCallback() {
return new MethodCallbackImpl();
}

@DubboReference(check = false,methods = {@Method(name = "sayHello",oninvoke = "methodCallback.oninvoke",onreturn = "methodCallback.onreturn",onthrow = "methodCallback.onthrow")})
private HelloService helloServiceMethodCallBack;

@Test
public void testMethodAnnotationCallBack() {
helloServiceMethodCallBack.sayHello("dubbo");
MethodCallback notify = (MethodCallback) context.getBean("methodCallback");
Assertions.assertEquals("dubbo invoke success", notify.getOnInvoke());
Assertions.assertEquals("dubbo return success", notify.getOnReturn());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 org.apache.dubbo.config.spring.impl;

import org.apache.dubbo.config.spring.api.MethodCallback;

public class MethodCallbackImpl implements MethodCallback {
private String onInvoke;
private String onReturn;
private String onThrow;

@Override
public void oninvoke(String request) {
this.onInvoke = "dubbo invoke success";
}

@Override
public void onreturn(String response, String request) {
this.onReturn = "dubbo return success";
}

@Override
public void onthrow(Throwable ex, String request) {
this.onThrow = "dubbo throw exception";
}

public String getOnInvoke() {
return this.onInvoke;
}

public String getOnReturn() {
return this.onReturn;
}

public String getOnThrow() {
return this.onThrow;
}
}

0 comments on commit dee7373

Please sign in to comment.