龙空技术网

java 定时器

北漂Leo 88

前言:

如今各位老铁们对“java中定时器”大致比较关切,小伙伴们都需要剖析一些“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) {        redisTemplate.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中定时器