一 使用注解Task

1、在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.mediaforce.news.api.quartz" />
    <task:executor id="executor" pool-size="1" />
    <task:scheduler id="scheduler" pool-size="1" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>

2、在任务类中使用注解

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;  

@Component(“taskJob”)
public class TaskJob {
    @Scheduled(cron = "0 0 3 * * ?")
    public void job1() {
        System.out.println(“任务进行中。。。”);
    }
}  

二、使用xml配置quarz

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

    <!-- 微信抓取 -->
    <bean id="weixinJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="taskJob" />   //目标类
        </property>
        <property name="targetMethod">
            <value>weixin</value>    //目标执行方法
        </property>
    </bean>

    <!-- 微信抓取,1分钟一次 -->
    <bean id="cronWeixinJobDetail" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="weixinJobDetail" />
        </property>
        <property name="cronExpression">
            <value>0 0/1 * * * ?</value>
        </property>
    </bean>

    <!-- 执行定时任务-->
    <bean id="zz" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronWeixinJobDetail" />
            </list>
        </property>
    </bean>

</beans>

spirng定时任务的两种配置:注解和xml的更多相关文章

  1. Hibernate实现有两种配置,xml配置与注释配置

    hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) < ...

  2. hibernate实现有两种配置,xml配置与注释配置。<转>

    <注意:在配置时hibernate的下载的版本一定确保正确,因为不同版本导入的jar包可能不一样,所以会导致出现一些错误> hibernate实现有两种配置,xml配置与注释配置. (1) ...

  3. cron 定时任两种配置方式

    第一种:xml文件方式 <bean id="commonTimer" class="com.course.wx.timer.CommonTimer"> ...

  4. 使用java配置定时任务的几种配置方式及示例

    Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package ...

  5. Linux下实现秒级定时任务的两种方案

    Linux下实现秒级定时任务的两种方案(Crontab 每秒运行): 第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间. while true ;do command s ...

  6. hibernate 一对一 one to one的两种配置方式

    hibernate中one-to-one两种配置方式 标签: hibernateHibernateone-to-one 2013-02-19 17:44 11445人阅读 评论(1) 收藏 举报  分 ...

  7. 浅谈Spring的两种配置容器

    浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41   作者:黄小鱼ZZZ     ...

  8. Hibernate中双向多对多的两种配置方式

    Hibernate中双向多对多的两种配置方式 1.建立多对多双向关联关系 package cn.happy.entitys; import java.util.HashSet; import java ...

  9. flask框架--设置配置文件的几种方式 与Flask两种配置路由的方式

    设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...

随机推荐

  1. Css Study - Top Menu in Header 横向间隔的菜单

    .shortcut ul li { display: inline; } CSS <style> ol, ul { list-style: none; } html, body, ul, ...

  2. Football Foundation (FOFO) TOJ 2556

    The football foundation (FOFO) has been researching on soccer; they created a set of sensors to desc ...

  3. (转) cocos2dx 显示中文

    cocos2dx 中文显示 分类: cocos2dx 2014-01-18 10:31 253人阅读 评论(0) 收藏 举报 目录(?)[+] 总结一句话,就是UTF-8编码. 1. 中文FNT字体文 ...

  4. WPFのInkCanvas作为蒙版透明笔迹不透明

    本人最近利用inkcavas做一个蒙版的功能,结果发现笔迹稀释了,经过一番查找发现:应该讲inkcavas的背景设置为白色,然后透明,而不是将整个控件透明,具体代码: <InkCanvas Na ...

  5. R----data.table包介绍学习

    相比dplyr包,data.table包能够更大程度地提高数据的处理速度,这里就简单介绍一下data.tale包的使用方法. data.table:用于快速处理大数据集的哦 数据的读取 data.ta ...

  6. Oracle的使用

    启动: 1.win+R ---> cmd  -----> sqlplus "/as sysdba"  //以sysdba身份登录(此时可以创建用户,分配权限等) win ...

  7. 。【自学总结 2】------3ds Max 菜单

    一.File〈文件菜单:主要用于打开.存储和打印文件,可以将文件导入和导出为不同的其他三维存档格式.〉 New〈新建〉 Reset〈重置〉 Open〈打开〉 Save〈保存〉 Save As〈保存为〉 ...

  8. HDU 5783 Divide the Sequence(数列划分)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  9. 深入浅出设计模式——工厂方法模式(Factory Method)

    介绍在简单工厂模式中,我们提到,工厂方法模式是简单工厂模式的一个延伸,它属于Gof23中设计模式的创建型设计模式.它解决的仍然是软件设计中与创建对象有关的问题.它可以更好的处理客户的需求变化. 引入我 ...

  10. hdu2243考研路茫茫——单词情结(ac+二分矩阵)

    链接 跟2778差不多,解决了那道题这道也不成问题如果做过基本的矩阵问题. 数比较大,需要用unsigned longlong 就不需要mod了 溢出就相当于取余 #include <iostr ...