我正在使用这个 cron 表达式 “0 0 9 * * ?” 来在每天上午9点安排任务,

huangapple go评论73阅读模式
英文:

I am using this cron "0 0 9 * * ?" expression for scheduling task at morning 9am every day,

问题

但问题是任务会触发两次一次在早上9点另一次在下午2点30分以下是我的代码

@Service
public class NotificationScheduler {

    @Autowired
    private UsersService userService;
    
    @Autowired
    private SendSMS sendSms;
    
    @Scheduled(cron = "0 0 9 * * ?")
    public void sendSmsNotifictaion() {
        DecimalFormat deciFormat = new DecimalFormat(); 
        deciFormat.setMaximumFractionDigits(4);
        List<Users> userList=userService.getAllUserByIsNotSuAcc();
        userList.forEach(obj -> {
            String updatemsg="UPDATE:+Commission+Wallet+balance+for+"+obj.getName()+"+(AGENT+ID+"+obj.getId()+")+on+"+BaseDateUtil.getDateYYYYMMDD(new Date())+"+is+Rs."+deciFormat.format(BaseUtil.getDouble(obj.getBalance()))+".+Log+in+to+Spark+OMOB+to+view+and+transfer+balance.+Thank+you+for+your+trust+and+growth+with+us.+-+Spark+Team";
            sendSms.sendSMS(updatemsg, obj.getAgentmobil(), "");
        });
    }
}
英文:

but problem is that task is triggering two times, one at morning 9am and second one at 2:30 afternoon, below is my code

@Service
public class NotificationScheduler {

@Autowired
private UsersService userService;

@Autowired
private SendSMS sendSms;

@Scheduled(cron = &quot;0 0 9 * * ?&quot;)
public void sendSmsNotifictaion() {
	DecimalFormat deciFormat = new DecimalFormat(); 
	deciFormat.setMaximumFractionDigits(4);
	List&lt;Users&gt; userList=userService.getAllUserByIsNotSuAcc();
	userList.forEach(obj -&gt; {
		String updatemsg=&quot;UPDATE:+Commission+Wallet+balance+for+&quot;+obj.getName()+&quot;+(AGENT+ID+&quot;+obj.getId()+&quot;)+on+&quot;+BaseDateUtil.getDateYYYYMMDD(new Date())+&quot;+is+Rs.+&quot;+deciFormat.format(BaseUtil.getDouble(obj.getBalance()))+&quot;.+Log+in+to+Spark+OMOB+to+view+and+transfer+balance.+Thank+you+for+your+trust+and+growth+with+us.+-+Spark+Team&quot;;
		sendSms.sendSMS(updatemsg, obj.getAgentmobil(), &quot;&quot;);
	});
}

}

答案1

得分: 2

你的定时任务应该是这样的 0 9 * * *,详细解释请参考 cronguru

英文:

Your cron should be this 0 9 * * * see cronguru for explanations

huangapple
  • 本文由 发表于 2020年8月20日 18:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定