利用spring配置定时任务(可多个)
来源:互联网 发布:上海java培训公司名单 编辑:程序博客网 时间:2024/06/08 13:27
通常在项目中需要定时的去处理相关任务,这时spring管理容器的定时任务就显得方便的多了,下面是自己整理的两种配置方法:
一.使用quartz配置:
①写好需要执行任务的java类
eg:
public class TestSchedueTask {
private static Log log = LogFactory.getLog(TestSchedueTask.class);
public void doTask(){
try {
log.info("定时任务测试开始------------------"+new Date().toLocaleString());
log.info("删除文件--------------"+(new File("/home/ap/was/image/image002.jpg")).delete());
log.info("定时任务测试成功----------------------");
} catch (Exception e) {
log.error(e);
}
}
}
public class TestSchedueTaskOne {
private static Log log = LogFactory.getLog(TestSchedueTask.class);
public void doTask(){
try {
log.info("定时任务测试开始------------------"+new Date().toLocaleString());
log.info("删除文件--------------"+(new File("/home/ap/was/image/image001.jpg")).delete());
log.info("定时任务测试成功----------------------");
} catch (Exception e) {
log.error(e);
}
}
}
②开始在spring的配置文件中进行配置
eg:
<!-- 配置自己的定时任务1 -->
<!--执行任务javabean-->
<bean id="job1"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.git.ecss.timetask.TestSchedueTaskOne" />
</property>
<property name="targetMethod" value="doTask" />
<!-- 当多个任务时,是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<propertyname="concurrent"value="true"/>
</bean>
<!--执行任务的定时管理,即 触发器配置 时间指定 -->
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job1" />
<!--每天12:10执行任务-->
<property name="cronExpression" value="0 10 12 * * ?" />
</bean>
<!-- 配置自己的定时任务2 -->
<!--执行任务javabean-->
<bean id="job2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.git.ecss.timetask.TestSchedueTask" />
</property>
<property name="targetMethod" value="doTask" />
<!-- 当多个任务时,是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<propertyname="concurrent"value="true"/>
</bean>
<!--执行任务的定时管理,即 触发器配置 时间指定 -->
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job2" />
<property name="cronExpression" value="0 10 11 * * ?" />
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<!-- 触发器列表 -->
<list>
<ref bean="cronTrigger1" />
<ref bean="cronTrigger2" />
</list>
</property>
</bean>
二.利用注解直接配置(spring需3.0版本以上)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!-- 定义任务执行类 -->
<bean id="myTask" class="com.young.git.myTask" />
<!-- 在这里定义要执行的任务列表 -->
<task:scheduled-tasks>
<!-- ref:具体执行的任务,method:类中的具体方法,cron表达式 -->
<task:scheduled ref="myTask" method="task" cron="0 10 11 * * ?"/>
</task:scheduled-tasks>
</beans>
执行任务java类
public class myTask {
private static Log log = LogFactory.getLog(TestSchedueTask.class);
public void task(){
try {
log.info("定时任务测试开始------------------"+new Date().toLocaleString());
log.info("删除文件--------------"+(new File("/home/ap/was/image/image002.jpg")).delete());
log.info("定时任务测试成功----------------------");
} catch (Exception e) {
log.error(e);
}
}
}
上述两种方法处理完后,部署后重启服务器即可。
- 利用spring配置定时任务(可多个)
- spring 定时任务配置
- spring 配置定时任务
- spring 配置定时任务
- spring配置定时任务
- Spring定时任务配置
- Spring定时任务配置
- spring 定时任务配置
- spring配置定时任务
- spring定时任务配置
- spring定时任务配置
- Spring 定时任务配置
- spring定时任务配置
- Spring定时任务配置
- spring 定时任务配置
- spring 定时任务配置
- spring定时任务配置
- spring 配置定时任务
- php面向对象之app接口文档
- CocoaPods 安装 使用
- 三星手机CROM锁、OEM锁等概念的区别
- redis cluster安装以及操作
- 链表保存数据
- 利用spring配置定时任务(可多个)
- GDI泄露,程序发布之前的审核
- 时间转换
- 列表、网格等多条目数据控件在内存中实现过滤(筛选)
- bzoj3675: [Apio2014]序列分割
- string和byte[]的转换 (C#)
- 第八周--项目1-建立顺序串的算法库
- 种设置vs 环境变量的简便方法
- gets()和puts()函数分析