Springboot +webscoket+定时器,定时器不执行

问题描述

需要定时通过websocket接口来推送mysql里面最新的数据,自定义了定时器

@Component
@Slf4j
public class  TaskScheduler {

    @Autowired
    private TparkOrderInOutMapper tparkOrderInOutMapper;

    @Autowired
    UserController userController;
    /**
     * 间隔是10秒执行一次
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void pushParkInfo() {
        userController.findAll();
    }
}

定时器配置

在启动类里面增加定时器的启动入口。

@SpringBootApplication
@MapperScan(basePackages = "com.stop.mapper") //扫描mapper包
@EnableScheduling //配置定时器
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class,args);
       System.out.println("hello world");
       System.out.println("test");
   }
}

其中,注解@EnableScheduling 就是配置定时器。

启动作业

启动作业发现定时器的任务没有执行。查阅资料是因为:
springBoot 默认是使用单线程的Scheduler来处理我们的 @Scheduled注解的定时任务。
我们需要定义一个TaskScheduler的配置类,使用多线程来执行我们的定时任务。

@Configuration
public class ScheduledTaskConfiguration implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(2);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}

最后运行application的时候,我们可以看到控制上:
在这里插入图片描述我们可以看到上面定时任务按照间隔10秒在执行操作。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值