spring quartz定时任务配置 淡淡的烟草味﹌ 2022-05-14 04:59 275阅读 0赞 1:新建需要执行定时任务的类,一般写在controller层里面,建好需要执行的方法:![70][] 2.写好配置文件 <?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置调度器工厂(SchedulerFactoryBean) --> <bean name="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myJobTrigger" /> </list> </property> </bean> <!-- 配置Cron触发器(CronTriggerFactoryBean) --> <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property name="cronExpression"> <!-- 每隔45秒钟执行一次 0/45 * * * * ? --> <!-- 本月最后一天0 0 0 L * ? --> <!-- 每一天执行一次0 0 0 1/1 * ? * --> <!-- 每天1点执行0 0 1 * * ? * --> <!-- 指定某几个时刻运行 0 0 1,2,3,4,5,6 * * ? --> <!-- 每个小时执行一次 0 0 0/1 * * ? --> <value>0 0/1 * * * ?</value><!-- 这里是每分钟执行一次 --> </property> </bean> <!-- 配置方法调用任务工厂(XXXJobDetailFactoryBean(操作没有大字段的表)) --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 上一个任务执行完再触发下一次任务 --> <property name="concurrent" value="false"/> <property name="targetObject"> <ref bean="myJob" /> </property> <!-- 要执行的方法 --> <property name="targetMethod"> <value>execute</value> </property> </bean> <!-- 需要执行定时任务的类 --> <bean id="myJob" class="com.dbts.ncz.controller.QutzTest" /> </beans> 3.配置好web.xml文件: <!-- spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <!--默认读取/WEB-INF/下的applicationContext.xml文件。但是通过context-param指定配置文件路径后,便会去你指定的路径下读取对应的配置文件--> <param-value>classpath:conf/spring-*.xml</param-value> </context-param> 4.启动服务,观察效果 [70]: /images/20220514/56447eef8f5a48c1ac41e664a9c6dd91.png
还没有评论,来说两句吧...