spring常规任务(轻便易)

spring常规任务(轻便易)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

spring提供了定时任务功能。我们不需要第三者jar包支持。spring够了。

代码:

package com.inth.product.web.task;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.inth.product.service.impl.ContractServiceImpl;

@Component("changeStateTask")
public class ChangeStateTask{
	
	@Autowired
	private ContractServiceImpl contractServiceImpl;
	/**
	 * 定时更改合同状态
	 * 0 0 * * * ?  表示每次秒和分为0时触发一次(每小时一次)
	 * "@Scheduled"时spring提供的定时任务标签
	 * 须要在applicationContext.xml中加入
	 * xmlns:task="http://www.springframework.org/schema/task" 
	 * xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "
	 * 	<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
	 *  <task:scheduler id="qbScheduler" pool-size="10"/
	 * ChangeStateTask.doJob()<BR>
	 * <P>Author : DingYinLong </P>  
	 * <P>Date : 2014年7月28日 </P>
	 */
	@Scheduled(cron = "0 0 * * * ?")
	public void doJob(){
		this.contractServiceImpl.executeStateChange();
	}
	/**
	 * 固定每分钟运行一次
	 * ChangeStateTask.doJob1()<BR>
	 * <P>Author : DingYinLong </P>  
	 * <P>Date : 2014年8月1日 </P>
	 */
	@Scheduled(fixedRate = 60*1000)
	public void doJob1(){
		System.out.println(new Date() + "-----------------doJob1");
	}
	/**
	 * 上次任务结束后一分钟后再次运行
	 * ChangeStateTask.doJob2()<BR>
	 * <P>Author : DingYinLong </P>  
	 * <P>Date : 2014年8月1日 </P>
	 */
	@Scheduled(fixedDelay = 60*1000)
	public void doJob2(){
		System.out.println(new Date() + "-----------------doJob2");
	}
}

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:context="http://www.springframework.org/schema/context"  
    xmlns:task="http://www.springframework.org/schema/task" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
	<!-- 扫描包基础文件夹 -->
    <context:component-scan base-package="com.inth" />
    <!-- 识别@Scheduled注解 -->
	<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/> 
</beans>

注意事项:

1,beans 属性加上xmlns:task=”http://www.springframework.org/schema/task”以及xsi:schemaLocation=”http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd “

2,fixedRate和fixedDelay的差别写在凝视上了。

以上情况不基于注解纯配置例如以下:

代码:

package com.inth.product.web.task;

import java.util.Date;


import com.inth.product.service.impl.ContractServiceImpl;

public class ChangeStateTask{
	private ContractServiceImpl contractServiceImpl;
	public void doJob(){
		System.out.println(new Date() + "-----------------doJob");
//		this.contractServiceImpl.executeStateChange();
	}
	public void doJob1(){
		System.out.println(new Date() + "-----------------doJob1");
	}
	public void doJob2(){
		System.out.println(new Date() + "-----------------doJob2");
	}
}

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:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <!-- 扫描包基础文件夹 --> <context:component-scan base-package="com.inth" /> <bean name="taskJob" class="com.inth.product.web.task.ChangeStateTask"></bean> <task:scheduled-tasks> <task:scheduled ref="taskJob" method="doJob" cron="0/5 * * * * ?

"/> <task:scheduled ref="taskJob" method="doJob1" fixed-rate="5000"/> <task:scheduled ref="taskJob" method="doJob2" fixed-delay="5000"/> </task:scheduled-tasks></beans>

附:cron表达式配置说明 

字段 同意值 同意的特殊字符 

秒 0-59 , – * / 

分 0-59 , – * / 

小时 0-23 , – * / 

日期 1-31 , – * ?

/ L W C 
月份 1-12 或者 JAN-DEC , – * / 
星期 1-7 或者 SUN-SAT , – * ?

