SpringBoot 2.X集成定时任务quartz,并数据持久化

本文介绍了如何在Java项目中使用Quartz框架进行定时任务调度,并详细阐述了如何与Oracle数据库的TASK_JOB和TASK_JOB_LOG表进行交互,包括domain类、Service接口及Mapper映射的实现。
摘要由CSDN通过智能技术生成

一、引入包,集成quartz(其余 的包我就不写了)

        <dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<exclusions>
				<exclusion>
					<groupId>com.mchange</groupId>
					<artifactId>c3p0</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

二、编写domain类,Service类,Mapper接口类

这里是Oracle数据库,两个表名分别为 TASK_JOB,TASK_JOB_LOG

create table TASK_JOB
(
  job_id          VARCHAR2(12) not null,
  job_name        VARCHAR2(64) default '' not null,
  job_group       VARCHAR2(64) default '' not null,
  method_name     VARCHAR2(500) default '',
  method_params   VARCHAR2(200) default '',
  cron_expression VARCHAR2(255) default '',
  misfire_policy  VARCHAR2(20),
  status          INTEGER default 0,
  create_by       VARCHAR2(64) default '' not null,
  create_time     TIMESTAMP(6) not null,
  update_by       VARCHAR2(64) default '',
  update_time     TIMESTAMP(6),
  remark          VARCHAR2(500) default ''
)
tablespace XXXX-表空间名,写自己的
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table TASK_JOB
  is '定时任务调度表';
-- Add comments to the columns 
comment on column TASK_JOB.job_id
  is '任务ID';
comment on column TASK_JOB.job_name
  is '任务名称';
comment on column TASK_JOB.job_group
  is '任务组名';
comment on column TASK_JOB.method_name
  is '任务方法';
comment on column TASK_JOB.method_params
  is '方法参数';
comment on column TASK_JOB.cron_expression
  is 'cron执行表达式';
comment on column TASK_JOB.misfire_policy
  is 'misfire_policy';
comment on column TASK_JOB.status
  is '状态(0正常 1暂停)';
comment on column TASK_JOB.create_by
  is '创建者';
comment on column TASK_JOB.create_time
  is '创建时间';
comment on column TASK_JOB.update_by
  is '更新者';
comment on column TASK_JOB.update_time
  is '更新时间';
comment on column TASK_JOB.remark
  is '备注信息';
-- Create/Recreate primary, unique and foreign key constraints 
alter table TASK_JOB
  add constraint PK_TASK_JOB primary key (JOB_ID, JOB_NAME, JOB_GROUP)
  using index 
  tablespace XXXX-表空间名,写自己的
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create table
create table TASK_JOB_LOG
(
  job_log_id     VARCHAR2(12) not null,
  job_name       VARCHAR2(64) not null,
  job_group      VARCHAR2(64) not null,
  method_name    VARCHAR2(500),
  method_params  VARCHAR2(200) default '',
  job_message    VARCHAR2(500),
  is_exception   CHAR(1) default '0',
  exception_info CLOB,
  create_time    TIMESTAMP(6),
  status         INTEGER default 0
)
tablespace XXX--表空间名
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table TASK_JOB_LOG
  is '定时任务调度日志表';
-- Add comments to the columns 
comment on column TASK_JOB_LOG.job_log_id
  is '任务日志ID';
comment on column TASK_JOB_LOG.job_name
  is '任务名称';
comment on column TASK_JOB_LOG.job_group
  is '任务组名';
comment on column TASK_JOB_LOG.method_name
  is '任务方法';
comment on column TASK_JOB_LOG.method_params
  is '方法参数';
comment on column TASK_JOB_LOG.job_message
  is '日志信息';
comment on column TASK_JOB_LOG.is_exception
  is '是否异常';
comment on column TASK_JOB_LOG.exception_info
  is '异常信息';
comment on column TASK_JOB_LOG.create_time
  is '创建时间';
comment on column TASK_JOB_LOG.status
  is '任务状态';
-- Create/Recreate primary, unique and foreign key constraints 
alter table TASK_JOB_LOG
  add constraint PK_TASK_JOB_LOG primary key (JOB_LOG_ID)
  using index 
  tablespace XXXX--表空间名
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

Domain类编写



