如果我们需要在某个点执行一个任务,可以用使用以下方法,首先在操作之间先明白思路
参考技术来源:https://jfinal.com/doc/9-2
第一步,先安装包,因为 这是第三方包:
<dependency>
<groupId>it.sauronsoftware.cron4j</groupId>
<artifactId>cron4j</artifactId>
<version>2.2.5</version>
</dependency>
第二 配置异业执行.如果是tomcat需要置
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>configClass</param-name>
<param-value>com.mock.frbmems.config.EmsConfig</param-value>
</init-param>
</filter>
第三步,增加插件配置
@Overridepublic void configPlugin(Plugins plugins) {ProUtil.use("jdbc.properties");Prop p = ProUtil.use("jdbc.properties");// 配置第一个数据源 //这儿是第一个 00是分 第二个00 是小时Cron4jPlugin cp = new Cron4jPlugin();cp.addTask("00 00 * * *", new Runnable() {@Overridepublic void run() {AutoWorkUtils.handleAutoWork();}});plugins.add(cp);}
第4步,业务参考如下,如果是检测代码就用了System,out 如果是上线的代码不要有这程这个打印,要用日志工具输出
package com.mock.frbmems.util;import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record;import java.sql.Timestamp;/*** @author rskg* @version 1.0* @project pc_frbmems* @description* @date 2025/1/1 11:27:46*/ public class AutoWorkUtils { /**/*** 更新交班记录的结束时间、状态和备注信息。*/ public static void updateShiftRecordEnd() {String sqlSelectId = "SELECT id FROM zyb_take_over_shift_record WHERE company_id = 130 AND status = 1 ORDER BY id DESC LIMIT 1";String lastShiftId = Db.queryStr(sqlSelectId);if (lastShiftId != null) {String sqlUpdate = "UPDATE zyb_take_over_shift_record SET end_time = NOW(), status = 2, remark = '系统自动交班' WHERE id = ?";int updatedRows = Db.update(sqlUpdate, lastShiftId);if (updatedRows > 0) {System.out.println("更新交班记录成功,ID: " + lastShiftId);} else {System.out.println("更新交班记录失败,未找到匹配的记录,ID: " + lastShiftId);}} else {System.out.println("未找到需要更新的交班记录");} }/*** 插入新的交班记录。** @param lastRecord 上一条记录的Record对象,包含班次和日记录ID*/public static void insertNewShiftRecord(Record lastRecord) {if (lastRecord != null) {String workSeq = lastRecord.getStr("work_seq");Long dayRecordId = lastRecord.getLong("day_record_id");String sqlInsert = "INSERT INTO zyb_take_over_shift_record (company_id, begin_time, status, work_seq, day_record_id) VALUES (?, ?, ?, ?, ?)";int insertedRows = Db.update(sqlInsert, 130, new Timestamp(System.currentTimeMillis()), 1, workSeq, dayRecordId);if (insertedRows > 0) {System.out.println("插入新的交班记录成功");} else {System.out.println("插入新的交班记录失败");}} else {System.out.println("未找到需要插入的交班记录信息");}}/*** 检查自动交班配置是否启用。** @return 如果自动交班配置为1,则返回true,否则返回false。*/public static boolean checkAutoWorkConfig() {String autoWorkValue = Db.queryStr("SELECT pvalue FROM fr_system_param WHERE pkey = 'autoWork' AND company_id = 130");return "1".equals(autoWorkValue);}/*** 获取最后一条交班记录的详细信息。** @return 包含最后一条交班记录详细信息的Record对象,如果没有找到记录,则返回null。*/public static Record getLastShiftRecord() {String sqlSelect = "SELECT work_seq, day_record_id FROM zyb_take_over_shift_record WHERE company_id = 130 AND status = 1 ORDER BY id DESC LIMIT 1";return Db.findFirst(sqlSelect);}/*** 处理自动交班逻辑。*/public static void handleAutoWork() {System.out.println("开始处理自动交班逻辑");if (checkAutoWorkConfig()) {Record lastShiftRecord = getLastShiftRecord();if (lastShiftRecord != null) {updateShiftRecordEnd();insertNewShiftRecord(lastShiftRecord);} else {System.out.println("未找到有效的交班记录");}} else {System.out.println("自动交班未配置或已禁用");}System.out.println("自动交班逻辑处理结束");} }