定时器
有时候需要进行定时计算数据,如每周定时计算佣金,每月计算奖励;每天计算务数据等;可以使用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,因为底层使用的是动态代理。还需要在启动类上进行开启。