How to schedule a task to send monthly emails to different timezones?

Issue

I want to write a scheduler in spring which should be triggered at the first day of each month and send emails to different users across the world. Can someone help me through this?
The project I have is a spring boot project with mongoDB as the database. I have a collection called machine_status. It stores all the status of each machine that are registered under our project. All the datas there are stored in UTC time. The requirement is, at the first day of each month. Find all the machines that were ONLINE in the previous month from the collection machine_status and send those machine details to all admin users and also store that details in a new collection for future reference. So that we don’t have to calculate it again. The concern here is our admins falls in different zones of the world. How can I schedule a task that will meet all this.
For example for a user in India the timeframe for month January is different from that for a user in america. We have to get the proper start and end time of a month in milliseconds, then only we will be able to query the collection machine_status to retrieve the data of that particular month.

Solution

You can use @Scheduled annotation multiple times on the function you want to schedule.

For example, if you want to run the same job on the first day Of the month for two time zones, you can use the below code

   @Schedule(dayOfMonth="first", zone = "IST")
   @Schedule(dayOfMonth="first", zone = "UTC")
   public void sendEmail() { ... }

If the number of zones is more, then you can use a for loop to create the CronTriggers.

Answered By – Deepak Patankar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published