1、CommonAsncInvokeServiceImpl类代码:
package com.example.gradleproject.service.impl;
import com.example.gradleproject.service.CommonAsncInvokeService;
import com.example.gradleproject.util.SpringContextUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;import org.springframework.stereotype.Service;
import java.lang.reflect.Method;
import java.util.concurrent.Future;
@Service
public class CommonAsncInvokeServiceImpl implements CommonAsncInvokeService {
static Log log = LogFactory.getLog(CommonAsncInvokeServiceImpl.class);
@Async
@Override
public <T> Future<?> asyncInvoke(Class<T> serviceType, String methodName, Object... params) {
Object result = null;
try {
T service = SpringContextUtil.getBean(serviceType);
Method[] declaredMethods = serviceType.getDeclaredMethods();
for (int i = 0; i < declaredMethods.length; i++) {
if (declaredMethods[i].getName().equals(methodName)) {
Method method = declaredMethods[i];
method.setAccessible(Boolean.TRUE);
result = method.invoke(service, params);
break;
}
}
} catch (Exception e) {
log.error("CommonAsncInvokeServiceImpl asyncInvoke error:", e);
}
return new AsyncResult<>(result);
}
@Override
public long getApolloAsyncInvokeTimeOut() {
return 1000;//todo
}
}
2、SpringContextUtil 类:
package com.example.gradleproject.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) throws BeansException {
return applicationContext.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) throws BeansException {
return applicationContext.getBean(name, clazz);
}
}
3、测试类:FutureController
package com.example.gradleproject.controller;
import com.example.gradleproject.service.CommonAsncInvokeService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@Slf4j
@RestController
@RequestMapping("/future")
public class FutureController {
@Autowired
private CommonAsncInvokeService commonAsncInvokeService;
@RequestMapping("/getFutrueInfo")
public Map<String, Object> getFutrueInfo() throws ExecutionException, InterruptedException {
// log.info("aaa" + UUID.randomUUID());
Map<String, Object> result = new HashMap<>();
Map<String, Object> params = Maps.newHashMap();
params.put("param1", "张三");
Future<Map<String, Object>> future1 = (Future<Map<String, Object>>) commonAsncInvokeService.asyncInvoke(FutureController.class,
"getUserInfo1", params);
Future<Map<String, Object>> future2 = (Future<Map<String, Object>>) commonAsncInvokeService.asyncInvoke(FutureController.class,
"getUserInfo2", params);
Map<String, Object> map1 = future1.get();
Map<String, Object> map2 = future2.get();
return result;
}
public Map<String, Object> getUserInfo1(Map<String, Object> params) {
Map<String, Object> result = new HashMap<>();
result.put("aaa", "aaa");
try {
Thread.sleep( 10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
public Map<String, Object> getUserInfo2(Map<String, Object> params) {
Map<String, Object> result = new HashMap<>();
result.put("bbb", "bbb");
try {
Thread.sleep( 5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
4、启动类需加注解:@EnableAsync
package com.example.gradleproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class GradleprojectApplication {
public static void main(String[] args) {
SpringApplication.run(GradleprojectApplication.class, args);
}
}
5、postman请求验证:
getUserInfo1方法中休眠十秒,
getUserInfo2方法中休眠五秒,
两个方法会异步请求;
6、备注:通过代理实现的异步