当前位置:网站首页>Quartz (timer)

Quartz (timer)

2022-06-27 09:33:00 Sparkle_ wsl

Quartz( Timer )

Use steps

  1. Guide pack

     <!-- Clean up regularly jar package -->
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
            </dependency>
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz-jobs</artifactId>
            </dependency>
    
  2. Customize a Job( Scheduled tasks )

    public class ClearImg {
          
        @Autowired
        private JedisPool jedisPool;
    
        public  void clearImg(){
          
            System.out.println(jedisPool);
            // Put all the contents 
            Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            if(set!=null){
          
                // Call the delete method 
                for (String s : set) {
          
                    QiniuUtils.deleteFileFromQiniu(s);
                    // eliminate redis Junk pictures in 
                    jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
                }
            }
        }
    }
    
    
  3. Accessories Spring-jobs Configuration file for

    1. Will make it up to you job Configuration to Spring in

      <!--  Register custom Job -->
          <bean id="jobDemo" class="com.itheima.jobs.ClearImg"></bean>
      
    2. register JobDetail, The action is responsible for invoking the specified through reflection job

      <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
              <!--  Inject the target object  -->
              <property name="targetObject" ref="jobDemo"/>
              <!--  Inject target method  -->
              <property name="targetMethod" value="clearImg"/>
      </bean>
      
    3. Register a trigger , Specify when the task is triggered

      <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
              <!--  Inject JobDetail  That is, who will execute  -->
              <property name="jobDetail" ref="jobDetail"/>
              <!--  Specify the trigger time , be based on Cron expression   Specify how often  -->
              <property name="cronExpression">
                  <value>0 0 0/1 * * ? </value>
              </property>
          </bean>
      
    4. Register a unified dispatching factory , Through this scheduling factory, scheduling tasks

      <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
              <!--  Inject multiple triggers  -->
           <property name="triggers">
               <list>
               <!--  trigger 1 -->
               <ref bean="myTrigger"/>
               <!--  trigger 2 -->
               <res bean="xXX">
      		 <!-- .... -->
               </list>
           </property>
      </bean>
      

Cron expression

  • Altogether 7 position , What is often used is 6 position

  • From right to right : second branch when Japan Zhou year

  • Online generation Cron expression

    https://cron.qqe2.com/

原网站

版权声明
本文为[Sparkle_ wsl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270924082292.html