刷新redis方法的bean:
public class MethodAndParameter {
private String methodName;//方法名
private String parameter1;、、参数一
private String parameter2;、、参数二
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public String getParameter1() {
return parameter1;
}
public void setParameter1(String parameter1) {
this.parameter1 = parameter1;
}
public String getParameter2() {
return parameter2;
}
public void setParameter2(String parameter2) {
this.parameter2 = parameter2;
}
}
===================================================================
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.Queue;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import baseData.bdUtils.SpringUtil;
import baseData.groupInfo.MethodAndParameter;
@Component
public class AsyncRedisRefresh implements ApplicationRunner {
private static Logger logger = Logger.getLogger(AsyncRedisRefresh.class);
static Queue<MethodAndParameter> queue = new LinkedList<MethodAndParameter>();
public void addMethodAndParameter(String methodName,String... parameters) {
MethodAndParameter demo = new MethodAndParameter();
demo.setMethodName(methodName);
demo.setParameter1(parameters[0]);
if(parameters.length > 1) {
demo.setParameter2(parameters[1]);
}
queue.offer(demo);
}
@Override
public void run(ApplicationArguments args) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
if(queue.size() > 0) {
MethodAndParameter demo = queue.poll();
GroupInfoServiceImpl info = (GroupInfoServiceImpl) SpringUtil.getBean("groupInfoServiceImpl");
Class<?> clazz = null;
try {
clazz = Class.forName("baseData.groupInfo.service.impl.GroupInfoServiceImpl");
} catch (ClassNotFoundException e1) {
logger.error("反射类找不到");
}
try {
if(StringUtils.isBlank(demo.getParameter2())) {
Method method = clazz.getMethod(demo.getMethodName(),String.class);
method.invoke(info,demo.getParameter1());
} else {
Method method = clazz.getMethod(demo.getMethodName(),String.class,String.class);
method.invoke(info,demo.getParameter1(),demo.getParameter2());
}
} catch (Exception e) {
logger.error("反射异常");
}
}
}
}
}).start();
}
}