Skip to content

Commit

Permalink
Fix the issue where @SofaService annotation cannot set a custom threa…
Browse files Browse the repository at this point in the history
…d pool when the interfaceType is empty (sofastack#1247)
  • Loading branch information
HzjNeverStop authored and 致节 committed Sep 25, 2023
1 parent c6e4418 commit d18e8e0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ protected void convertServiceAnnotation(RpcBindingParam bindingParam,

UserThreadPool threadPoolObj = (UserThreadPool) applicationContext.getBean(threadPool);

String interfaceName = sofaServiceAnnotation.interfaceType().getCanonicalName();
String interfaceName = bindingConverterContext.getInterfaceType().getCanonicalName();
String uniqId = sofaServiceAnnotation.uniqueId();
String uniqueName = interfaceName + ":1.0"
+ (StringUtils.isEmpty(uniqId) ? "" : ":" + uniqId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ public class SofaBootRpcAllTest {
private GenericService genericService;

@Autowired
@Qualifier("threadPoolService")
private ThreadPoolService threadPoolService;

@Autowired
@Qualifier("threadPoolAnnotationService")
private ThreadPoolService threadPoolAnnotationService;

@Autowired
private RestService restService;

Expand Down Expand Up @@ -233,7 +238,8 @@ public void testGeneric() {
public void testThreadPool() {
Assert.assertTrue(threadPoolService.sayThreadPool("threadPool").startsWith(
"threadPool[SOFA-customerThreadPool_name"));

Assert.assertTrue(threadPoolAnnotationService.sayThreadPool("threadPool").startsWith(
"threadPool[SOFA-customerThreadPool_name"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.alipay.sofa.rpc.boot.test.bean.threadpool;

import com.alipay.sofa.runtime.api.annotation.SofaServiceBean;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;

/**
* @author huzijie
* @version ThreadPoolServiceAnnotationImpl.java, v 0.1 2023年09月19日 6:05 PM huzijie Exp $
*/
@SofaServiceBean(uniqueId = "annotation", bindings = { @SofaServiceBinding(bindingType = "bolt", userThreadPool = "customerThreadPool") })
public class ThreadPoolServiceAnnotationImpl implements ThreadPoolService {

@Override
public String sayThreadPool(String string) {
return string + "[" + Thread.currentThread().getName() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
interface="com.alipay.sofa.rpc.boot.test.bean.threadpool.ThreadPoolService">
<sofa:binding.bolt/>
</sofa:reference>
<sofa:reference jvm-first="false" id="threadPoolAnnotationService" unique-id="annotation"
interface="com.alipay.sofa.rpc.boot.test.bean.threadpool.ThreadPoolService">
<sofa:binding.bolt/>
</sofa:reference>

<!-- rest -->
<bean id="restServiceImpl" class="com.alipay.sofa.rpc.boot.test.bean.rest.RestServiceImpl"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class BindingConverterContext {

private String repeatReferLimit;

private Class interfaceType;

public ClassLoader getAppClassLoader() {
return appClassLoader;
}
Expand Down Expand Up @@ -94,4 +96,12 @@ public String getRepeatReferLimit() {
public void setRepeatReferLimit(String repeatReferLimit) {
this.repeatReferLimit = repeatReferLimit;
}

public Class getInterfaceType() {
return interfaceType;
}

public void setInterfaceType(Class interfaceType) {
this.interfaceType = interfaceType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ private void generateSofaServiceDefinition(String beanId, SofaService sofaServic
interfaceType);
builder.addPropertyValue(AbstractContractDefinitionParser.UNIQUE_ID_PROPERTY,
sofaServiceAnnotation.uniqueId());
builder.addPropertyValue(AbstractContractDefinitionParser.BINDINGS,
getSofaServiceBinding(sofaServiceAnnotation, sofaServiceAnnotation.bindings()));
builder.addPropertyValue(
AbstractContractDefinitionParser.BINDINGS,
getSofaServiceBinding(sofaServiceAnnotation, sofaServiceAnnotation.bindings(),
interfaceType));
builder.addPropertyReference(ServiceDefinitionParser.REF, beanId);
builder.addPropertyValue(ServiceDefinitionParser.BEAN_ID, beanId);
builder.addPropertyValue(AbstractContractDefinitionParser.DEFINITION_BUILDING_API_TYPE,
Expand All @@ -342,7 +344,8 @@ private void generateSofaServiceDefinition(String beanId, SofaService sofaServic
}

private List<Binding> getSofaServiceBinding(SofaService sofaServiceAnnotation,
SofaServiceBinding[] sofaServiceBindings) {
SofaServiceBinding[] sofaServiceBindings,
Class<?> interfaceType) {
List<Binding> bindings = new ArrayList<>();
for (SofaServiceBinding sofaServiceBinding : sofaServiceBindings) {
BindingConverter bindingConverter = bindingConverterFactory
Expand All @@ -356,6 +359,7 @@ private List<Binding> getSofaServiceBinding(SofaService sofaServiceAnnotation,
bindingConverterContext.setApplicationContext(applicationContext);
bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
bindingConverterContext.setInterfaceType(interfaceType);
Binding binding = bindingConverter.convert(sofaServiceAnnotation, sofaServiceBinding,
bindingConverterContext);
bindings.add(binding);
Expand Down

0 comments on commit d18e8e0

Please sign in to comment.