任务调度 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/ ...
随机推荐
- python全栈开发之OS模块的总结
OS模块 1. os.name() 获取当前的系统 2.os.getcwd #获取当前的工作目录 import os cwd=os.getcwd() # dir=os.listdi ...
- Delphi 正则表达式语法(3): 匹配范围
Delphi 正则表达式语法(3): 匹配范围 // [A-Z]: 匹配所有大写字母 var reg: TPerlRegEx; begin reg := TPerlRegEx.Create(n ...
- 百度feed 寒假实习 一面二面(offer)
一面(1小时) 自我介绍,研究方向,本科学校,家乡等等.. 1 介绍了jdd风险登录的比赛,问题背景,建模,特征构建,特征选择,模型等. 个人觉得介绍项目一定要高大上一点,把自己创新或者有创意的点子讲 ...
- 解决Can't connect to local MySQL server through socket '/tmp/mysql.sock'
0 Problem 电脑重启后用python的MySQLdb连接数据库报错 Can't connect to local MySQL server through socket '/tmp/mysql ...
- with as (cte common table expression) 公共表表达式
SQL中 with as 的用法——使用公用表表达式(CTE) 公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的 ...
- 存储库之mongodb,redis,mysql
一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性 ...
- MapReduce:将下面的两排数字先按第一排排序,然后再按第二排排序,要求顺序排序
MapReduce:将下面的两排数字先按第一排排序,然后再按第二排排序,要求顺序排序 文件如下: 这个案例主要考察我们对排序的理解,我们可以这样做: 代码如下(由于水平有限,不保证完全正确,如果发现错 ...
- spark SQL学习(load和save操作)
load操作:主要用于加载数据,创建出DataFrame save操作:主要用于将DataFrame中的数据保存到文件中 代码示例(默认为parquet数据源类型) package wujiadong ...
- 网络数据包头部在linux网络协议栈中的变化
接收时使用skb_pull()不断去掉各层协议头部:发送时使用skb_push()不断添加各层协议头部. 先说说接收: * eth_type_trans - determine the packet' ...
- 移动端给img元素添加content: "";
误给img原始添加 content: "";属性后发现在ios系统中图片是不会显示的android系统是正常的