java定时任务spring+quartz作业调度

适用于无期限数据挖掘。

Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序

Quartz应用能被集群。集群提供以下好处: 伸缩性 、高可用性 、负载均衡
Quartz可以借助关系数据库和JDBC作业存储支持集群。
Terracotta扩展quartz提供集群功能而不需要数据库支持

包:quartz-all-1.8.5.jar



配置文件: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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false">
<property name="triggers">
<!--配置所有任务list-->

           <list>

 <ref local="oneTestJob" /> <!--名字与下面id对应-->
</list>
</property>
</bean>

<!-- 测试调度 -->
<bean id="oneTestJob" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="tvMethodInvokingJob" /><!--与下面bean id 一致-->
</property>
<property name="cronExpression">
<!-- 15秒一次 -->
<value>*/15 * * * * ?</value>
</property>
</bean>
<bean id="tvMethodInvokingJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="oneTestBo" /><!--配置bean-->
</property>
<property name="targetMethod">
<value>runJob</value><!--让它执行runjob方法-->
</property>
</bean>
</beans>



web.xml 配置quartz 的xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>


/**
 * @description: 调度测<br>
 * @author: jialiuyang
 * @update: 2015-7
 * @version: 1.0
 */
@Component("oneTestBo")
public class Onetest   { 
@Autowired
private ICommonService commonService;
@Autowired
protected CommonDAO commonDAO;
private final Logger log = Logger.getLogger(this.getClass());
public synchronized void runJob()   {
try {
work(); 
} catch (Exception e) {
log.error("系统处理失败", e);
}
}
private void work() {
System.out.println("轮询调度执行。。。");

}


启动tomcate。。










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章