springboot 定时任务问题

1.springboot 定时执行某个方法

2.springboot 通过接口生成 某个调度时间 定时执行

  方法1: 网上好多案例 不做详细阐述

  方法2:业务场景:用户配置一个活动,然后根据配置活动的时间定时推送消息。

    1.配置一个每天凌晨的定时任务,去数据库查询数据,将符合条件的 cron表达式放入定时任务中,依次执行,同时将结束时间是昨天的定时任务删除。

    2.如果新添加定时任务,需要在当天执行定时任务,需要手动调用定时任务启动方法,添加定时任务。

 

直接上代码:

 

@Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Autowired
    RoomMembersService roomMembersService;

    @Autowired
    MongoTemplate mongoTemplate;

    private Map<String, ScheduledFuture<?>> taskMap = new HashMap<>();

    private ScheduledFuture<?> future;

    @Scheduled(cron = "0 0 0 * * ?")
    public void syncWxOnlineUsersAndGroupsMsg() {
        log.info("-------------> 凌晨调度开始");
        //获取所有符合条件的cron表达式,添加调度
        List<GroupSignIn> signInCronList = roomMembersService.getSignInCronList();
        log.info("-------------> "+signInCronList.size());
        for (GroupSignIn groupSignIn : signInCronList){
            String cron = groupSignIn.getReminder_cron();
            String sign_id = groupSignIn.get_id().toString();
            String material_content = groupSignIn.getMaterial_content();
            startCron(cron,sign_id,material_content);
        }
        //获取所有昨天结束的调度,删除任务
        List<GroupSignIn> executeOverList = roomMembersService.executeOver();
        if (null != executeOverList && executeOverList.size() >0){
            for (GroupSignIn groupSignIn : executeOverList){
                String sign_id = groupSignIn.get_id().toString();
                log.info("删除调度任务",sign_id);
                stop(sign_id);
            }
        }
        log.info("-------------> 凌晨调度结束");
    }


    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

    public void startCron(String cron,String sign_id,String material_content) {
        log.info("-------------> 开始添加定时任务");
        String createTime = TimeUtils.getCurrentTimeInString(TimeUtils.DATETIME_FORMAT_DATE);
        ScheduledFuture<?> future = threadPoolTaskScheduler.schedule(new MyRunnable(cron,sign_id,material_content), new CronTrigger(cron));
//        stop(sign_id);
        taskMap.put(sign_id,future);
        SignInTask signInTask = new SignInTask();
        signInTask.setMaterial_content(material_content);
        signInTask.setSign_id(sign_id);
        signInTask.setReminder_cron(cron);
        signInTask.setCreated_date(createTime);
        mongoTemplate.insert(signInTask);
    }
    private class MyRunnable implements Runnable {
        private  String cron ;
        private  String sign_id ;
        private  String material_content ;
        public  MyRunnable(String cron,String sign_id,String material_content){
            this.cron = cron;
            this.sign_id = sign_id;
            this.material_content = material_content;
        }

        @Override
        public void run() {
            roomMembersService.sendSignMsg(sign_id,material_content);
        }
    }


    public boolean stop(String name) {
        log.info("-------------> 删除调度开始"+name);
        if(null == taskMap.get(name)) {
            return false;
        }
        ScheduledFuture<?> scheduledFuture = taskMap.get(name);
        scheduledFuture.cancel(true);
        taskMap.remove(name);
        return true;
    }

 

  因为本人对 多线程 了解的少,所以还需要优化 如何异步跑调度。

对自己---》需要看定时任务源码,详细理解为什么 这样做,如何优化

 

posted @ 2020-07-06 17:27  牧の风  阅读(271)  评论(0编辑  收藏  举报