java simpletrigger_Spring整合Quartz (cronTrigger和simpleTrigger实现方法)

Spring整合Quartz (cronTrigger和simpleTrigger实现方法)

之前有记录过一次springboot整合Quartz的文章,由于偶尔一次自己使用spring需要整合Quartz时有遇到一些异常,导致定时job无法正常执行,所以在此也记录一下spring整合quartz的两种实现方式。SpringBoot整合Quartz

应用框架版本

Spring 5.1.4.RELEASE

Quartz 2.3.0

Maven 3.5.0

Demo目录结构

dd71371eb1839f50474656fa95cda2ee.png

web.xml配置

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

contextConfigLocation

classpath:spring.xml

org.springframework.web.context.ContextLoaderListener

index.jsp

Spring.xml配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

run

0/1 * * * * ?

pom.xml依赖

在使用quartz版本时需要注意与spring的版本对应。

spring 3.0版本内置的是Quartz<2.0的版本,所以在使用Spring 3.0版本以上时,需要使用Quartz 2.0以上的版本

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

spring-quartz

com.test.quartz

1.0-SNAPSHOT

war

com.test.quartz Maven Webapp

http://www.example.com

UTF-8

1.7

1.7

5.1.4.RELEASE

2.3.0

junit

junit

4.11

test

org.springframework

spring-context

${spring.version}

commons-logging

commons-logging

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-orm

${spring.version}

jar

compile

org.springframework

spring-test

${spring.version}

jar

test

org.quartz-scheduler

quartz

${quartz.version}

com.test.quartz

maven-clean-plugin

3.1.0

TimerTask.java

package com.test.quartz.timer;

import com.test.quartz.service.TaskService;

/**

* @author yd

* @date 2019-06-11 16:08

* @describe 定时job1

*/

public class TimerTask {

private TaskService taskService;

public TaskService getTaskService() {

return taskService;

}

public void setTaskService(TaskService taskService) {

this.taskService = taskService;

}

public void run(){

String process = taskService.process(this.getClass().getSimpleName());

System.out.println(process);

}

}

TimeTask2.java

package com.test.quartz.timer;

import com.test.quartz.service.TaskService;

/**

* @author yd

* @date 2019-06-11 16:28

* @describe 定时job2

*/

public class TimeTask2 {

private TaskService taskService;

public TaskService getTaskService() {

return taskService;

}

public void setTaskService(TaskService taskService) {

this.taskService = taskService;

}

public void run(){

String process = taskService.process(this.getClass().getSimpleName());

System.out.println(process);

}

}

service

在对quartz集成的同时,在这里也引入了service层的注入,demo中只是一个简单的样例,可以根据自己的实际需求去对service层进行编辑。

TaskService.java

package com.test.quartz.service;

import org.springframework.stereotype.Service;

/**

* @author yd

* @date 2019-06-11 16:11

* @describe 文件描述

*/

@Service

public class TaskService {

public String process(String taskname){

return this.getClass().getSimpleName()+"在处理任务:"+taskname;

}

}

执行结果

89600038f193afb16c1a2f596f9b00a7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值