-1

I m using @Scheduled in spring which will work fine if i want to execute task on evey 5 Second using @Scheduled(fixedRate = 5000) But I want to execute Job on 8:00 AM and 3:10 Pm Every Day which is not working. Following is My code.

@Component
public class FinanceJob  {

    @Autowired
    AdminService adminService;

    @Autowired
    AdminDao dao;

    @Autowired
    CommonService cservice;


    //@Scheduled(fixedRate = 5000)

    // @Scheduled(cron = "0/20 * * * * ?")
    @Scheduled(cron="0 48 2 * * *",zone = "Indian/Maldives")
    public void sajan() {
        List<SystemParameter> paramList=cservice.getSysParam();

        for(SystemParameter param :paramList)
        {
            if(param.getUid()==263 && param.getIsactive()==1)
            {
                System.out.println("Hello Sahjan");
            }
        }

    }

}
Rahul
  • 131
  • 3
  • 10

1 Answers1

0

AFAIK, you cannot schedule job by two cron schedule.

What you can do is wrap your task and expose schedule by another function:

@Scheduled(cron="0 0 8 */1 * *",zone = "Indian/Maldives")
public void schedule1() {
   sajan();
}

@Scheduled(cron="0 10 15 */1 * *",zone = "Indian/Maldives")
public void schedule2() {
   sajan();
}

public void sajan() {

    // yOur task
}
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • @Scheduled(cron="0 0 5 ? * * *",zone = "Indian/Maldives") public void schedule4() { sajan(); } this will not work. set schedule at 5 o'clock – Rahul May 28 '18 at 11:32
  • `0 0 5 */1 * * *` is the cron for 5AM daily, `0 0 17 */1 * * *` is the cron for 5PM daily – Mạnh Quyết Nguyễn May 28 '18 at 11:44
  • Thanks.can you please share reference that how to generate cron online.any web site? – Rahul May 28 '18 at 11:52
  • You can refer to this [answer](https://stackoverflow.com/questions/26147044/spring-cron-expression-for-every-day-101am) – Mạnh Quyết Nguyễn May 28 '18 at 11:55
  • It's shows error that i will require only 6 parameters and in your code there are 7 parameters.//9 AM daily //@Scheduled(cron="0 0 9 * * ?",zone = "Indian/Maldives") public void schedule4() { System.out.println("SendFinanceEmail Scheduler is called..."); SendFinanceEmailController(); } Above Code is Not Working. I got reference from https://www.freeformatter.com/cron-expression-generator-quartz.html#cronexpressionexamples – Rahul May 31 '18 at 06:38