springboot的定时任务

1、在启动类上加@EnableScheduling注解

@SpringBootApplication
@EnableScheduling
public class GunsApplication {

    private final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(GunsApplication.class, args);
        logger.info("GunsApplication is success!");
    }
}


2、在需要定时执行的类的方法上添加@Scheduled(fixedRate = 3001)注解参数还可以是cron表达式

@Service
public class GameServiceImpl extends ServiceImpl<GameMapper, Game> implements IGameService {

    /****
     * 保存每期开奖结果
     * @param url
     * @param game
     * @return
     * @throws Exception
     */
    @Scheduled(fixedRate = 3001)
    @Override
    public Integer insertGameResult() throws Exception { // 方法上不能有参数
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页