在此记录两种定时任务的配置:

一种是quart定时器:

  <1>配置xml文件(定时任务配置)

  <!--定时任务 -->
<bean id="txfwBomc" class="shmc.framework.scheduling.JobDetailFactoryBean">
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="bomcManager"/> <!-- 定时任务所在类 -->
<entry key="targetMethod" value="bomcDataPropel"/> <!-- 定时任务实现方法 -->
</map>
</property>
<property name="concurrent" value="false" />
</bean> <!--定义时间间隔触发器 -->
<bean id="bomcTigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="txfwBomc"/>
<property name="cronExpression" value="0 0 1 * * ?" />
</bean>

  <2>定义实现类的bean

<bean id="bomcManager" class="com.test.service.BomcManager" parent="frameworkManager">
</bean>

  <3>启动定时任务

  <!-- 集群定时器调度工厂  -->
<bean id="clusterSchedule" class="shmc.framework.scheduling.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
<property name="triggers">
<list>
         <!-- BOMC推送 -->
<ref bean="bomcTigger"/>
</list>
</property>
</bean>

  <4>业务层实现代码

  ………………

一种利用注解:

  <1>配置xml文件,需要引用spring-task-3.1.xsd文档。

<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.1.xsd"> //模式文档的位置 <!-- 扫描包路径 -->
<context:component-scan base-package="com.src.xx" >
</context:component-scan>
<!-- 开启定时器-->
<task:annotation-driven/> </beans>

<注意:>上边开启定时任务时,没有设置线程池。而spring默认是单线程,多个任务执行起来时间会有问题(B任务会因为A任务执行起来需要20S而被延后20S执行)

所以要配置线程池如下:

    <!--  开启定时器-->
<task:annotation-driven scheduler="taskScheduler"/>   <!-- 配置定时任务的线程池 -->
<task:scheduler id="taskScheduler" pool-size="5"/>

<2>java代码,具体实现在业务层实现

package com.src.xx.controller.api.timer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import com.src.xx.service.timer.XianshiTimerService; @Component("taskXianshi")
public class XianshiTimer { @Autowired
private XianshiTimerService xianshiTimerService; /**
* 定时任务
*/
@Scheduled(cron = "0 26 10 * * ?")
public void xsActivityRemind(){
xianshiTimerService.xsActivityRemind();
} }

● spring定时任务cronExpression时间:<注意:每个时间点中间必须有空格分隔>
  一个cronExpression的表达式从左到右定义:
  秒(0-59)
  分钟(0-59)
  小时(0-23)
  月份中的日期(1-31)
  月份(1-12或JAN-DEC)
  星期中的日期(1-7或SUN-SAT)
  年份(1970-2099)

spring定时器配置的更多相关文章

  1. 两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz

    1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportT ...

  2. Spring定时器配置与运用,及Cron表达式的详解

    一:首先在spring的配置文件里配置一个定时器 <task:executor id="executor" pool-size="5" /> < ...

  3. spring定时器

    本人小菜鸟一枚,今天在公司看到一段spring定时器配置,自己总结一下! <!-- 定义调用对象和调用对象的方法 --><bean id="jobtask9" c ...

  4. Spring定时器的两种实现方式

    有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz. 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 imp ...

  5. [spring-framework]Spring定时器的配置和使用

    开发中我们常常会做一些定时任务,这些任务有开始时间,并会按一定的周期或规则执行.如此我们在Java程序开发中使用定时器来处理定时任务. <!-- MessageRequestTask类中包含了m ...

  6. spring定时器的配置

    首先,新建一个java项目,下面导入需要的jar包: 这里有你需要的jar包哦. jar包下载 在src文件夹下,新建一个applicationContext.xml文件 <?xml versi ...

  7. spring通过配置xml文件集成quartz定时器

    概述 Spring为创建Quartzde Scheduler.Trigger和JobDetail提供了方便的FactoryBean类,以便能够在Spring容器中享受注入的好处. 此外,Spring还 ...

  8. spring定时器,定时器一次执行两次的问题

    Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...

  9. Spring 定时器Quartz的用法

    Spring定时器Quartz的用法也很简单,需要引入quartz-all-1.5.2.jar java代码如下: package com.coalmine.desktop; import java. ...

随机推荐

  1. JS操作cookie

    JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求.cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由Ja ...

  2. JavaScript:编程改变文本样式

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" Content=&q ...

  3. java 使用map返回多个对象组装

    Object json=JSONObject.fromObject("{}"); List<Object> list = new ArrayList<Object ...

  4. js出错总结

    1 没有</script>  src="js" "./js" "../js"2 dom对象与jquery对象(jquery对象其 ...

  5. vm网络设置

    设置NET模式 cat  /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" BOOTPROTO="stati ...

  6. TestNG测试报告美化buid.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <project name="myproject&qu ...

  7. 奥威软件Speed-BI荣获2016年度中国大数据最佳云平台奖

    (原文转自:http://www.powerbi.com.cn/page110?article_id=210) 2016年12月16日,“科技原力觉醒,引领创新巅峰”—2016创新影响力年会暨国家产业 ...

  8. js暂停的函数

    // numberMillis 毫秒 function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() ...

  9. [Android Tips] 22. Available Java 7 Features in Android

    This only allows Java 7 language features, and you can hardly benefit from anything since a half of ...

  10. C#调用windows API的一些方法

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...