日期:2014-05-16  浏览次数:20732 次

quartz 1.7.3+spring3无数据库集成配置
Spring配置文件:
<bean id="httpAvailableJob" class="cmcc.gz.bc.collect.HttpAvailableJob">
		 <property name="dao" ref="dao"/>
    </bean>
    <!-- 调度任务执行的方法 -->
    <bean id="schedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="httpAvailableJob"/>
        <property name="targetMethod" value="job"/>
        <property name="concurrent" value="false"/><!-- 多个job不会并发运行,第二个job将不会在第一个job完成之前开始。 -->
    </bean>
    <!-- 从数据库中取得调度时间 -->
    <bean id="cronTrigger" class="cmcc.gz.bc.collect.HttpAvailableCronTrigger">
        <property name="jobDetail" ref="schedulerJobDetail"/>
        <property name="dao" ref="dao"/>
    </bean>
    <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local="cronTrigger"/>
            </list>
        </property>
	</bean>

HttpAvailableCronTrigger类:
package cmcc.gz.bc.collect;

import java.io.Serializable;
import java.text.ParseException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;

import cmcc.gz.common.dao.hibernate.IHibernateDao;

public class HttpAvailableCronTrigger extends CronTriggerBean  implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private Logger log = Logger.getLogger(this.getClass().getName());
	/**
	 * 注入数据层
	 */
	private IHibernateDao dao;
	public IHibernateDao getDao() {
		return dao;
	}
	public void setDao(IHibernateDao dao)  throws ParseException{
		this.dao = dao;
		String cronExpression = getCronExpressionFromDB ();    // ①
        // 因为extends CronTriggerBean ,此处调用父类方法初始化cronExpression
		setCronExpression(cronExpression);
	}
	/*public HttpAvailableCronTrigger() throws ParseException {
	}*/
	public String getCronExpressionFromDB(){
		 log.debug("从数据库读取数据采集调度时间值CRON表达式:"+dao);
		 String dbCronExpression = "0/10 * * * * ?";
         return dbCronExpression;
	 }
}

HttpAvailableJob类:
package cmcc.gz.bc.collect;

import java.io.Serializable;
import java.text.ParseException;

import javax.annotation.Resource;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.stereotype.Service;

import cmcc.gz.common.dao.hibernate.IHibernateDao;

public class HttpAvailableJob {
	private Logger log = Logger.getLogger(this.getClass().getName());
	/**
	 * 注入数据层
	 */
	private IHibernateDao dao;
	 public IHibernateDao getDao() {
		return dao;
	}
	public void setDao(IHibernateDao dao) {
		this.dao = dao;
	}
	private void job() throws SchedulerException, ParseException {
		 	log.debug("开始执行网站可用性检测方法:"+dao);
		 	HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet("http://www.qq.com");
			//测试505错误:http://www.trgov.gov.cn/Board_News.asp?ID=270
			int timeout=10000;
			httpclient.getParams().setIntParameter("http.socket.timeout",timeout);//设置超时时间10秒
			String httpResult="";
			try {
				HttpResponse response = httpclient.execute(httpget);	
				httpResult = response.getStatusLine().getStatusCode()+"";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				httpResult = "请求超时,超时设置时间为:"+(timeout/1000)+"秒";
				//e.printStackTrace();
			}finally{
				log.debug("返回结果