当前位置 : 主页 > 编程语言 > java >

quertz定时任务

来源:互联网 收集:自由互联 发布时间:2021-06-28
quertz.xml selectResettingRefundsMoney updateResettingRefundsMoney 0 0 0 1 * ? 0 2 0/6 * * ? 实体类 package org.woke.oss.manager.Job;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import or
quertz.xml
 

 
	
  
	
  



	
  
	
   
    
     
    
    
    
     selectResettingRefundsMoney
     
    
    
  
	
	
   
    
     
    
    
    
     updateResettingRefundsMoney
     
    
    
  
	
	
  

	
  
	
   
    
     
    
    
     
    
     0 0 0 1 * ?
     
    
  
	
	
   
    
     
    
    
     
    
     0 2 0/6 * * ?
     
    
  
	
  
	
  
	
  
	
  
	
   
    
     
      
      
      
     
    
    
    
    
  
	
  

 
实体类
package org.woke.oss.manager.Job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.woke.oss.common.exception.AdminCoreException;
import org.woke.oss.manager.ServiceProviderDataManager;

/**
 * @Author:LiuHao
 * @Date: Create in 18:22 2017/10/25
 * 定时改变服务商退款额度
 **/
@Slf4j
public class RefundsJob {
    @Autowired
    ServiceProviderDataManager serviceProviderDataManager;
    /**
     *@Author:LiuHao
     *@Date:15:18 2017/10/25
     *每个月的第一天的00:00::00的时候查询一次 将所有服务商的退款额度重置为押金数
     **/
    public void selectResettingRefundsMoney(){
        try {
            serviceProviderDataManager.selectResettingRefundsMoneyManager();
            log.info("每月开始的时间 重置退款额度成功");
        } catch (AdminCoreException e) {
            log.info("每月开始的时间 重置退款额度失败");
            e.printStackTrace();
        }
    }
    /**
     *@Author:LiuHao
     *@Date:16:36 2017/10/25
     *每过6个小时 更新一次退款额度
     **/
    public void updateResettingRefundsMoney(){
        try {
            serviceProviderDataManager.updateResettingRefundsMoneyManager();
            log.info("每6个小时重置退款额度成功");
        } catch (AdminCoreException e) {
            log.error("每6个小时重置退款额度失败");
            e.printStackTrace();
        }
    }

}
推送调度器
package org.woke.oss.manager.Job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.woke.oss.common.emuns.AdminCoreCodeEmun;
import org.woke.oss.common.exception.AdminCoreException;
import org.woke.oss.common.test.MyJob1;
import org.woke.oss.dao.models.Push;

import java.text.ParseException;

/**
 * @Author:LiuHao
 * @Date: Create in 10:11 2017/10/26
 * 推送调度器
 **/
@Component
public class PushScheduler {
    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;
    private static SchedulerFactory schedulerfactory = new StdSchedulerFactory();
    private static String groupName="triggerGroup";
    private static String jobGroupName="jgroup1";
    public  String pushScheduler(String pushId,String time){
        //通过schedulerFactory获取一个调度器
        Scheduler scheduler=null;
        try{
            try {
//      通过schedulerFactory获取一个调度器
                scheduler=schedulerFactoryBean.getScheduler();
//       创建jobDetail实例,绑定Job实现类
//       指明job的名称,所在组的名称,以及绑定job类

                JobDetail job= JobBuilder.newJob(PushJob.class)
                        .withIdentity(pushId, jobGroupName)
                        .usingJobData("pushId",pushId)
                        .build();

//      使用cornTrigger规则 定时发送
                Trigger trigger=TriggerBuilder.newTrigger().withIdentity(pushId+"Trigger", groupName)
                        .withSchedule(CronScheduleBuilder.cronSchedule(time))
                        .startNow().build();

//       把作业和触发器注册到任务调度中
                scheduler.scheduleJob(job, trigger);
//       启动调度
                scheduler.start();
            } catch (SchedulerException e) {
                e.printStackTrace();
                throw new AdminCoreException(AdminCoreCodeEmun.PUSH_SCHEDULER_FAIL);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 从Scheduler 移除当前的Job
     * @param
     * @throws SchedulerException
     * @throws ParseException
     */
    public static void modifyJobTime(String jobName)
            throws SchedulerException, ParseException {
        Scheduler sched = schedulerfactory.getScheduler();
        sched.deleteJob(JobKey.jobKey(jobName,jobGroupName));
    }



}
//    /**
//     * 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
//     * @param jobName
//     */
//    public static void removeJob(String jobName) {
//        try {
//            Scheduler sched = gSchedulerFactory.getScheduler();
//            sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
//            sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
//            sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
推送任务
package org.woke.oss.manager.Job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.woke.oss.common.exception.AdminCoreException;
import org.woke.oss.dao.mapper.PushMapper;
import org.woke.oss.dao.models.Push;
import org.woke.oss.manager.PushManager;

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

/**
 * @Author:LiuHao
 * @Date: Create in 10:07 2017/10/26
 * 推送任务
 **/
@Slf4j
@Component
public class PushJob implements Job {
    @Autowired
    PushManager pushManager;
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Map map=jobExecutionContext.getMergedJobDataMap();
        System.out.println(map.get("pushId"));
        System.out.println("执行了定时器");
        try {
            System.out.println(1);
            pushManager.sendOutPush(map.get("pushId")+"");
        } catch (AdminCoreException e) {
            e.printStackTrace();
            String now=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            log.error("发送推送失败了,时间为{}",now);
        }
    }
}
pom.xml
 
      
  
   org.quartz-scheduler
  
      
  
   quartz
  
      
  
   2.2.3
  
    
 
JOB工厂类
package org.woke.oss.manager.Job;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;

/**
 * @Author:LiuHao
 * @Date: Create in 15:14 2017/11/17
 **/
public class MyJobFactory extends AdaptableJobFactory {

    //这个对象Spring会帮我们自动注入进来,也属于Spring技术范畴.
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        //调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
        //进行注入,这属于Spring的技术,不清楚的可以查看Spring的API.
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
时间转corn表达式
package org.woke.oss.common.utils;

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

/**
 * @Author:LiuHao
 * @Date: Create in 16:35 2017/11/9
 **/
public class quartzTime {

    public static String getQuartzTime(Date time){
        String string="";
        SimpleDateFormat sj=new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        string=sj.format(time);
        return string;
    }

    public static void main(String[] args) {

    	    System.out.println( quartzTime.getQuartzTime(new Date()));
    }
}
网友评论