业务可视化-让你的流程图“Run“起来(6.定时任务&Spring-Batch的集成)

本文介绍了如何在ladybugflow中配置cron表达式实现定时任务,以及如何与Spring-Batch集成进行批处理的可视化操作。ladybugflow通过在流程启动节点设置定时器和cron表达式,利用Spring的scheduling解析执行定时任务。同时展示了流程的配置方法和工程集成示例。
摘要由CSDN通过智能技术生成

在项目中,经常会有一些批处理的定时任务需要配置,目前的java项目一般采用Spring-Batch来作为批处理的平台。

但是Spring-Batch在对Job流程(并行,分支,条件分支,合并等)的支持略显复杂臃肿。

尤其是流程可视化这方面几乎不支持。

所以,最近对ladybugflow做了改进,在天然支持可视化流程的基础上,追加了对定时任务的支持。

本篇文章介绍ladybugflow定时任务的配置,和与SpringBatch相结合完成批处理的可视化操作的例子。

定时任务

对于流程的执行,前面文章介绍了流程的启动方法,如下:

Flow1 testFlow = new Flow1();
testFlow.startFlow(true);

这样只能完成流程的一次启动,如果需要定时启动流程,ladybugflow提供了在启动节点中设置cron表达式的方法来实现定时启动流程。

cron表达式

我们首先简单的了解一下cron表达式

Cron表达式是有六个空格分隔的时间和日期字段(域)组成的字符串。

┌───────────── second (0-59)

│ ┌───────────── minute (0 - 59)

│ │ ┌───────────── hour (0 - 23)

│ │ │ ┌───────────── day of the month (1 - 31)

│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)

│ │ │ │ │ ┌───────────── day of the week (0 - 7)

│ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)

│ │ │ │ │ │

*  *  *  *  *  *

比如:

“0 0 * * * *” = 每天 00:00。

"*/10 * * * * *" = 每 10 秒。

“0 0 8-10 * * *” = 每天 8、9 和 10 个小时。

"0 0 6,19 * * *" = 每天早上 6:00 和晚上 7:00。

“0 0/30 8-10 * * *” = 每天 8:00、8:30、9:00、9:30、10:00 和 10:30。

"0 0 9-17 * * MON-FRI" = 工作日 9 到 5

“0 0 0 25 12 ?” = 圣诞节午夜

"0 0 0 L * *" = 每个月的午夜

"0 0 0 L-3 * *" = 每月第三天到最后一天午夜

"0 0 0 1W * *" = 每月第一个工作日午夜

"0 0 0 LW * *" = 每月最后一个工作日的午夜

"0 0 0 * * 5L" = 每月最后一个星期五的午夜

"0 0 0 * * THUL" = 每月最后一个星期四的午夜

"0 0 0 ? * 5#2" = 每月第二个星期五的午夜

"0 0 0 ? * MON#1" = 每月第一个星期一的午夜

在ladybugflow中配置cron表达式

在流程编辑器中,将流程的首节点的启动类型设置成Timer,然后输入cron表达式,如下图所示

点击更新,更新json,

将json字符串拷贝到工程里,

启动工程,如下:

运行结果

[I]2022/08/09 18:23:35.547  main:ladybugflow.properties in root path not found, use default configuration
[I]2022/08/09 18:23:35.558  main:NodePool started.
[I]2022/08/09 18:23:35.559  main:Ready queue consumer thread started.
[I]2022/08/09 18:23:35.610  main:Complete queue consumer thread started.
[I]2022/08/09 18:23:36.230 [your flow id][283eb8de-e12c-4de0-9484-51da87538032] main:json:
{"flowId":"your flow id","nodes":[{"id":"1","label":"start","readyCheck":0,"startType":3,"startCron":"0 0/1 * * * ?"},{"id":"a1a38c2e-0e05-4c68-bd49-f12aea070876","label":"查询用户信息","readyCheck":0,"startType":0,"startCron":null},{"id":"1a90a997-4390-470a-ae7c-626a725438d2","label":"查询酒店信息","readyCheck":0,"startType":0,"startCron":null},{"id":"52289e99-363d-4453-8077-ca8bdc6d35bf","label":"下单","readyCheck":0,"startType":0,"startCron":null},{"id":"16422cbb-ccb0-4fe2-952b-e3ad5c3acbb2","label":"下单成功","readyCheck":0,"startType":0,"startCron":null}],"edges":[{"id":"1","from":"1","to":"2","condition":null,"arrows":"to"},{"id":"b3ad7ab3-8fb6-4527-8cae-6845e03da3e4","from":"1","to":"a1a38c2e-0e05-4c68-bd49-f12aea070876","condition":null,"arrows":"to"},{"id":"001375c7-19e7-436b-bbcd-68e36c8f23b7","from":"1","to":"1a90a997-4390-470a-ae7c-626a725438d2","condition":null,"arrows":"to"},{"id":"dd830043-c7a7-4c71-b91c-10c007b7b19c","from":"1a90a997-4390-470a-ae7c-626a725438d2","to":"52289e99-363d-4453-8077-ca8bdc6d35bf","condition":null,"arrows":"to"},{"id":"21c2c69d-0050-4eca-8283-5a2bcbdc6c37","from":"52289e99-363d-4453-8077-ca8bdc6d35bf","to":"16422cbb-ccb0-4fe2-952b-e3ad5c3acbb2","condition":null,"arrows":"to"},{"id":"19f2f329-8163-4dc6-a353-800df79d18a6","from":"a1a38c2e-0e05-4c68-bd49-f12aea070876","to":"52289e99-363d-4453-8077-ca8bdc6d35bf","condition":null,"arrows":"to"}]}
[your flow id] next execute time:2022/08/09 18:24:00
[I]2022/08/09 18:24:00.027 [your flow id][cef59351-e2b5-4e81-b85a-5c751e35d8c4] ThreadPoolTaskScheduler-1:json:
{"flowId":"your flow id","nodes":[{"id":"1","label":"start","readyCheck":0,"startType":3,"startCron":"0 0/1 * * * ?"},{"id":"a1a3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Batch项目中集成Mybatis-Plus,需要进行以下步骤: 1. 添加Mybatis-Plus依赖:在pom.xml文件中添加Mybatis-Plus的依赖。 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> ``` 2. 配置数据源:在application.properties文件中配置数据源。 ```properties spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root ``` 3. 配置Mybatis-Plus:在application.properties文件中配置Mybatis-Plus。 ```properties mybatis-plus.mapper-locations=classpath:/mapper/*.xml mybatis-plus.global-config.id-type=auto mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0 ``` 4. 创建Mapper接口:创建Mapper接口继承BaseMapper接口。 ```java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建实体类:创建实体类并添加注解。 ```java @Data @TableName("user") public class User implements Serializable { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; } ``` 6. 在Spring Batch任务中使用Mapper:在Spring Batch任务中使用Mapper。 ```java @Autowired private UserMapper userMapper; @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { List<User> userList = userMapper.selectList(null); // do something with user list return RepeatStatus.FINISHED; } ``` 这样,就可以在Spring Batch项目中集成Mybatis-Plus了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值