您的位置 首页 java

java 定时器

定时器

有时候需要进行定时计算数据,如每周定时计算佣金,每月计算奖励;每天计算务数据等;可以使用java定时器也可以使用xxlJob定时器。xxlJob管理端使用起来更灵活,但使用起来复杂;

每天执行:cron = “0 10 * * * ?”

每隔几分钟执行:cron = “0 0/10 * * * ?”

 @Scheduled(cron = "0 10 * * * ?")
public  void  calculateServiceData() {
    String buyerAmount = redisTemplate.opsForValue().get(MachineConstant.MACHINE_BUYER_AMOUNT);
    String buyerMoney = redisTemplate.opsForValue().get(MachineConstant.MACHINE_BUYER_MONEY);
    if (buyerAmount != null) {
         redis Template.opsForValue().set(MachineConstant.MACHINE_BUYER_AMOUNT, String.valueOf(RandomUtil.randomInt(1000,3000)+Long.parseLong(buyerAmount)));
    }else {
        redisTemplate.opsForValue().set(MachineConstant.MACHINE_BUYER_AMOUNT,MachineConstant.BUYER_AMOUNT_INIT.toString());
    }
    if (buyerMoney != null) {
        redisTemplate.opsForValue().set(MachineConstant.MACHINE_BUYER_MONEY,String.valueOf(RandomUtil.randomInt(500000,1000000)+Long.parseLong(buyerMoney)));
    }else {
        redisTemplate.opsForValue().set(MachineConstant.MACHINE_BUYER_MONEY,MachineConstant.BUYER_MONEY_INIT.toString());
    }
}  

注意:方法需要为public,因为底层使用的是动态代理。还需要在启动类上进行开启。

文章来源:智云一二三科技

文章标题:java 定时器

文章地址:https://www.zhihuclub.com/176300.shtml

关于作者: 智云科技

热门文章

网站地图