引言
定时任务在项目中的使用场景还是非常多的,quarty框架也是非常不错的,尤其是和spring整合以后,在使用上更加简单了,这也是备受大家喜爱的原因,在这里不得不佩服spring的强悍的功力。其实想要达到定时任务的方式有很多,比如我在上一个项目中,什么框架都没有使用,直接用while...true 就实现了,还有通过linux系统本身的调度来实现,在这篇文章中主要介绍quarty这个任务调度的两种实现方式(xml配置和注解)。
准备工作:
在已有的ssm框架中,引入下面jar:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz.version}</version> </dependency>
第一种: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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!--需要时 打开--> <!--<ref bean="sendCaseTrigger"/>--> </list> </property> <property name="quartzProperties"> <props> <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop> </props> </property> </bean> <!--定时给fs推送案件任务--> <bean id="sendCaseTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="sendCaseTask"/> </property> <property name="cronExpression"> <!-- 每秒执行一次 --> <value>0/3 * * * * ? </value> </property> </bean> <bean id="sendCaseTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="sendCaseTaskJob"/> </property> <property name="targetMethod"> <value>sendCaseTask</value> </property> </bean> <bean id="sendCaseTaskJob" class="com.zqf.urgerobot.task.service.TaskService"></bean> <!--定时任务二--> <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="testTask"/> </property> <property name="cronExpression"> <!-- 每秒执行一次 --> <value>0/1 * * * * ? </value> </property> </bean> <bean id="testTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="testTaskBean"/> </property> <property name="targetMethod"> <value>test</value> </property> </bean> <bean id="testTaskBean" class="com.zqf.urgerobot.task.service.TaskService"></bean> </beans>
业务实现类:
@Service public class TaskService { private static Logger log = LoggerFactory.getLogger(TaskService.class); @Autowired private FsCallService fsCallService; /** * @description 案件推送任务 * @param * @date 10:45 2018/8/25 * @return * @author zhenghao */ public void sendCaseTask(){ fsCallService.sendCaseTask(); } public void test(){ System.out.println("test"); } }
这种方式相比没有和spring整合的时候已经非常简单了,但是在配置多个任务的时候,还是比较繁琐一些。下面看一下注解的方式实现。
第二种:注解方式实现
@Scheduled(cron = "0/3 * * * * ?") public void test(){ System.out.println("test"); }
在我们业务处理方法上,添加一个注解@Scheduled即可,我们最关注的是cron这个参数,它的作用也就是控制方法执行的时机。下面看一下这个注解又实现了那些东西:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) public @interface Scheduled { String cron() default ""; String zone() default ""; long fixedDelay() default -1L; String fixedDelayString() default ""; long fixedRate() default -1L; String fixedRateString() default ""; long initialDelay() default -1L; String initialDelayString() default ""; }
我们在ssm中多处使用注解,所以对注解非常的熟悉,我们仅仅在方法上添加注解是远远不够的,如果想让注解起到作用我们还需要告诉spring容器,我们要启动这个注解,所以我们还需要增加下面配置:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd" > <context:component-scan base-package="com.zqf.urgerobot" /> <task:annotation-driven scheduler="synScheduler"/> <task:scheduler id="synScheduler" pool-size="10"/> </beans>
配置文件中的几个配置还是非常简单的,需要注意的是:在Spring配置文件中为task增加相应的命名空间。
总结
注解的方式相对xml方式又简单了很多,简单配置以后,就可以灵活的使用,所以在这个项目中,我采用了注解的方式。希望对各位读者有所帮助。
心情一抒:
心中有所念,喜忧各参半!@过去