开发者

如何解决SpringBoot定时任务报错Unexpected error occurred in scheduled task问题

目录
  • 问题
    • 主要有两个原因
    • 以下是我的代码
  • 解决方法
    • 总结

      问题

      spring boot项目在线上一直正常运行没有错误,然后今天发生了报错,如图

      如何解决SpringBoot定时任务报错Unexpected error occurred in scheduled task问题

      这是一个定时器错误,发生这个报错

      主要有两个原因

      • 定时器编写的有错误
      • @Scheduled注解方式级别高于资源注入级别,导致了资源注入失败

      以下是我的代码

      @RestController
      @Slf4j
      @SuppressWarnings({"all"})
      @CrossOrigin
      @RequestMapping("/journal")
      @Component
      public class JournalController {
      
      	@Autowired
          Journal_timeMapper journal_timeMapper;
          
             /**
           * 每周日将签到时间数据清空
           */
          @Scheduled(cron = "0 0 0 * * SUN")
          public void journaltimeout() {
              List<Journal_time> journal_times = journal_timeMapper.selectList(null);
              for (Journal_time journal_time : journal_times) {
                  journal_time.setDay_time("0小时0分钟");
                  journal_time.setWeek_time("0小时0分钟");
                  journal_time.setAll_time("0小时0分钟");
                  journal_time.setWeek_time_desc("第0名");
                  journal_timeMapper.updateById(journal_time);
              }
          }
      
      
          /**
           * 每天晚上将一天签到数据清空
           */
          @Scheduled(cron = "0 0 0 * * ?")php
          public void daytimeout() {
              List<Journal_time> journal_times = journal_timandroideMapper.selectList(null);
              for (Journal_time journal_time : journal_times) {
                  journal_time.setDay_time("0小时0分钟");
                  journal_timeMapper.updateById(journal_time);
              }
          }
      }

      有一个特别奇怪的点,就是我的daytimeout方法一直在生效,但是journaltimeout却报错了,

      这都是在一个Controller下面(我直接晕厥)–目前还没有找到原因,先说解决方法

      解决方法

      使用ApplicationContextAware,它实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来

      修改后的代码

      @RestController
      @Slf4j
      @SuppressWarnings({"all"})
      @CrossOrigin
      @RequestMapping("/journal")
      @Component
      public class JournalController implements ApplicatjsionContextAware {
      
        	private static ApplicationContext context;
      
        	@Override
        	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException 	{
              context = applicationContext;
         	}
          
          public static ApplicationContext getApplicationContext(php) {
              return context;
          }
      
          public static Object getBean(String name) {
              return getApplicationContext().getBean(name);
          }
      
          
          
             /**
           * 每周日将签到时间数据清空
           */
          @Scheduled(cron = "0 0 0 * * SUN")
          public void journaltimeout() {
              Journal_timeMapper journal_timeMapper = (Journal_timeMapper)this.getBean("journal_timeMapper");
              List<Journal_time> journal_times = journal_timeMapper.selectList(null);
              for (Journal_time journal_time : journal_times) {
                  journal_time.setDay_time("0小时0分钟");
                  journal_time.setWeek_time("0小时0分钟");
                  journal_time.setAll_time("0小时0分钟");
                  journal_time.setWeek_time_desc("第0名");
                  journal_timeMapper.updateById(journal_time);
              }
          }
      
      
          /**
           * 每天编程晚上将一天签到数据清空
           */
          @Scheduled(cron = "0 0 0 * * ?")
          public void daytimeout() {
              Journal_timeMapper journal_timeMapper = (Journal_timeMapper)this.getBean("journal_timeMapper");
              List<Journal_time> journal_times = journal_timeMapper.selectList(null);
              for (Journal_time journal_time : journal_times) {
                  journal_time.setDay_time("0小时0分钟");
                  journal_timeMapper.updateById(journal_time);
              }
          }
      }

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

      0

      上一篇:

      下一篇:

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      最新开发

      开发排行榜