您的位置:首页 > 编程语言 > Java开发

Quartz 集成Spring实现简单的任务调度Demo

2017-11-03 22:31 645 查看
  代码:

#配置文件

<?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"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
<bean id="quartzService" class="com.haiyi.service.QuartzService"/>
<!-- 配置一个Job -->
<bean id="printInfoJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.haiyi.bean.PrintInfoJob"/>
<property name="jobDataAsMap">
<map>
<entry key="quartzService" value-ref="quartzService"></entry>
</map>
</property>
</bean>

<!-- 简单的触发器 -->
<bean id="simplePrintInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="printInfoJob"/>
</property>
<property name="startDelay">
<value>6000</value>
</property>
<property name="repeatInterval">
<value>6000</value>
</property>
</bean>

<!--复杂的触发器(Cron表达式) -->
<bean id="MyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="printInfoJob"/>
</property>
<property name="cronExpression">
<!-- <value>0 0/1 * * * ?</value> -->
<value>0/5 * 21,22 * * ?</value>
</property>
</bean>

<!-- spring触发工厂 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="MyTrigger"/>
</list>
</property>
</bean>

</beans>

#需要调度的服务层

package com.haiyi.service;

import java.text.SimpleDateFormat;
import java.util.Date;

public class QuartzService {
public void Print(){
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("任务执行时间为: " + sf.format(date));
}
}

#任务调度执行类

package com.haiyi.bean;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.haiyi.service.QuartzService;

@Component(value="printInfoJob")
public class PrintInfoJob extends QuartzJobBean{
private QuartzService quartzService;

public QuartzService getQuartzService() {
return quartzService;
}

public void setQuartzService(QuartzService quartzService) {
this.quartzService = quartzService;
}

@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
this.quartzService.Print();
}
}


#简单测试类

package com.haiyi.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.haiyi.service.QuartzService;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("测试开始......");
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("测试结束......");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: