----添加流程测试demo
This commit is contained in:
@ -0,0 +1,119 @@
|
||||
package com.xjrsoft.common;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.pictc.listener.ListenerParameterContext;
|
||||
import com.pictc.listener.MethodInfo;
|
||||
import com.pictc.listener.PEventListener;
|
||||
import com.pictc.utils.CollectionUtils;
|
||||
import com.pictc.utils.ObjectUtils;
|
||||
import com.pictc.utils.StringUtils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
@Slf4j
|
||||
@ApiIgnore
|
||||
@RequestMapping("/reception")
|
||||
public class ServiceListenerProvider implements ApplicationContextAware,ApplicationRunner{
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
private List<MethodInfo> cmethods = CollectionUtils.newArrayList();
|
||||
|
||||
// SpEL解析器
|
||||
private final ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
private static byte[] FALSE;
|
||||
|
||||
private static byte[] TRUE;
|
||||
|
||||
static {
|
||||
try {
|
||||
FALSE = "false".getBytes("UTF-8");
|
||||
TRUE = "true".getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
@PostMapping("/listener")
|
||||
public void reception(HttpServletRequest request,HttpServletResponse response) throws Exception {
|
||||
boolean flag = false;
|
||||
if(!cmethods.isEmpty()) {
|
||||
ListenerParameterContext context = ObjectUtils.reader(request.getInputStream());
|
||||
log.info("监听服务:{},调用{}->{},是否单例:{}",context.getSource(),JSON.toJSONString(context.getData()),context.isSingle());
|
||||
StandardEvaluationContext elEContext = new StandardEvaluationContext();
|
||||
elEContext.setVariable("source", context.getSource());
|
||||
elEContext.setVariable("data", context.getData());
|
||||
for (MethodInfo info : cmethods) {
|
||||
PEventListener annotation = info.getAnnotation();
|
||||
if(StringUtils.isNotEmpty(annotation.value())) {
|
||||
Object result = parser.parseExpression(annotation.value()).getValue(elEContext);
|
||||
System.out.println("EL表达式执行结果:" + result);
|
||||
if(result!=null && (boolean)result == false) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
flag = parseResult(info.invoke(new Object[] {context.getSource(),context.getData()}));
|
||||
if(context.isSingle() && flag) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ServletOutputStream stream = response.getOutputStream();
|
||||
stream.write(flag?TRUE:FALSE);
|
||||
stream.flush();
|
||||
}else {
|
||||
log.info("没有注册监听器");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean parseResult(Object val) {
|
||||
if(val==null) return false;
|
||||
if(Boolean.class.equals(val.getClass())) {
|
||||
return Boolean.class.cast(val).booleanValue();
|
||||
}else if(boolean.class.equals(val.getClass())) {
|
||||
return (boolean)val;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 项目启动后执行扫描
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
String[] beanNames = context.getBeanDefinitionNames();
|
||||
for (String beanName : beanNames) {
|
||||
Object bean = context.getBean(beanName);
|
||||
Method[] methods = bean.getClass().getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.isAnnotationPresent(PEventListener.class)) {
|
||||
cmethods.add(new MethodInfo(bean, method, method.getAnnotation(PEventListener.class)));
|
||||
}
|
||||
}
|
||||
}
|
||||
log.info("监听器客户端:{}",cmethods);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user