1、下载quartz的相应jar包
2、增加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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 需要定时执行的类 -->
<bean id="heartTask" class="com.tf.user.util.BoHeartTask">
<property name="heartHref" value="http://localhost:8080/ScientificMarketing/bo_heart.jsp"></property>
<property name="loginHref" value="http://localhost:8080/ScientificMarketing/user.do?action=bologin"></property>
</bean>
<!-- 心跳任务-->
<bean id="taskJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="heartTask"></property>
<property name="targetMethod">
<value>doHeart</value>
</property>
</bean>
<!-- 登陆任务 -->
<bean id="loginJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="heartTask"></property>
<property name="targetMethod">
<value>doFirstLogin</value>
</property>
</bean>
<!-- 每15分钟心跳一次-->
<bean id="runTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="taskJob" />
</property>
<property name="cronExpression">
<value>0 0/15 * * * ?</value>
</property>
</bean>
<!-- 服务器启动后延迟1秒发登陆请求 -->
<bean id="loginTime" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="loginJob" />
</property>
<property name="startDelay" value="1000" />
<property name="repeatInterval" value="0" />
<property name="repeatCount" value="0" />
</bean>
<!-- 最终启动的配置 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="runTime" />
<ref bean="loginTime" />
</list>
</property>
</bean>
</beans>
其中配置任务的定时执行的有2个类可供选择:
org.springframework.scheduling.quartz.CronTriggerBean
org.springframework.scheduling.quartz.SimpleTriggerBean
CronTriggerBean类的主要属性:
jobDetail:目标执行job的bean
cronExpression:一个表达式,主要配置多久循环一次
SimpleTriggerBean的主要属性:
jobDetail:目标执行job的bean
startDelay:第一次执行的时间,延迟多久执行
repeatInterval:任务执行周期
repeatCount:执行次数
任务类BoHeartTask.java
package com.tf.user.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class BoHeartTask {
private static Log log = LogFactory.getLog(BoHeartTask.class);
private String heartHref;
private String loginHref;
public void setLoginHref(String loginHref) {
this.loginHref = loginHref;
}
public void setHeartHref(String heartHref) {
this.heartHref = heartHref;
}
public void doFirstLogin() throws IOException{
URL url = new URL(loginHref);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setReadTimeout(30000);
con.connect();
int state = con.getResponseCode();//网页状态码
String logStr = "----BO登陆连接,状态码:"+state+",时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(logStr);
log.info(logStr);
}
public void doHeart() throws IOException{
URL url = new URL(heartHref);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setReadTimeout(30000);
con.connect();
int state = con.getResponseCode();//网页状态码
String logStr = "----心跳链接BO,状态码:"+state+",时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(logStr);
log.info(logStr);
}
}
cronExpression配置说明
星期的简写:
周一 MON 周二 TUE 周三 WED 周四 THU 周五FRI 周六 SAT 周日 SUN
在MONTH和Day Of Week字段里对字母大小写不敏感