serv-定时任务
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.xyzh.crontab.mapper.CrontabLogMapper">
|
||||
|
||||
<!-- 结果映射 -->
|
||||
<resultMap id="BaseResultMap" type="org.xyzh.common.dto.crontab.TbCrontabLog">
|
||||
<id column="id" property="ID" />
|
||||
<result column="task_id" property="taskId" />
|
||||
<result column="task_name" property="taskName" />
|
||||
<result column="task_group" property="taskGroup" />
|
||||
<result column="bean_name" property="beanName" />
|
||||
<result column="method_name" property="methodName" />
|
||||
<result column="method_params" property="methodParams" />
|
||||
<result column="execute_status" property="executeStatus" />
|
||||
<result column="execute_message" property="executeMessage" />
|
||||
<result column="exception_info" property="exceptionInfo" />
|
||||
<result column="start_time" property="startTime" />
|
||||
<result column="end_time" property="endTime" />
|
||||
<result column="execute_duration" property="executeDuration" />
|
||||
<result column="create_time" property="createTime" />
|
||||
<result column="update_time" property="updateTime" />
|
||||
<result column="delete_time" property="deleteTime" />
|
||||
<result column="deleted" property="deleted" />
|
||||
</resultMap>
|
||||
|
||||
<!-- 字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id, task_id, task_name, task_group, bean_name, method_name, method_params,
|
||||
execute_status, execute_message, exception_info, start_time, end_time,
|
||||
execute_duration, create_time, update_time, delete_time, deleted
|
||||
</sql>
|
||||
|
||||
<!-- 查询条件 -->
|
||||
<sql id="Base_Where_Clause">
|
||||
<where>
|
||||
<if test="filter != null">
|
||||
<if test="filter.ID != null and filter.ID != ''">
|
||||
AND id = #{filter.ID}
|
||||
</if>
|
||||
<if test="filter.taskId != null and filter.taskId != ''">
|
||||
AND task_id = #{filter.taskId}
|
||||
</if>
|
||||
<if test="filter.taskName != null and filter.taskName != ''">
|
||||
AND task_name LIKE CONCAT('%', #{filter.taskName}, '%')
|
||||
</if>
|
||||
<if test="filter.taskGroup != null and filter.taskGroup != ''">
|
||||
AND task_group = #{filter.taskGroup}
|
||||
</if>
|
||||
<if test="filter.beanName != null and filter.beanName != ''">
|
||||
AND bean_name = #{filter.beanName}
|
||||
</if>
|
||||
<if test="filter.executeStatus != null">
|
||||
AND execute_status = #{filter.executeStatus}
|
||||
</if>
|
||||
<if test="filter.deleted != null">
|
||||
AND deleted = #{filter.deleted}
|
||||
</if>
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
|
||||
<!-- 插入日志 -->
|
||||
<insert id="insertLog">
|
||||
INSERT INTO tb_crontab_log
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="log.ID != null">id,</if>
|
||||
<if test="log.taskId != null">task_id,</if>
|
||||
<if test="log.taskName != null">task_name,</if>
|
||||
<if test="log.taskGroup != null">task_group,</if>
|
||||
<if test="log.beanName != null">bean_name,</if>
|
||||
<if test="log.methodName != null">method_name,</if>
|
||||
<if test="log.methodParams != null">method_params,</if>
|
||||
<if test="log.executeStatus != null">execute_status,</if>
|
||||
<if test="log.executeMessage != null">execute_message,</if>
|
||||
<if test="log.exceptionInfo != null">exception_info,</if>
|
||||
<if test="log.startTime != null">start_time,</if>
|
||||
<if test="log.endTime != null">end_time,</if>
|
||||
<if test="log.executeDuration != null">execute_duration,</if>
|
||||
<if test="log.createTime != null">create_time,</if>
|
||||
deleted
|
||||
</trim>
|
||||
VALUES
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="log.ID != null">#{log.ID},</if>
|
||||
<if test="log.taskId != null">#{log.taskId},</if>
|
||||
<if test="log.taskName != null">#{log.taskName},</if>
|
||||
<if test="log.taskGroup != null">#{log.taskGroup},</if>
|
||||
<if test="log.beanName != null">#{log.beanName},</if>
|
||||
<if test="log.methodName != null">#{log.methodName},</if>
|
||||
<if test="log.methodParams != null">#{log.methodParams},</if>
|
||||
<if test="log.executeStatus != null">#{log.executeStatus},</if>
|
||||
<if test="log.executeMessage != null">#{log.executeMessage},</if>
|
||||
<if test="log.exceptionInfo != null">#{log.exceptionInfo},</if>
|
||||
<if test="log.startTime != null">#{log.startTime},</if>
|
||||
<if test="log.endTime != null">#{log.endTime},</if>
|
||||
<if test="log.executeDuration != null">#{log.executeDuration},</if>
|
||||
<if test="log.createTime != null">#{log.createTime},</if>
|
||||
0
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 根据ID查询日志 -->
|
||||
<select id="selectLogById" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_log
|
||||
WHERE id = #{logId} AND deleted = 0
|
||||
</select>
|
||||
|
||||
<!-- 根据任务ID查询日志列表 -->
|
||||
<select id="selectLogsByTaskId" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_log
|
||||
WHERE task_id = #{taskId} AND deleted = 0
|
||||
ORDER BY start_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 根据过滤条件查询日志列表 -->
|
||||
<select id="selectLogList" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_log
|
||||
<include refid="Base_Where_Clause" />
|
||||
ORDER BY start_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 分页查询日志列表 -->
|
||||
<select id="selectLogPage" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_log
|
||||
<include refid="Base_Where_Clause" />
|
||||
ORDER BY start_time DESC
|
||||
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
|
||||
</select>
|
||||
|
||||
<!-- 统计日志总数 -->
|
||||
<select id="countLogs" resultType="long">
|
||||
SELECT COUNT(*)
|
||||
FROM tb_crontab_log
|
||||
<include refid="Base_Where_Clause" />
|
||||
</select>
|
||||
|
||||
<!-- 删除日志(逻辑删除) -->
|
||||
<update id="deleteLog">
|
||||
UPDATE tb_crontab_log
|
||||
SET deleted = 1,
|
||||
delete_time = NOW()
|
||||
WHERE id = #{logId} AND deleted = 0
|
||||
</update>
|
||||
|
||||
<!-- 批量删除日志 -->
|
||||
<update id="batchDeleteLogs">
|
||||
UPDATE tb_crontab_log
|
||||
SET deleted = 1,
|
||||
delete_time = NOW()
|
||||
WHERE deleted = 0
|
||||
AND id IN
|
||||
<foreach collection="ids" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</update>
|
||||
|
||||
<!-- 清理指定时间之前的日志 -->
|
||||
<update id="cleanLogsByDate">
|
||||
UPDATE tb_crontab_log
|
||||
SET deleted = 1,
|
||||
delete_time = NOW()
|
||||
WHERE deleted = 0
|
||||
AND create_time < #{beforeDate}
|
||||
</update>
|
||||
|
||||
<!-- 根据任务ID和执行状态查询日志 -->
|
||||
<select id="selectLogsByTaskIdAndStatus" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_log
|
||||
WHERE task_id = #{taskId}
|
||||
AND execute_status = #{executeStatus}
|
||||
AND deleted = 0
|
||||
ORDER BY start_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 查询最近的执行日志 -->
|
||||
<select id="selectRecentLogs" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_log
|
||||
WHERE task_id = #{taskId} AND deleted = 0
|
||||
ORDER BY start_time DESC
|
||||
LIMIT #{limit}
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,188 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.xyzh.crontab.mapper.CrontabTaskMapper">
|
||||
|
||||
<!-- 结果映射 -->
|
||||
<resultMap id="BaseResultMap" type="org.xyzh.common.dto.crontab.TbCrontabTask">
|
||||
<id column="id" property="ID" />
|
||||
<result column="task_id" property="taskId" />
|
||||
<result column="task_name" property="taskName" />
|
||||
<result column="task_group" property="taskGroup" />
|
||||
<result column="bean_name" property="beanName" />
|
||||
<result column="method_name" property="methodName" />
|
||||
<result column="method_params" property="methodParams" />
|
||||
<result column="cron_expression" property="cronExpression" />
|
||||
<result column="status" property="status" />
|
||||
<result column="description" property="description" />
|
||||
<result column="concurrent" property="concurrent" />
|
||||
<result column="misfire_policy" property="misfirePolicy" />
|
||||
<result column="creator" property="creator" />
|
||||
<result column="updater" property="updater" />
|
||||
<result column="create_time" property="createTime" />
|
||||
<result column="update_time" property="updateTime" />
|
||||
<result column="delete_time" property="deleteTime" />
|
||||
<result column="deleted" property="deleted" />
|
||||
</resultMap>
|
||||
|
||||
<!-- 字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id, task_id, task_name, task_group, bean_name, method_name, method_params,
|
||||
cron_expression, status, description, concurrent, misfire_policy,
|
||||
creator, updater, create_time, update_time, delete_time, deleted
|
||||
</sql>
|
||||
|
||||
<!-- 查询条件 -->
|
||||
<sql id="Base_Where_Clause">
|
||||
<where>
|
||||
<if test="filter != null">
|
||||
<if test="filter.ID != null and filter.ID != ''">
|
||||
AND id = #{filter.ID}
|
||||
</if>
|
||||
<if test="filter.taskId != null and filter.taskId != ''">
|
||||
AND task_id = #{filter.taskId}
|
||||
</if>
|
||||
<if test="filter.taskName != null and filter.taskName != ''">
|
||||
AND task_name LIKE CONCAT('%', #{filter.taskName}, '%')
|
||||
</if>
|
||||
<if test="filter.taskGroup != null and filter.taskGroup != ''">
|
||||
AND task_group = #{filter.taskGroup}
|
||||
</if>
|
||||
<if test="filter.beanName != null and filter.beanName != ''">
|
||||
AND bean_name = #{filter.beanName}
|
||||
</if>
|
||||
<if test="filter.methodName != null and filter.methodName != ''">
|
||||
AND method_name = #{filter.methodName}
|
||||
</if>
|
||||
<if test="filter.status != null">
|
||||
AND status = #{filter.status}
|
||||
</if>
|
||||
<if test="filter.deleted != null">
|
||||
AND deleted = #{filter.deleted}
|
||||
</if>
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
|
||||
<!-- 插入任务 -->
|
||||
<insert id="insertTask">
|
||||
INSERT INTO tb_crontab_task
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="task.ID != null">id,</if>
|
||||
<if test="task.taskId != null">task_id,</if>
|
||||
<if test="task.taskName != null">task_name,</if>
|
||||
<if test="task.taskGroup != null">task_group,</if>
|
||||
<if test="task.beanName != null">bean_name,</if>
|
||||
<if test="task.methodName != null">method_name,</if>
|
||||
<if test="task.methodParams != null">method_params,</if>
|
||||
<if test="task.cronExpression != null">cron_expression,</if>
|
||||
<if test="task.status != null">status,</if>
|
||||
<if test="task.description != null">description,</if>
|
||||
<if test="task.concurrent != null">concurrent,</if>
|
||||
<if test="task.misfirePolicy != null">misfire_policy,</if>
|
||||
<if test="task.creator != null">creator,</if>
|
||||
<if test="task.createTime != null">create_time,</if>
|
||||
deleted
|
||||
</trim>
|
||||
VALUES
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="task.ID != null">#{task.ID},</if>
|
||||
<if test="task.taskId != null">#{task.taskId},</if>
|
||||
<if test="task.taskName != null">#{task.taskName},</if>
|
||||
<if test="task.taskGroup != null">#{task.taskGroup},</if>
|
||||
<if test="task.beanName != null">#{task.beanName},</if>
|
||||
<if test="task.methodName != null">#{task.methodName},</if>
|
||||
<if test="task.methodParams != null">#{task.methodParams},</if>
|
||||
<if test="task.cronExpression != null">#{task.cronExpression},</if>
|
||||
<if test="task.status != null">#{task.status},</if>
|
||||
<if test="task.description != null">#{task.description},</if>
|
||||
<if test="task.concurrent != null">#{task.concurrent},</if>
|
||||
<if test="task.misfirePolicy != null">#{task.misfirePolicy},</if>
|
||||
<if test="task.creator != null">#{task.creator},</if>
|
||||
<if test="task.createTime != null">#{task.createTime},</if>
|
||||
0
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 更新任务 -->
|
||||
<update id="updateTask">
|
||||
UPDATE tb_crontab_task
|
||||
<set>
|
||||
<if test="task.taskName != null">task_name = #{task.taskName},</if>
|
||||
<if test="task.taskGroup != null">task_group = #{task.taskGroup},</if>
|
||||
<if test="task.beanName != null">bean_name = #{task.beanName},</if>
|
||||
<if test="task.methodName != null">method_name = #{task.methodName},</if>
|
||||
<if test="task.methodParams != null">method_params = #{task.methodParams},</if>
|
||||
<if test="task.cronExpression != null">cron_expression = #{task.cronExpression},</if>
|
||||
<if test="task.status != null">status = #{task.status},</if>
|
||||
<if test="task.description != null">description = #{task.description},</if>
|
||||
<if test="task.concurrent != null">concurrent = #{task.concurrent},</if>
|
||||
<if test="task.misfirePolicy != null">misfire_policy = #{task.misfirePolicy},</if>
|
||||
<if test="task.updater != null">updater = #{task.updater},</if>
|
||||
update_time = NOW()
|
||||
</set>
|
||||
WHERE id = #{task.ID} AND deleted = 0
|
||||
</update>
|
||||
|
||||
<!-- 删除任务(逻辑删除) -->
|
||||
<update id="deleteTask">
|
||||
UPDATE tb_crontab_task
|
||||
SET deleted = 1,
|
||||
delete_time = NOW()
|
||||
WHERE id = #{taskId} AND deleted = 0
|
||||
</update>
|
||||
|
||||
<!-- 根据ID查询任务 -->
|
||||
<select id="selectTaskById" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_task
|
||||
WHERE id = #{taskId} AND deleted = 0
|
||||
</select>
|
||||
|
||||
<!-- 根据过滤条件查询任务列表 -->
|
||||
<select id="selectTaskList" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_task
|
||||
<include refid="Base_Where_Clause" />
|
||||
ORDER BY create_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 分页查询任务列表 -->
|
||||
<select id="selectTaskPage" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_task
|
||||
<include refid="Base_Where_Clause" />
|
||||
ORDER BY create_time DESC
|
||||
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
|
||||
</select>
|
||||
|
||||
<!-- 查询所有运行中的任务 -->
|
||||
<select id="selectRunningTasks" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_task
|
||||
WHERE status = 1 AND deleted = 0
|
||||
ORDER BY create_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 更新任务状态 -->
|
||||
<update id="updateTaskStatus">
|
||||
UPDATE tb_crontab_task
|
||||
SET status = #{status},
|
||||
update_time = NOW()
|
||||
WHERE id = #{taskId} AND deleted = 0
|
||||
</update>
|
||||
|
||||
<!-- 根据Bean名称和方法名称查询任务 -->
|
||||
<select id="selectTaskByBeanAndMethod" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM tb_crontab_task
|
||||
WHERE bean_name = #{beanName}
|
||||
AND method_name = #{methodName}
|
||||
AND deleted = 0
|
||||
LIMIT 1
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user