Spring 定时任务quartz配置及代码示例详解

前段时间做公司项目,有个需求定期清理一些超过时限的文件。

谈到处理定时任务,quartz应该是目前比较成熟,使用较为广泛的java任务调度框架了,功能强大配置灵活,在企业应用中占重要地位。现在就跟大家一起学习下如何在项目中使用quartz。

首先了解下Quartz的背景
    Quartz是opensymphony组织专攻job scheduling领域又一个开源利器,可以到http://www.opensymphony.com/quartz查看详细信息。Quartz是轻量级的组件,开发人员只需要加载单独的jar包就可以利用Quartz强大的日程安排功能。当然,假如你为Quartz配备了数据库持久化任务的特性,Quartz也可以很好的利用这一点,从而在机器重启后还能够记住你原先安排的计划。

   Quartz几个核心概念

1、Job

表示一个工作,要执行的具体内容。此接口中只有一个方法

void execute(JobExecutionContext context)

 

2、JobDetail

JobDetail表示一个具体的可执行的调度程序,Job是这个可执行程调度程序所要执行的内容,另外JobDetail还包含了这个任务调度的方案和策略,它本身可能是有状态的。

 

3、Trigger代表一个调度参数的配置,什么时候去调。

 

4、Scheduler代表一个调度容器,一个调度容器中可以注册多个JobDetail和Trigger。当Trigger与JobDetail组合,就可以被Scheduler容器调度了。容器启动后,里面的每个JobDetail都会根据trigger按部就班自动去执行。


结构图


下面我们着重学习下如何在spring项目中使用quartz

1)增加jar包依赖:

<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.1</version>
		</dependency>

2)增加spring配置文件 applicationContext-auartz.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 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.2.xsd  
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">


	<!-- Scheduler Config / -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="cacheFlushJobTrigger" />
			</list>
		</property>
		<property name="autoStartup" value="true" />
		<property name="taskExecutor" ref="executor" />
	</bean>
	<bean id="executor"
		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
		<property name="corePoolSize" value="1" />
		<property name="maxPoolSize" value="4" />
		<property name="queueCapacity" value="128" />
	</bean>

	<!-- Trigger Config /// -->
	<bean id="cacheFlushJobTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
		<property name="jobDetail" ref="cacheFlushJobDetail" />
		<property name="cronExpression" value="0 0 1 * * ?" />
	</bean>

	<!-- JobDetail Config  -->
	<bean id="cacheFlushJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="cacheFlushJob" />
		<property name="targetMethod" value="execute" />
		<property name="concurrent" value="false" />
		
	</bean>

	<bean id="cacheFlushJob"
		class="com.acxiom.dha.quartz.CacheFlushJob" />

</beans>

每个任务JobDetail可以绑定多个Trigger,但一个Trigger只能绑定一个任务

3)增加业务类代码

public class CacheFlushJob {
    private static final Logger LOG = Logger.getLogger(CacheFlushJob.class);
    
    @Autowired
    private JobPathService jobPathService;
    
    @Autowired
    private JobService jobService;
    
    @Autowired
    private TemplateConfigurationDao templateConfigurationDao;

    public void execute() throws DhaException{
        int days = getReportExpireDays();  
        String time = getPreDaysTime(days);
        List<JobPath> expiredFileList = jobPathService.getExpiredFileList(time);
        if(!CollectionUtils.isEmpty(expiredFileList)){
            for(JobPath jobPath:expiredFileList){
                String path = jobPath.getPath();
                deleteFile(path);
                int jobId = jobPath.getJobId();
                jobService.updateFileStatus(jobId);
                LOG.debug("file delete succ!");
            }
        }
        
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值