任务调度 Spring Task 4(二 )
注解和配置文件两种
第一种:配置文件方式
第一步:编写作业类
即普通的pojo,如下:
import org.springframework.stereotype.Service;
@Service
public class TaskJob { public void job1() {
System.out.println(“任务进行中。。。”);
}
}
Java代码
第二步:在spring配置文件头中添加命名空间及描述
<</span>beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
。。。。。。 http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
>
XML代码
第三步:spring配置文件中设置具体的任务
<</span>task:scheduled-tasks>
<</span>task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
</</span>task:scheduled-tasks> <</span>context:component-scan base-package=" cn.springmvc" />
XML代码
第四步:启动测试
package cn.springmvc.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StartConfig {
public static void main(String[] args){
System.out.println("启动spring容器");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
示例
二:annotation 注解配置
第一步:配置测试类
package cn.springmvc.quartz; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component("annotationTask")
public class AnnotationTask {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void job1() {
System.out.println("spring task 注解使用。。。任务进行中");
}
}
测试代码
第二步: 配置配置文件:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
。。。 <</span>task:annotation-driven scheduler="qbScheduler" mode="proxy"/> // 理论上只需要这么一句就可以了
<</span>task:scheduler id="qbScheduler" pool-size="10"/>
配置文件
第三步:启动测试
package cn.springmvc.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StartConfig {
public static void main(String[] args){
System.out.println("启动spring容器");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
示例
实现TaskScheduler 接口的ThreadPoolTaskExecutor 继承关系。 
ThreadPoolTaskExecutor 是TaskExecutor 的最主要实现类之一。该类的核心继承关系如下图所示。 
ThreadPoolTaskExecutor 接口扩展了重多的接口,让其具备了更多的能力。要实现异步需要标注@Async 注解:
- AsyncTaskExecutor 增加了返回结果为Future 的submit() 方法,该方法的参数为Callable 接口。相比Runnable 接口,多了将执行结果返回的功能。
- AsyncListenableTaskExecutor 接口允许返回拥有回调功能的ListenableFuture 接口,这样在结果执行完毕是,能够直接回调处理。
任务调度 Spring Task 4(二 )的更多相关文章
- 任务调度 Spring Task 4(一)
深入浅出spring task定时任务 在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提 ...
- Spring 使用介绍(十二)—— Spring Task
一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...
- 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job
任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...
- Spring任务调度器之Task的使用
Spring Task提供两种方式进行配置,正如大家所想吧,还是一种是annotation(标注),而另外一种就是XML配置了.但其实这里我觉得比较尴尬,因为任务调度这样的需求,通常改动都是比较多的, ...
- Spring任务调度器之Task的使用(转)
文章转自 http://blog.csdn.net/l454822901/article/details/51829307 最近发现真的凹凸了,spring升级到3后原来已经自带任务调度器了,之前还一 ...
- 基于Spring Task的定时任务调度器实现
在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...
- spring-boot-route(二十)Spring Task实现简单定时任务
Spring Task是Spring 3.0自带的定时任务,可以将它看作成一个轻量级的Quartz,功能虽然没有Quartz那样强大,但是使用起来非常简单,无需增加额外的依赖,可直接上手使用. 一 如 ...
- java Quartz定时器任务与Spring task定时的几种实现,
java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置 请查阅 http://www.cnblogs.com/si ...
- Quartz和Spring Task定时任务的简单应用和比较
看了两个项目,一个用的是Quartz写的定时器,一个是使用spring的task写的,网上看了2篇文章,写的比较清楚,这里做一下留存 链接一.菠萝大象:http://www.blogjava.net/ ...
随机推荐
- 企业级web nginx服务优化
1.1)隐藏nginx header 内版本号信息 [root@aliyun ~]# vi /application/nginx/conf/nginx.conf http{ …… server_tok ...
- 常用模块(random,os,json,pickle,shelve)
常用模块(random,os,json,pickle,shelve) random import random print(random.random()) # 0-1之间的小数 print(rand ...
- MyBatis For .NET学习- 初识MyBatis
MyBatis的框架. Introduction MyBatis本是apache的一个开源项目iBatis,2010年这个项目由 apache software foundation迁移到了googl ...
- linux sed批量替换多个文件内容
sed -i "s/lgside/main/g" `grep -rl lgside /home/zn/work/project-template` 注意标点符号:`
- Oracle事务和锁机制
事务 1. 说明 一组SQL,一个逻辑工作单位,执行时整体修改或者整体回退. 2.事务相关概念 1)事务的提交和回滚:COMMIT/ROLLBACK 2)事务的开始和结束 开始事务:连接到数据库,执行 ...
- hadoop14---centos 安装activemq
创建activemq目录 [root@node1 ~]# mkdir -p /usr/local/activemq 狐火下载activemq,从用户/download目录把文件cp到/usr/loca ...
- JDK1.8之Stream
为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...
- resin启动不成功
resin启动只有一个进程,但显示启动正常: Resin/4.0.35 launching watchdog at 127.0.0.1:28084Resin/4.0.35 started -serve ...
- c++中小项堆声明和使用
c++默认是大顶堆,小顶堆有两种声明方法: 1.对于基本类型直接用 priority_queue<int, vector<int>, greater<int> >p ...
- maven中使用dom4j解析、生成XML的简易方法
此片文章主要写一些关于如何在maven工程中使用dom4j来解析或生成XML的建议方法,实际可使用的写法不仅限于如下所写的样例代码.此处进攻快速入手和提供思路使用. 首先配置pom.xml中的依赖的包 ...