关于本文
本文通过一个简单的任务调度应用,介绍了如何使用 schedule 来构建 Web 应用程序。本教程以讲解实例为主,为了读者更好地理解 schedule任务调度 ,也有部分理论的解释。
先决条件
本文要求读者具备 Java Web 应用的基本知识、熟悉 Spring Framework 的应用。
系统要求
运行本教程中的示例,需要下列工具:
schedule2.0特性(本文使用schedule版本2.0.1)
1、 任务调度分配器的目标: 让所有的任务不重复,不遗漏的被快速处理
2、 一个Manager只管理一种任务类型的一组工作线程
3、 在一个JVM里面可能存在多个处理相同任务类型的Manager,也可能存在处理不同任务类型的Manager
4、 在不同的JVM里面可以存在处理相同任务的Manager
5、 调度的Manager可以动态的随意增加和停止
6、 可以通过JMX控制调度服务的创建和停止
7、 可以指定调度的时间区间
要了解 schedule 是什么东西,最好的办法莫过于跑应用示例,图 1 展示了一个简化的任务处理,即:将状态STS为N的所有数据更新为Y。
图 1
这里我们把每一条数据更新看做一个任务,使用schedule进行大批量任务的处理。关于schedule的基本元素概率可在下面网址中获取。
http://code.taobao.org/trac/taobao-pamirs-schedule/wiki/ZhWikiStart
配置数据库初始化表和业务处理表
执行createSql4MySql.sql文件创建schedule需要的表结构,这里数据库取名为schedule
创建完schedule基础表后,执行createTest4MySql.sql文件用于初始化schedule需要初始化的任务基础数据,已经本demo中需要处理的简单业务表schedule_test
此时我们的schedule_test表中已有10万条STS为N的数据
创建Web应用的目录结构
本示例应用将采用 eclipse Dynamic Web Project 向导默认生成的目录结构src/main/resources 源目录用来存放各种配置文件, schedule.xml即为schedule的核心配置文件。最后目录如图2所示:
图2目录结构
Schedule Demo依赖的类库
pom.xml文件中添加如下依赖:
1. <dependencies>
2. <dependency>
3. <groupId>org.apache.openejb</groupId>
4. <artifactId>javaee-api</artifactId>
5. <version>5.0-1</version>
6. </dependency>
7. <dependency>
8. <groupId>com.taobao.pamirs.schedule</groupId>
9. <artifactId>taobao-pamirs-schedule</artifactId>
10. <version>2.0.1</version>
11. </dependency>
12. <dependency>
13. <groupId>org.slf4j</groupId>
14. <artifactId>slf4j-log4j12</artifactId>
15. <version>1.5.6</version>
16. </dependency>
17. <dependency>
18. <groupId>mysql</groupId>
19. <artifactId>mysql-connector-java</artifactId>
20. <version>5.0.5</version>
21. </dependency>
22. <dependency>
23. <groupId>org.unitils</groupId>
24. <artifactId>unitils</artifactId>
25. <version>2.4</version>
26. <scope>test</scope>
27. </dependency>
28. <dependency>
29. <groupId>junit</groupId>
30. <artifactId>junit</artifactId>
31. <version>4.5</version>
32. <scope>test</scope>
33. </dependency>
34. <dependency>
35. <groupId>org.springframework</groupId>
36. <artifactId>spring</artifactId>
37. <version>2.5.6.SEC02</version>
38. </dependency>
39. <dependency>
40. <groupId>org.springframework</groupId>
41. <artifactId>spring-test</artifactId>
42. <version>3.0.5.RELEASE</version>
43. </dependency>
44. <!-- cxf -->
45. <dependency>
46. <groupId>org.apache.cxf</groupId>
47. <artifactId>cxf-rt-frontend-jaxws</artifactId>
48. <version>${cxf.version}</version>
49. </dependency>
50. <dependency>
51. <groupId>org.apache.cxf</groupId>
52. <artifactId>cxf-rt-transports-http</artifactId>
53. <version>${cxf.version}</version>
54. </dependency>
55. </dependencies>
此处cxf的两个依赖(版本为2.2.3)用于暴露web服务后面会做介绍。
web.xml声明spring监听器 并指定配置文件
1. <!-- Spring Config Location -->
2. <context-param>
3. <param-name>contextConfigLocation</param-name>
4. <param-value>classpath*:spring.xml</param-value>
5. </context-param>
6. <!-- Spring ContextLoaderListener -->
7. <listener>
8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
9. </listener>