Quartz 框架快速入门(四)
在上一篇文章《 Quartz 框架快速入门(三) 》中我们将示例迁移到 Web 环境下了,但使用的是 Quartz 的启动机制,这一篇中我们将让 Web 服务器启动 Spring ,通过 Spring 的配置文件来进行任务的调度
1, 创建一个 Web 项目, 加入 spring.jar , quartz-1.6.0.jar , commons-collections.jar , jta.jar , commons-logging.jar 这几个包 .
2, 创建一个类,在类中添加一个方法 execute, 我们将对这个方法进行定时调度.
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
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());
}
}
2. Spring 配置文件 applicationContext.xml 修改如下:
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
<!-- 要调用的工作类 -->
< bean id ="quartzJob" class ="com.vista.quartz.HelloWorld" ></ bean >
<!-- 定义调用对象和调用对象的方法 -->
< bean id ="jobtask" class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<!-- 调用的类 -->
< property name ="targetObject" >
< ref bean ="quartzJob" />
</ property >
<!-- 调用类中的方法 -->
< property name ="targetMethod" >
< value > execute </ value >
</ property >
</ bean >
<!-- 定义触发时间 -->
< bean id ="doTime" class ="org.springframework.scheduling.quartz.CronTriggerBean" >
< property name ="jobDetail" >
< ref bean ="jobtask" />
</ property >
<!-- cron表达式 -->
< property name ="cronExpression" >
< value > 10,15,20,25,30,35,40,45,50,55 * * * * ? </ value >
</ property >
</ bean >
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
< bean id ="startQuertz" lazy-init ="false" autowire ="no" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name ="triggers" >
< list >
< ref bean ="doTime" />
</ list >
</ property >
</ bean >
</ beans >
3, 先在控制台中对上面的代码进行测试,我们要做的只是加载 Spring 的配置文件就可以了,代码如下 :
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
System.out.println( " Test start. " );
ApplicationContext context = new ClassPathXmlApplicationContext( " applicationContext.xml " );
// 如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
// context.getBean("startQuertz");
System.out.print( " Test end.. " );
}
}
4, 然后将 Web.xml 修改如下,让 tomcat 在启动时去初始化 Spring :
< 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/classes/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 >
5, 最后启动 Tomcat, 测试结果如下图所示:
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License ,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。