Quartz作业调度(2)

来源:互联网 发布:淘宝卖家在哪里看等级 编辑:程序博客网 时间:2024/05/22 12:52

在spring中使用

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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><!-- 定时任务 --><bean id="quartzJob" class="com.test.quartz.ScheduleJob" scope="prototype" /><!-- 绑定任务bean 和目标方法 --><bean id="jobOne"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="quartzJob" /></property><property name="targetMethod"><value>JobOne</value></property><property name="concurrent" value="false" /></bean><!-- 设置执行时间 --><bean id="jobOneTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="jobOne"></property><!-- 系统会在每分钟的0秒执行调度任务。 --><property name="cronExpression"><!-- 每秒执行 --><value>0/30 * * * * ?</value></property></bean><!-- 定时任务2--><bean id="jobTwo"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="quartzJob" /></property><property name="targetMethod"><value>JobTwo</value></property><property name="concurrent" value="false" /></bean><!-- 设置执行时间 --><bean id="jobTwoTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="jobTwo"></property><!-- 系统会在每分钟的0秒执行调度任务。 --><property name="cronExpression"><!-- 每秒执行 --><value>0/1 * * * * ?</value><!-- 每分钟执行1次 --><!-- <value>0 0/01 * * * ?</value> --></property></bean><!-- 调度器执行 --><bean id="startQuertz" lazy-init="false"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers" ><list><ref bean="jobOneTrigger" /><ref bean="jobTwoTrigger" /></list></property></bean></beans>


java

package com.test.quartz;import java.util.Date;public class ScheduleJob {public void JobOne(){System.out.println("Job 1 "+ new Date());}public void JobTwo(){System.out.println("Job 2 "+ new Date());}}



0 0
原创粉丝点击