Quartz入门示例
??? 文章出处 http://www.cnblogs.com/phinecos/archive/2008/09/03/1283376.html
Spring的scheduling.quartz包中对Quartz框架进行了封装,使得开发时不用写任何QuartSpring的代码就可以实现定时任务。Spring通过JobDetailBean,MethodInvokingJobDetailFactoryBean实现Job的定义。后者更加实用,只需指定要运行的类,和该类中要运行的方法即可,Spring将自动生成符合Quartz要求的JobDetail。
1,创建一个Web项目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar?,commons-logging.jar这几个包.
?????2,创建一个类,在类中添加一个方法execute,我们将对这个方法进行定时调度.
package com.vista.quartz;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HelloWorld {
??? ?private static Log logger = LogFactory.getLog(HelloWorld.class);//日志记录器
??? ??? public HelloWorld()
??? ??? {
??? ??? }
??? ??? public void execute()
??? ??? {
??? ??????? logger.info("Kick your ass and Fuck your mother! - " + new Date()+"正在运行中");
??? ??? }
}
3. Spring配置文件applicationContext.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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
?????????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
?????????? default-autowire="byName">
??? <!-- 要调用的工作类 -->
??? <bean id="quartzJob" lazy-init="false" autowire="no" encoding="UTF-8"?>
<web-app version="2.4"
??? xmlns="http://java.sun.com/xml/ns/j2ee"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
??? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
???? <context-param>
??????? <param-name>contextConfigLocation</param-name>
??????? <param-value>
??????????? /WEB-INF/spring/applicationContext.xml//配置文件的存放目录
??????? </param-value>
??? </context-param>
???
??? <servlet>
??????? <servlet-name>SpringContextServlet</servlet-name>
??????? <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
??????? <load-on-startup>1</load-on-startup>
??? </servlet>
? <welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>