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。。










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