zoukankan      html  css  js  c++  java
  • Quartz+spring

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
       <!-- 注册一个普通bean -->
       <bean id="quartzTask" class="cn.itcast.nsfw.complain.QuartzTask"></bean>
       
       <!-- 1、制定任务信息信息 -->
       <bean id="jobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                  <!-- 设置执行对象 -->
                  <property name="targetObject" ref="quartzTask"></property>
                  <!-- 设置执行对象中对应的执行方法 -->
                  <property name="targetMethod" value="doSimpleTriggerTask"></property>
                  <!-- 是否可以同步执行;不可同步执行 -->
                  <property name="concurrent" value="false"></property>
       </bean>
       <bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                  <!-- 设置执行对象 -->
                  <property name="targetObject" ref="quartzTask"></property>
                  <!-- 设置执行对象中对应的执行方法 -->
                  <property name="targetMethod" value="doCronTriggerTask"></property>
                  <!-- 是否可以同步执行;不可同步执行 -->
                  <property name="concurrent" value="false"></property>
       </bean>
       
       <!-- 2、制定任务执行时机(任务执行触发器) -->
       <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
               <!-- 设置任务详细 -->
               <property name="jobDetail" ref="jobDetail1"></property>
               <!-- 设置任务延迟执行时间 ;延迟2秒执行-->
               <property name="startDelay" value="2000"></property>
               <!-- 设置任务执行频率;执行频率为每4秒执行一下 -->
               <property name="repeatInterval" value="2000"></property>
       </bean>
       <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <!-- 设置任务详细 -->
               <property name="jobDetail" ref="jobDetail2"></property>
               <!-- 设置任务执行时机,cron表达式 -->
               <property name="cronExpression" value="* * * 18c * ?"></property>
       </bean>
       
       <!-- 3、设置调度工厂 -->
       <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
               <property name="triggers">
                   <list>
                       <!-- <ref bean="simplerTrigger"/> -->
                       <ref bean="cronTrigger"/>
                   </list>
               </property>
       </bean>
    </beans>

    package cn.itcast.nsfw.complain;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class QuartzTask {

        public void doSimpleTriggerTask() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("doing simpleTrigger task..." + sdf.format(new Date()));
        }

        public void doCronTriggerTask() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("doing cronTrigger task..." + sdf.format(new Date()));
        }
        
    }

     使用quartz-1.8.6.jar测试通过

  • 相关阅读:
    MyBatis参数传入集合之foreach动态sql
    【mybatis】多次查询缓存的问题
    svn is already locked解决方案
    Python 命令行之旅:使用 docopt 实现 git 命令
    Django 官方推荐的姿势:类视图
    这可能就是你苦苦寻找免费、高颜值、功能强大的 Markdown 编辑器(共5款)
    超级好用的 Java 数据可视化库:Tablesaw
    让你如“老”绅士般编写 Python 命令行工具的开源项目:docopt
    开发 Django 博客文章阅读量统计功能
    使用 Docker 让部署 Django 项目更加轻松
  • 原文地址:https://www.cnblogs.com/god-monk/p/6519196.html
Copyright © 2011-2022 走看看