ssm框架整合quartz实现定时任务

时间:2020-12-19 23:24:54

ssm框架搭建在此不做说明
新增一个applicationContext-quartz.xml配置文件,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "
>


<context:component-scan base-package="com.lyt.timer.job" />

<!-- 注入任务处理类bean -->
<bean id="myJob" class="com.lyt.timer.job.MyJob"></bean>
<bean id="otherJob" class="com.lyt.timer.job.OtherJob"></bean>

<!-- 任务触发器详细信息bean -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJob"></property>
<property name="targetMethod" value="execute"></property>
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<bean id="otherDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="otherJob"></property>
<property name="targetMethod" value="execute"></property>
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>

<!-- 定义trigger 触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="cronExpression" value="${crond.myJob}"></property>
</bean>
<bean id="otherTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="otherDetail"></property>
<property name="cronExpression" value="${crond.otherJob}"></property>
</bean>

<!-- 设置触发器调度工厂 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
<ref bean="otherTrigger"/>
</list>
</property>
</bean>

</beans>

设置定时时间crontab.properties

<!-- 每天下午三点执行 -->
crond.myJob=0 0 15 * * ?
crond.otherJob=0 0 15 * * ?

最后必须要在applicationContext-beans.xml配置中引入上述配置,方式如下

<import resource="classpath:spring/applicationContext-quartz.xml"/>

执行代码(main方式运行就好)

package com.lyt.timer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

public class TimerCore {

public static void main(String[] args) {
System.out.println("timer start ...");

ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-beans.xml");

}
}

任务类

package com.lyt.timer.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;
@Component("MyJob")
public class MyJob {

public void execute(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Quartz的任务调度!!!"+format.format(new Date()));

}

}



package com.lyt.timer.job;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lyt.timer.soa.dao.PostOldDao;

@Component
public class OtherJob{

@Autowired private PostOldDao topicDao;

public void execute(){
System.out.println("这是第二个任务开始");
List<Long> lst = topicDao.getPostOldLst();
System.out.println("出来:"+lst.size());
System.out.println("这是第二个任务结束");
}

}

以上就是ssm定时任务简单使用