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测试通过

  • 相关阅读:
    BZOJ 1008: [HNOI2008]越狱
    BZOJ 1007: [HNOI2008]水平可见直线
    BZOJ 1005: [HNOI2008]明明的烦恼
    【LibreOJ10121】与众不同(RMQ,二分)
    【NOIP模拟】数字对(RMQ,二分)
    【POJ3264】Balanced Lineup(RMQ)
    【JZOJ4857】Tourist Attractions(Bitset)
    【BZOJ2330】糖果(差分约束系统,强连通分量,拓扑排序)
    C++对拍数据生成
    C++字符串读入
  • 原文地址:https://www.cnblogs.com/god-monk/p/6519196.html
Copyright © 2011-2022 走看看