Skip to content
This repository was archived by the owner on May 30, 2022. It is now read-only.

Commit dd9267d

Browse files
authored
Support method annotation for rpc. (#172)
* update sofa boot 263 * sofa method annotation
1 parent d735b1b commit dd9267d

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.alipay.sofa.rpc.filter.ExcludeFilter;
2929
import com.alipay.sofa.rpc.filter.Filter;
3030
import com.alipay.sofa.rpc.server.UserThreadPool;
31+
import com.alipay.sofa.runtime.api.annotation.SofaMethod;
3132
import com.alipay.sofa.runtime.api.annotation.SofaParameter;
3233
import com.alipay.sofa.runtime.api.annotation.SofaReference;
3334
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
@@ -437,6 +438,11 @@ protected void convertServiceAnnotation(RpcBindingParam bindingParam, SofaServic
437438
if (parameters.length > 0) {
438439
bindingParam.setParameters(parseSofaParameters(parameters));
439440
}
441+
442+
SofaMethod[] sofaMethods = sofaServiceBindingAnnotation.methodInfos();
443+
if (sofaMethods.length > 0) {
444+
bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
445+
}
440446
}
441447

442448
/**
@@ -462,7 +468,6 @@ public abstract RpcBinding convert(SofaReference sofaReferenceAnnotation,
462468
protected void convertReferenceAnnotation(RpcBindingParam bindingParam,
463469
SofaReferenceBinding sofaReferenceBindingAnnotation,
464470
BindingConverterContext bindingConverterContext) {
465-
//TODO need a magic number
466471
if (sofaReferenceBindingAnnotation.addressWaitTime() != 0) {
467472
bindingParam.setAddressWaitTime(sofaReferenceBindingAnnotation.addressWaitTime());
468473
}
@@ -513,6 +518,29 @@ protected void convertReferenceAnnotation(RpcBindingParam bindingParam,
513518
if (parameters.length > 0) {
514519
bindingParam.setParameters(parseSofaParameters(parameters));
515520
}
521+
522+
SofaMethod[] sofaMethods = sofaReferenceBindingAnnotation.methodInfos();
523+
if (sofaMethods.length > 0) {
524+
bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
525+
}
526+
}
527+
528+
protected List<RpcBindingMethodInfo> parseSofaMethods(SofaMethod[] sofaMethods) {
529+
530+
List<RpcBindingMethodInfo> rpcBindingMethodInfos = new ArrayList<RpcBindingMethodInfo>();
531+
for (SofaMethod sofaMethod : sofaMethods) {
532+
RpcBindingMethodInfo rpcBindingMethodInfo = new RpcBindingMethodInfo();
533+
rpcBindingMethodInfo.setName(sofaMethod.name());
534+
rpcBindingMethodInfo.setType(sofaMethod.invokeType());
535+
rpcBindingMethodInfo.setTimeout(sofaMethod.timeout());
536+
rpcBindingMethodInfo.setRetries(sofaMethod.retries());
537+
rpcBindingMethodInfo.setCallbackClass(sofaMethod.callbackClass());
538+
rpcBindingMethodInfo.setCallbackRef(sofaMethod.callbackRef());
539+
540+
rpcBindingMethodInfos.add(rpcBindingMethodInfo);
541+
}
542+
543+
return rpcBindingMethodInfos;
516544
}
517545

518546
private Map<String, String> parseSofaParameters(SofaParameter[] parameterAnnos) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.boot.runtime.converter;
18+
19+
import com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo;
20+
import com.alipay.sofa.runtime.api.annotation.SofaMethod;
21+
import com.alipay.sofa.runtime.api.annotation.SofaReference;
22+
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
import java.util.List;
27+
28+
public class RpcBindingConverterTest {
29+
30+
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", methodInfos = { @SofaMethod(name = "test", retries = 1, invokeType = "callback", callbackClass = "class", callbackRef = "ref", timeout = 2000) }))
31+
private String testAnnotation;
32+
33+
@Test
34+
public void parseSofaMethods() {
35+
36+
RpcBindingConverter rpcBindingConverter = new BoltBindingConverter();
37+
38+
SofaReference reference = null;
39+
try {
40+
reference = RpcBindingConverterTest.class.getDeclaredField("testAnnotation").getAnnotation(
41+
SofaReference.class);
42+
} catch (NoSuchFieldException e) {
43+
e.printStackTrace();
44+
}
45+
List<RpcBindingMethodInfo> result = rpcBindingConverter.parseSofaMethods(reference.binding().methodInfos());
46+
47+
Assert.assertEquals(1, result.size());
48+
final RpcBindingMethodInfo rpcBindingMethodInfo = result.get(0);
49+
Assert.assertEquals("test", rpcBindingMethodInfo.getName());
50+
Assert.assertEquals(1, rpcBindingMethodInfo.getRetries().intValue());
51+
Assert.assertEquals("callback", rpcBindingMethodInfo.getType());
52+
Assert.assertEquals("class", rpcBindingMethodInfo.getCallbackClass());
53+
Assert.assertEquals("ref", rpcBindingMethodInfo.getCallbackRef());
54+
55+
Assert.assertEquals(2000, rpcBindingMethodInfo.getTimeout().intValue());
56+
57+
}
58+
}

sofa-boot-samples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
<dependency>
3333
<groupId>com.alipay.sofa</groupId>
3434
<artifactId>test-sofa-boot-starter</artifactId>
35-
<version>${sofaboot.version}</version>
35+
<version>2.6.2</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>com.alipay.sofa</groupId>
3939
<artifactId>healthcheck-sofa-boot-starter</artifactId>
40-
<version>${sofaboot.version}</version>
40+
<version>2.6.2</version>
4141
</dependency>
4242
<dependency>
4343
<groupId>org.apache.curator</groupId>

0 commit comments

Comments
 (0)