异步任务总体介绍
函数的触发和定时在很多场景中会用到,也是一个oinone的基础能力。比如我们的流程产品中在定义流程触发时就会让用户选择模型触发还是时间触发,就是用到了函数的触发与定时能力。
触发任务TriggerTaskAction
- 触发任务的创建,使用sql-record模块监听mysql的binlog事件,通过rocketmq发送变更数据消息,收到MQ消息后,创建TriggerAutoTask。
- 触发任务的执行,使用TBSchedule拉取触发任务后,执行相应函数。
项目中引入依赖
1、项目的API工程引入依赖pamirs-core-trigger模块
<dependency>
<groupId>pro.shushi.pamirs.core</groupId>
<artifactId>pamirs-trigger-api</artifactId>
</dependency>
2、DemoModule在模块依赖定义中增加@Module(dependencies={TriggerModule.MODULE_MODULE})
@Component
@Module(
name = DemoModule.MODULE_NAME,
displayName = "oinoneDemo工程",
version = "1.0.0",
dependencies = {ModuleConstants.MODULE_BASE, CommonModule.MODULE_MODULE, UserModule.MODULE_MODULE, TriggerModule.MODULE_MODULE}
)
@Module.module(DemoModule.MODULE_MODULE)
@Module.Advanced(selfBuilt = true, application = true)
@UxHomepage(PetShopProxy.MODEL_MODEL)
public class DemoModule implements PamirsModule {
……其他代码
}
3、项目的boot工程引入依赖
<dependency>
<groupId>pro.shushi.pamirs.core</groupId>
<artifactId>pamirs-trigger-core</artifactId>
</dependency>
<dependency>
<groupId>pro.shushi.pamirs.core</groupId>
<artifactId>pamirs-trigger-bridge-tbschedule</artifactId>
</dependency>
<dependency>
<groupId>pro.shushi.pamirs.core</groupId>
<artifactId>pamirs-sql-record-core</artifactId>
</dependency>
yml文件修改(applcation-xxx.yml)
a. 修改pamris.event.enabled和pamris.event.schedule.enabled为true
b. pamirs_boot_modules增加启动模块:trigger、sql_record
pamirs:
record:
sql:
#改成自己路径
store: /opt/pamirs/logs
...
event:
enabled: true
schedule:
enabled: true
rocket-mq:
namesrv-addr: 127.0.0.1:9876
boot:
init: true
sync: true
modules:
- base
-……
- trigger
- sql_record
-……
新建触发任务
新建PetTalentTrigger类,当PetTalent模型的数据记录被新建时触发系统做一些事情
package pro.shushi.pamirs.demo.core.trigger;
import pro.shushi.pamirs.demo.api.model.PetTalent;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.trigger.annotation.Trigger;
import pro.shushi.pamirs.trigger.enmu.TriggerConditionEnum;
@Fun(PetTalent.MODEL_MODEL)
@Slf4j
public class PetTalentTrigger {
@Function
@Trigger(displayName = "PetTalent创建时触发",name = "PetTalent#Trigger#onCreate",condition = TriggerConditionEnum.ON_CREATE)
public PetTalent onCreate(PetTalent data){
log.info(data.getName() + ",被创建");
//可以增加逻辑
return data;
}
}
定时任务
定时任务是一种非常常见的模式,这里就不介绍概念了,直接进入示例环节
新建PetTalentAutoTask实现ScheduleAction
- getInterfaceName()需要跟taskAction.setExecuteNamespace定义保持一致,都是函数的命名空间
- taskAction.setExecuteFun("execute");跟执行函数名“execute”一致
- TaskType需配置为CYCLE_SCHEDULE_NO_TRANSACTION_TASK,把定时任务的schedule线程分开,要不然有一个时间长的任务会导致普通异步或触发任务全部延时。
package pro.shushi.pamirs.demo.core.task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.core.common.enmu.TimeUnitEnum;
import pro.shushi.pamirs.demo.api.model.PetTalent;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.domain.fun.FunctionDefinition;
import pro.shushi.pamirs.middleware.schedule.api.ScheduleAction;
import pro.shushi.pamirs.middleware.schedule.common.Result;
import pro.shushi.pamirs.middleware.schedule.domain.ScheduleItem;
import pro.shushi.pamirs.middleware.schedule.eunmeration.TaskType;
import pro.shushi.pamirs.trigger.enmu.TriggerTimeAnchorEnum;
import pro.shushi.pamirs.trigger.model.ScheduleTaskAction;
import pro.shushi.pamirs.trigger.service.ScheduleTaskActionService;
@Slf4j
@Component
@Fun(PetTalent.MODEL_MODEL)
public class PetTalentAutoTask implements ScheduleAction {
@Autowired
private ScheduleTaskActionService scheduleTaskActionService;
public void initTask(){
ScheduleTaskAction taskAction = new ScheduleTaskAction();
taskAction.setDisplayName("定时任务测试"); //定时任务描述
taskAction.setDescription("定时任务测试");
taskAction.setTechnicalName(PetTalent.MODEL_MODEL+"#"+PetTalentAutoTask.class.getSimpleName()+"#"+"testAutoTask"); //设置定时任务技术名
taskAction.setLimitExecuteNumber(-1); //设置执行次数
taskAction.setPeriodTimeValue(1); //设置执行周期规则
taskAction.setPeriodTimeUnit(TimeUnitEnum.MINUTE);
taskAction.setPeriodTimeAnchor(TriggerTimeAnchorEnum.START);
taskAction.setLimitRetryNumber(1); //设置失败重试规则
taskAction.setNextRetryTimeValue(1);
taskAction.setNextRetryTimeUnit(TimeUnitEnum.MINUTE);
taskAction.setExecuteNamespace(PetTalent.MODEL_MODEL);
taskAction.setExecuteFun("execute");
taskAction.setExecuteFunction(new FunctionDefinition().setTimeout(5000));
taskAction.setTaskType(TaskType.CYCLE_SCHEDULE_NO_TRANSACTION_TASK.getValue()); //设置定时任务,执行任务类型
taskAction.setContext(null); //用户传递上下文参数
taskAction.setActive(true); //定时任务是否生效
taskAction.setFirstExecuteTime(System.currentTimeMillis());
scheduleTaskActionService.submit(taskAction);//初始化任务,幂等可重复执行
}
@Override
public String getInterfaceName() {return PetTalent.MODEL_MODEL;}
@Override
@Function
public Result execute(ScheduleItem item) {
log.info("testAutoTask,上次执行时间"+item.getLastExecuteTime());
return new Result<>();
}
}
修改DemoModuleBizInit,进行定时任务初始化
模块更新的时候调用 petTalentAutoTask.initTask(),initTask本身是幂等的所以多掉几次没有关系。在【模块之生命周期】一文介绍过InstallDataInit、UpgradeDataInit、ReloadDataInit
```java
package pro.shushi.pamirs.demo.core.init;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.boot.common.api.command.AppLifecycleCommand;
import pro.shushi.pamirs.boot.common.api.init.InstallDataInit;
import pro.shushi.pamirs.boot.common.api.init.ReloadDataInit;
import pro.shushi.pamirs.boot.common.api.init.UpgradeDataInit;
import pro.shushi.pamirs.demo.api.DemoModule;
import pro.shushi.pamirs.demo.api.enumeration.DemoExpEnumerate;
import pro.shushi.pamirs.demo.core.task.PetTalentAutoTask;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import java.util.Collections;
import java.util.List;
@Component
public class DemoModuleBizInit implements InstallDataInit,
UpgradeDataInit, ReloadDataInit {
@Autowired
private PetTalentAutoTask petTalentAutoTask;
@Override
public boolean init(AppLifecycleCommand command, String version) {
//安装指令执行逻辑
initTask();
return Boolean.TRUE;
}
@Override
public boolean reload(AppLifecycleCommand command, String version) {
//重启指令执行逻辑
initTask();
return Boolean.TRUE;
}
@Override
public boolean upgrade(AppLifecycleCommand command, String version, String existVersion) {
//升级指令执行逻辑
initTask();
return Boolean.TRUE;
}
@Override
public List<String> modules() {
return Collections.singletonList(DemoModule.MODULE_MODULE);
}
@Override
public int priority() {return 0;}
private void initTask() {
petTalentAutoTask.initTask(); //初始化petTalent的定时任务
}
}
Oinone社区 作者:望闲原创文章,如若转载,请注明出处:https://doc.oinone.top/backend/11475.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验