0
点赞
收藏
分享

微信扫一扫

Spring项目 使用quartz定时执行任务


1.导入jar包

我使用的jar包版本是quartz-all-1.8.5。jar包下载地址参考:​​https://www.cr173.com/soft/65793.html​​

2.配置application-context文件

<!--执行任务类--->
<bean id="aaa" class="com.xxx.timing.test.timing"></bean>
<!--配置定时任务-->
<bean id ="compareJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name ="targetObject" ref ="aaa"/>
<property name ="targetMethod" value ="run"/>
<property name ="concurrent" value ="false"/>
</bean>
<bean id ="compareTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name ="jobDetail" ref ="compareJob" />
<property name ="cronExpression" >
<!--没5秒执行一次-->
<value>*/5 * * * * ?</value>
</property >
</bean >

<bean id ="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name ="triggers" >
<list >
<ref local ="compareTrigger"/>
</list >
</property >
</bean >

3.执行任务类代码

package com.xxx.timing.test;
import org.quartz.JobExecutionException;
public class timing{

public void run()throws JobExecutionException{
long ms = System.currentTimeMillis();
System.out.println("\t\t" + new Date(ms));
}
}



举报

相关推荐

定时执行任务

0 条评论