Spring定时任务[不定时添加任务]
这里讲叙这进在工作上用的到Spring任务。
在项目中,我们需要定时任务来发送微博。当我添加微博后,选择指定的时间发送微博信息时,我们需要添加一个任务[这里是不定时的添加任务],下面的代码就是实现这个功能。
这里我们需要用到Spring的jar包,如果Spring中不包含quartz.jar包,我们这需要下载,然后引用。
下面为web.xml的配置:
01
<?xml version="1.0" encoding="UTF-8"?>
02
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
03
<display-name>SpringTask</display-name>
04
<context-param>
05
<param-name>contextConfigLocation</param-name>
06
<param-value>classpath*:config/spring/applicationContext.xml,classpath*:config/spring/applicationContext-quartz.xml</param-value>
07
</context-param>
08
<listener>
09
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10
</listener>
11
<listener>
12
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
13
</listener>
14
<welcome-file-list>
15
<welcome-file>index.html</welcome-file>
16
<welcome-file>index.htm</welcome-file>
17
<welcome-file>index.jsp</welcome-file>
18
<welcome-file>default.html</welcome-file>
19
<welcome-file>default.htm</welcome-file>
20
<welcome-file>default.jsp</welcome-file>
21
</welcome-file-list>
22
</web-app>
Spring的配置文件[这里主要配置了Spring的注解、任务]:
01
<?xml version="1.0" encoding="UTF-8"?>
02
<beans xmlns="http://www.springframework.org/schema/beans"
03
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04
xmlns:jaxws="http://cxf.apache.org/jaxws"
05
xmlns:aop="http://www.springframework.org/schema/aop"
06
xmlns:tx="http://www.springframework.org/schema/tx"
07
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
08
xmlns:context="http://www.springframework.org/schema/context"
09
xsi:schemaLocation="
10
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
11
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
12
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
13
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
15
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
16
17
<!-- enable autowire -->
18
<context:annotation-config />
19
20
<!--自动扫描base-package下面的所有的java类,看看是否配置了annotation -->
21
<context:component-scan base-package="com.jing.spring" />
22
23
<!-- enable transaction demarcation with annotations -->
24
<tx:annotation-driven />
25
26
<!-- 定时任务的配置 -->
27
<bean id="scheduler" class="org.springframework.sche易做图ng.quartz.SchedulerFactoryBean" lazy-init="false">
28
<property name="triggers">
29
<list>
30
</list>
31
</property>
32
</bean>
33
</beans>
java文件:
Task.java[达到任务调用时执行的类]:
01
package com.jing.spring.scheduler.task;
02
03
import org.apache.log4j.Logger;
04
import org.springframework.stereotype.Component;
05
06
/**
07
* 定时任务类
08
* <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a> jing.yue
09
* @version 2012/07/24 1.0.0
10
*/
11
@Component
12
public class Task extends TaskAbs {
13
14
// log4j日志
15
private static final Logger logger = Logger.getLogger(Task.class);
16
17
/**
18
* 调用定时任务
19
* @param taskId
20
* @param taskType
21
*/
22
@Override
23
public void startTask(String taskId, String taskType) {
24
// TODO Auto-generated method stub
25
try {
26
logger.info("执行TaskJob任务...");
27
logger.info("任务编号: " + taskId + "\t任务类型: " + taskType);
28
logger.info("在这里加入您需要操作的内容...");
29
} catch (Exception e) {
30
logger.info("发送微博的定时任务类--出错");
31
logger.info(e,e);
32
}
33
}
34
}
TaskAbs.java[任务调用的抽象类]:
01
package com.jing.spring.scheduler.task;
02
03
/**
04
* 定时任务抽象类
05
* <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a> jing.yue
06
* @version 2012/07/24 1.0.0
07
*/
08
补充:移动开发 , Android ,