废话不多说,我的任务的配置文件都是交给 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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 加入注解支持 --> <context:annotation-config /> <!-- 启用Spring对@AspectJ的支持 --> <aop:aspectj-autoproxy /> <context:component-scan base-package="com.jiyuan.qqapp"> </context:component-scan> <import resource="applicationContext-giant.xml" /> <import resource="dataSource-config.xml" /> <import resource="services.xml" /> <import resource="sessionFactory-config.xml" /> <import resource="transaction-config.xml" /> <import resource="task-config.xml" /> </beans>
任务配置文件 task-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 要调用的工作类 --> <bean id="qqmsgTask" class="com.jiyuan.qqapp.task.busqq.GetQQMsgTask"/> <!-- 定义调用对象和调用对象的方法 --> <bean id="getqqmsgTsk" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject" ref="qqmsgTask" /> <!-- 调用类中的方法 --> <property name="targetMethod" value="deleteAttachments" /> </bean> <!-- 定义触发时间 --> <bean id="getqqmsgTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="getqqmsgTsk" /> <!-- cron表达式 此处定义为一直触发执行任务 --> <!--<property name="cronExpression" value="10,15,20,25,30,35,40,45,50,55 * * * * ?" /> --> <!-- run every morning at 0 AM --> <!-- 10 0/1 * * * ? every minute --> <property name="cronExpression" value="0 0 0 * * ?" /> </bean> <!-- 总管理类 如果将lazy-init=‘false‘那么容器启动就会执行调度程序 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="getqqmsgTrigger" /> </list> </property> </bean> </beans>
通过上面配置文件基本上可以看出最主要的执行任务类就是com.jiyuan.qqapp.task.busqq.GetQQMsgTask
要执行的方法是 deleteAttachments
这样就能够 在每天0点执行GetQQMsgTask的deleteAttachments方法了,是不是很简单
package com.jiyuan.qqapp.task.busqq; import org.springframework.beans.factory.annotation.Autowired; import com.jiyuan.qqapp.log.service.LogService; /** * 获取企业QQ聊天记录任务 * 名称: GetQQMsgTask.java * 描述: * 类型: JAVA * 最近修改时间:2015-7-2 下午5:50:31 * @since 2015-7-2 * @author liaokn */ public class GetQQMsgTask { private LogService logService; public LogService getLogService() { return logService; } @Autowired public void setLogService(LogService logService) { this.logService = logService; } /** * */ public void deleteAttachments() { try { System.out.println("删除测试"); } catch (Exception e) { e.printStackTrace(); } } }
使用到的JAR包
c3p0-0.9.1.1.jar
log4j-1.2.16.jar
quartz-2.2.1.jar
quartz-jobs-2.2.1.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
时间: 2024-11-05 21:55:20