• 212-如何实现定时器扫描?


    //@EnableScheduling注意这个定时任务是SpringBoot的注解,只要我们导入SpringBoot的核心依赖,也就是继承web的依赖就可以使用了。
    //这个注解我们可以写在启动了上,也可以写在当前类上。我推荐写下启动类上,我们的p2p老师就是写在启动类上的,但是我们的最终阶段老师是写在当前类上
    @EnableScheduling
    @Component
    public class SecKillTimer { @Resource private RedisTemplate redisTemplate; private StringRedisSerializer stringRedisSerializer=new StringRedisSerializer(); @Resource private GoodsService goodsService; /** * 定时任务,每5秒钟执行一次这个方法 * 注意:实际工作时不是每5秒钟执行一次,应该商品即将开始活动时执行定时任务或每天的固定时间执行定时任务 * 将当前需要参与活动的商品写入Redis中例如每天的23:55分将未来一天所有活动商品写入Redis中 * 我们每5秒执行一次仅仅为了方便测试 */ @Scheduled(cron = "0/5 * * * * *") public void initSecKillDataToRedis(){ redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(stringRedisSerializer); System.out.println("--------------------------"); ReturnObject<List<Goods>> returnObject=goodsService.getGoodsList(); List<Goods>goodsList=returnObject.getResult(); for(Goods goods:goodsList){ //如果key在Redis中存在则放弃写入数据,如果key不存在则将数据写入到Redis中 //使用统一的Key前缀+商品随机名作为key, //使用商品库存作为value将数据写入Redis中 redisTemplate.opsForValue().setIfAbsent(Constants.GOODS_STORE+goods.getRandomName(),goods.getStore()+""); } } }
  • 相关阅读:
    NSURLConnection和Runloop(面试)
    文件的上传
    CentOS 7防火墙快速开放端口配置方法
    国内常用源镜像地址:
    yum安装zabbix-web-mysql出现[Errno 256] No more mirrors to try.
    1251-Client does not support authentication protocol requested by server; consider upgrading MySQL client。
    http代理和SOCKS代理的区别
    windows下redis安装
    centeros7安装mysql
    nginx配置负载均衡分发服务器笔记
  • 原文地址:https://www.cnblogs.com/pogusanqian/p/13035136.html
Copyright © 2020-2023  润新知