mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-17 16:36:03 +08:00
fix(规则脚本增加日志): 规则脚本增加日志
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.fastbee.ruleEngine.config;
|
||||
|
||||
import com.yomahub.liteflow.thread.ExecutorBuilder;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class MainExecutorBuilder implements ExecutorBuilder {
|
||||
private ThreadFactory springThreadFactory = new CustomizableThreadFactory("liteflow-main-");
|
||||
@Override
|
||||
public ExecutorService buildExecutor() {
|
||||
return new ThreadPoolExecutor(
|
||||
10,
|
||||
30,
|
||||
5,
|
||||
TimeUnit.MINUTES,
|
||||
new ArrayBlockingQueue<Runnable>(1000),
|
||||
springThreadFactory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.fastbee.ruleEngine.config;
|
||||
|
||||
import com.yomahub.liteflow.thread.ExecutorBuilder;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class WhenExecutorBuilder implements ExecutorBuilder {
|
||||
private ThreadFactory springThreadFactory = new CustomizableThreadFactory("liteflow-when-");
|
||||
@Override
|
||||
public ExecutorService buildExecutor() {
|
||||
return new ThreadPoolExecutor(
|
||||
10,
|
||||
30,
|
||||
5,
|
||||
TimeUnit.MINUTES,
|
||||
new ArrayBlockingQueue<Runnable>(1000),
|
||||
springThreadFactory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.fastbee.ruleEngine.context;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.exception.NullParamException;
|
||||
import com.yomahub.liteflow.log.LFLoggerManager;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class MsgContext {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger("script");
|
||||
|
||||
/**
|
||||
* 消息主题
|
||||
*/
|
||||
private String topic;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String payload;
|
||||
/**
|
||||
* 设备编号
|
||||
*/
|
||||
private String serialNumber;
|
||||
/**
|
||||
* 产品id
|
||||
*/
|
||||
private Long productId;
|
||||
/**
|
||||
* 协议编码
|
||||
*/
|
||||
private String protocolCode;
|
||||
|
||||
private ConcurrentHashMap<String, Object> dataMap = new ConcurrentHashMap<>();
|
||||
|
||||
public void printlog(String var1, Object... var2) {
|
||||
String requestId = LFLoggerManager.getRequestId();
|
||||
logger.info(StrUtil.isBlank(requestId) ? "" : StrUtil.format("[{}]:", new Object[]{LFLoggerManager.getRequestId()}) + var1, var2);
|
||||
}
|
||||
|
||||
private <T> void putDataMap(String key, T t) {
|
||||
if (this.dataMap == null) {
|
||||
this.dataMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
if (ObjectUtil.isNull(t)) {
|
||||
throw new NullParamException("data can't accept null param");
|
||||
} else {
|
||||
this.dataMap.put(key, t);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasData(String key) {
|
||||
if (this.dataMap == null) {
|
||||
return false;
|
||||
}
|
||||
return this.dataMap.containsKey(key);
|
||||
}
|
||||
|
||||
public <T> T getData(String key) {
|
||||
if (this.dataMap == null) {
|
||||
return null;
|
||||
}
|
||||
return (T) this.dataMap.get(key);
|
||||
}
|
||||
|
||||
public <T> void setData(String key, T t) {
|
||||
this.putDataMap(key, t);
|
||||
}
|
||||
|
||||
//"{ topic:${topic}, payload:${payload} }";
|
||||
// 自定义占位符,可在脚本中使用msgContext.setData("test":1);
|
||||
// 然后通过${test}调用,可在输出侧http body和 sql语句中传入该值
|
||||
public String placeholders(String str) {
|
||||
if (getData("topic") == null) {
|
||||
setData("topic", getTopic());
|
||||
}
|
||||
if (getData("payload") == null) {
|
||||
setData("payload", getPayload());
|
||||
}
|
||||
if (getData("serialNumber") == null) {
|
||||
setData("serialNumber", getSerialNumber());
|
||||
}
|
||||
if (getData("productId") == null) {
|
||||
setData("productId", getProductId());
|
||||
}
|
||||
if (getData("protocolCode") == null) {
|
||||
setData("protocolCode", getProtocolCode());
|
||||
}
|
||||
StringSubstitutor substitutor = new StringSubstitutor(this.dataMap);
|
||||
return substitutor.replace(str);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.fastbee.ruleEngine.core;
|
||||
|
||||
import com.fastbee.ruleEngine.context.MsgContext;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FlowLogExecutor {
|
||||
private static final Logger script_logger = LoggerFactory.getLogger("script");
|
||||
private static final Logger scene_logger = LoggerFactory.getLogger("scene");
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
|
||||
public LiteflowResponse execute2Resp(String chainId, Object param, Object... contextBeanArray) {
|
||||
printContextBean(contextBeanArray);
|
||||
LiteflowResponse response = flowExecutor.execute2Resp(chainId, param, contextBeanArray);
|
||||
printResponse(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public LiteflowResponse execute2RespWithRid(String chainId, Object param, String requestId, Object... contextBeanArray) {
|
||||
printContextBeanWithRid(requestId, contextBeanArray);
|
||||
LiteflowResponse response = flowExecutor.execute2RespWithRid(chainId, param, requestId, contextBeanArray);
|
||||
printResponseWithRid(requestId, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public LiteflowResponse execute2Future(String chainId, Object param, Object... contextBeanArray) throws ExecutionException, InterruptedException {
|
||||
printContextBean(contextBeanArray);
|
||||
Future<LiteflowResponse> future = flowExecutor.execute2Future(chainId, param, contextBeanArray);
|
||||
LiteflowResponse response = future.get();
|
||||
printResponse(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public LiteflowResponse execute2FutureWithRid(String chainId, Object param, String requestId, Object... contextBeanArray) throws ExecutionException, InterruptedException {
|
||||
printContextBeanWithRid(requestId, contextBeanArray);
|
||||
Future<LiteflowResponse> future = flowExecutor.execute2FutureWithRid(chainId, param, requestId, contextBeanArray);
|
||||
LiteflowResponse response = future.get();
|
||||
printResponseWithRid(requestId, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public void printContextBean(Object... contextBeanArray) {
|
||||
log.info("=====+>规则引擎执行前,上下文变量值");
|
||||
for (Object contextBean : contextBeanArray) {
|
||||
log.info("上下文数值:{}", contextBean);
|
||||
}
|
||||
log.info("=====+>规则引擎正在执行......");
|
||||
}
|
||||
|
||||
public void printContextBeanWithRid(String requestId, Object... contextBeanArray) {
|
||||
Logger ruleLogger;
|
||||
String[] parts = requestId.split("/");
|
||||
if (parts.length >= 2) {
|
||||
if (Objects.equals(parts[0], "script")) {
|
||||
ruleLogger = script_logger;
|
||||
} else if (Objects.equals(parts[0], "scene")) {
|
||||
ruleLogger = scene_logger;
|
||||
} else {
|
||||
ruleLogger = log;
|
||||
}
|
||||
ruleLogger.info("[{}]=====+>规则引擎执行前,上下文变量值", requestId);
|
||||
for (Object contextBean : contextBeanArray) {
|
||||
ruleLogger.info("[{}]=====+>上下文数值:{}", requestId, contextBean);
|
||||
}
|
||||
ruleLogger.info("[{}]=====+>规则引擎正在执行......", requestId);
|
||||
}
|
||||
}
|
||||
|
||||
public void printResponse(LiteflowResponse response) {
|
||||
if (!response.isSuccess()) {
|
||||
Exception e = response.getCause();
|
||||
log.error("=====+>报错信息:{}", e.getMessage(), e);
|
||||
} else {
|
||||
//步骤详情
|
||||
// Map<String, List<CmpStep>> stepMap = response.getExecuteSteps();
|
||||
// stepMap.forEach((k, v) -> {
|
||||
// v.forEach((step) -> {
|
||||
// log.info("步骤:{}({}),执行时间:{}", step.getNodeId(), step.getNodeName(), step.getTimeSpent());
|
||||
// });
|
||||
// });
|
||||
//每各步骤执行时间
|
||||
String stepStr = response.getExecuteStepStrWithTime();
|
||||
log.info("=====+>步骤:{}", stepStr);
|
||||
}
|
||||
}
|
||||
|
||||
public void printResponseWithRid(String requestId, LiteflowResponse response) {
|
||||
Logger ruleLogger;
|
||||
String[] parts = requestId.split("/");
|
||||
if (parts.length >= 2) {
|
||||
if (Objects.equals(parts[0], "script")) {
|
||||
ruleLogger = script_logger;
|
||||
MsgContext msgContext = response.getContextBean(MsgContext.class);
|
||||
if (msgContext != null) {
|
||||
ruleLogger.info("[{}]=====+>执行后,msgContext:{}", requestId, msgContext);
|
||||
}
|
||||
} else if (Objects.equals(parts[0], "scene")) {
|
||||
ruleLogger = scene_logger;
|
||||
} else {
|
||||
ruleLogger = log;
|
||||
}
|
||||
if (!response.isSuccess()) {
|
||||
Exception e = response.getCause();
|
||||
ruleLogger.error("[{}]=====+>报错信息:{}", requestId, e.toString());
|
||||
} else {
|
||||
//每各步骤执行时间
|
||||
String stepStr = response.getExecuteStepStrWithTime();
|
||||
ruleLogger.info("[{}]=====+>步骤:{}", requestId, stepStr);
|
||||
ruleLogger.info("[{}]=====+>执行完成!", requestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.fastbee.ruleEngine.core;
|
||||
|
||||
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.fastbee.ruleEngine.util.Constant;
|
||||
|
||||
public class RequestIdBuilder {
|
||||
public static String buildSnowflakeRequestId() {
|
||||
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
|
||||
return Constant.REQUEST_ID_SNOWFLAKE_PREFIX + snowflake.nextId();
|
||||
}
|
||||
|
||||
public static String buildDeviceRequestId(String serialNumber) {
|
||||
return Constant.REQUEST_ID_PREFIX + serialNumber;
|
||||
}
|
||||
|
||||
public static String buildSceneRequestId(String sceneId) {
|
||||
return Constant.REQUEST_ID_PREFIX + sceneId;
|
||||
}
|
||||
|
||||
public static String buildProductRequestId(Long product, String serialNumber) {
|
||||
return Constant.REQUEST_ID_PREFIX + product + Constant.REQUEST_ID_SPLIT + serialNumber;
|
||||
}
|
||||
|
||||
public static String buildALLRequestId(String serialNumber, String product, String sceneId) {
|
||||
return Constant.REQUEST_ID_PREFIX + product + Constant.REQUEST_ID_SPLIT + serialNumber + Constant.REQUEST_ID_SPLIT + sceneId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.fastbee.ruleEngine.util;
|
||||
|
||||
public class Constant {
|
||||
public static final String REQUEST_ID_SNOWFLAKE_PREFIX = "S";
|
||||
public static final String REQUEST_ID_PREFIX = "R";
|
||||
public static final String REQUEST_ID_SPLIT = "_";
|
||||
// D=数据流,A=执行动作,T=触发器
|
||||
public static final String SCRIPT_DATA_PREFIX = "D";
|
||||
public static final String SCRIPT_ACTION_PREFIX = "A";
|
||||
public static final String SCRIPT_T_PREFIX = "T";
|
||||
}
|
||||
Reference in New Issue
Block a user