/ L C # 
年(可选) 留空, 1970-2099 , – * / 
表达式 意义 
“0 0 12 * * ?” 每天中午12点触发 
“0 15 10 ? * *” 每天上午10:15触发 
“0 15 10 * * ?” 每天上午10:15触发 
“0 15 10 * * ? *” 每天上午10:15触发 
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发 
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发 
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发 
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
“0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发 
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发 
“0 15 10 ?

* MON-FRI” 周一至周五的上午10:15触发 
“0 15 10 15 * ?

” 每月15日上午10:15触发 
“0 15 10 L * ?” 每月最后一日的上午10:15触发 
“0 15 10 ?

* 6L” 每月的最后一个星期五上午10:15触发 
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发 
“0 15 10 ?

* 6#3″ 每月的第三个星期五上午10:15触发 
特殊字符 意义 
* 表示全部值。 
?

表示未说明的值,即不关心它为何值。 
– 表示一个指定的范围; 
, 表示附加一个可能值; 
/ 符号前表示開始时间,符号的每个增量后值; 

版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117000.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea maven project 包加载出错问题

    idea maven project 包加载出错问题错误要点:maven下载包很慢,配置了setting.xml文件之后还是很慢然后我采取的方案是重新导入项目,下载maven ,还是很慢,而且外网的包无法下载,于是我配置了setting国内的仓库,还是无法下载,而且 mavenproject中包倒入出错解决方案:去 C:\Users\Administrator\.IntelliJIdea14\system 

    2022年7月14日
    27
  • VS2013 产品密钥 – 所有版本-亲试,好使!!

    VS2013 产品密钥 – 所有版本-亲试,好使!!VisualStudioUltimate2013KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9VisualStudioPremium2013KEY(密钥):FBJVC-3CMTX-D8DVP-RTQCT-92494VisualStudioProfessional2013KEY(密钥):XDM3T-W3T3V-MGJWK-8B…

    2022年5月19日
    747
  • linux svn服务器搭建和配置_如何搭建web服务器

    linux svn服务器搭建和配置_如何搭建web服务器1.安装SVN服务器:检查是否已安装#rpm-qasubversion安装SVN服务器#yuminstallhttpdhttpd-develsubversionmod_dav_svnmod_auth_mysql验证安装#cd/etc/httpd/modules#ls|grepsvnmod_authz_svn.somod_dav_…

    2022年10月9日
    0
  • 迪奥布兰度正在挑战fgo 小说_god eater resurrection

    迪奥布兰度正在挑战fgo 小说_god eater resurrectiongodis之aof持久化文章目录godis之aof持久化基本说明文件写入加载文件文件重写数据转化为redis命令外部调用基本说明在godis中,只有aof持久化,而没有rdb持久化。aof持久化分为三个基本的模块:将命令持久化到aof文件将aof文件的命令加载到内存aof文件重写文件写入handlerAof函数的作用是将命令持久化到aof文件中。它监听着aof通道并写入到aof文件,在初始化handler的时候,就开启一个子goroutine来执行这个函数。//监听aof通

    2022年10月8日
    0
  • Python匹马行天下之python之父

    Python匹马行天下之python之父龟叔和他的python经过了漫长的旅程,终于要看到主角Python了。Python是现在非常非常流行的编程语言,在我们能看到的大部分编程语言排行榜中,Python都能在前三甲中拥有一席之地,并且发

    2022年7月5日
    21
  • 基于python的情感分析案例_关于python爬虫的情感分析

    基于python的情感分析案例_关于python爬虫的情感分析今天给大家分享的是通过情感词典来对文本进行情感分析最后计算出情感得分通过情感得分来判断正负调性主要步骤:数据准备本次情感词典采用的是BosonNLP的情感词典,来源于社交媒体文本,所以词典适用于处理社交媒体的情感分析本次分析准备的文本数据有:BosonNLP情感词典停用词表否定词表程度副词表生成停用…

    2022年8月23日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号