public class TaskJob extends implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 任务ID */
    @Excel(name = "任务序号")
    private Long jobId;

    /** 任务名称 */
    @Excel(name = "任务名称")
    private String jobName;

    /** 任务组名 */
    @Excel(name = "任务组名")
    private String jobGroup;

    /** 任务方法 */
    @Excel(name = "任务方法")
    private String methodName;

    /** 方法参数 */
    @Excel(name = "方法参数")
    private String methodParams;

    /** cron执行表达式 */
    @Excel(name = "执行表达式 ")
    private String cronExpression;

    /** cron计划策略 */
    @Excel(name = "计划策略 ")
    private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;

    /** 任务状态(0正常 1暂停) */
    @Excel(name = "任务状态")
    private String status;
}
public class TaskJobLog
{
    private static final long serialVersionUID = 1L;

    /** ID */
    @Excel(name = "日志序号")
    private String jobLogId;

    /** 任务名称 */
    @Excel(name = "任务名称")
    private String jobName;

    /** 任务组名 */
    @Excel(name = "任务组名")
    private String jobGroup;

    /** 任务方法 */
    @Excel(name = "任务方法")
    private String methodName;

    /** 方法参数 */
    @Excel(name = "方法参数")
    private String methodParams;

    /** 日志信息 */
    @Excel(name = "日志信息")
    private String jobMessage;

    /** 执行状态(0正常 1失败) */
    @Excel(name = "执行状态")
    private String status;

    /** 异常信息 */
    @Excel(name = "异常信息")
    private String exceptionInfo;

Mapper类编写

 

/**
 * 调度任务信息 数据层
 * 
 * @author tolzlz
 */
public interface TaskJobMapper
{
    /**
     * 查询调度任务日志集合
     * 
     * @param job 调度信息
     * @return 操作日志集合
     */
    public List<TaskJob> selectJobList(TaskJob job);

    /**
     * 查询所有调度任务
     * 
     * @return 调度任务列表
     */
    public List<TaskJob> selectJobAll();

    /**
     * 通过调度ID查询调度任务信息
     * 
     * @param jobId 调度ID
     * @return 角色对象信息
     */
    public TaskJob selectJobById(Long jobId);

    /**
     * 通过调度ID删除调度任务信息
     * 
     * @param jobId 调度ID
     * @return 结果
     */
    public int deleteJobById(TaskJob job);

    /**
     * 批量删除调度任务信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteJobLogByIds(Long[] ids);

    /**
     * 修改调度任务信息
     * 
     * @param job 调度任务信息
     * @return 结果
     */
    public int updateJob(TaskJob job);

    /**
     * 新增调度任务信息
     * 
     * @param job 调度任务信息
     * @return 结果
     */
    public int insertJob(TaskJob job);



}
/**
 * 调度任务日志信息 数据层
 * 
 * @author tolzlz
 */
public interface TaskJobLogMapper
{
    /**
     * 获取quartz调度器日志的计划任务
     * 
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    public List<TaskJobLog> selectJobLogList(TaskJobLog jobLog);

    /**
     * 通过调度任务日志ID查询调度信息
     * 
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    public TaskJobLog selectJobLogById(Long jobLogId);

    /**
     * 新增任务日志
     * 
     * @param jobLog 调度日志信息
     * @return 结果
     */
    public int insertJobLog(TaskJobLog jobLog);

    /**
     * 批量删除调度日志信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteJobLogByIds(String[] ids);

    /**
     * 删除任务日志
     * 
     * @param jobId 调度日志ID
     * @return 结果
     */
    public int deleteJobLogById(Long jobId);

    /**
     * 清空任务日志
     */
    public void cleanJobLog();

    public String createcLogId();
}

 

(三)、编写Mapper xml 映射文件,以及Service和Service实现类

(看项目,这里不再编写)

(四)、编写bean工具类,以便操作bean

@Component
public class SpringContextUtils implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) {
        return applicationContext.isSingleton(name);
    }

    public static Class<? extends Object> getType(String name) {
        return applicationContext.getType(name);
    }

    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }
}

(五)、数据源配置(重点!!!!!!!!!!!)

如果子model配置,只需配置一次

在application.properties中配置数据源

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值