2

We have users from all over the world using application. Our requirement is to run cron job at 12 am daily at users time zone and only for users in that time zone.

How can this be achieved using spring boot??

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
  • Do you have any information stored about the users timezones, or any information that you can derive into the timezones? Such as country, coordinates or similar. – Johan Oct 07 '18 at 12:21
  • Yes we are maintaining time zone of users in database – rajadilipkolli Oct 07 '18 at 12:21
  • 2
    Then you need to run a Cron job every hour and apply the job only for the users that match the criteria for the timezone. If you want to make it simple I assume you could make a script that checks which timezone equals to 12am at the given moment and then select those users with the corresponding timezone. – Johan Oct 07 '18 at 12:25
  • We have written this to run every 15min to cover all timezones. I am looking if there is some solution which simplies . – rajadilipkolli Oct 07 '18 at 12:34
  • Can you define what's "simpler"? What are the steps in the current flow that you want eliminated or redefined? – Johan Oct 07 '18 at 12:50
  • 1
    With current approach we are running 96 times same job. I want to eliminate this – rajadilipkolli Oct 08 '18 at 18:31
  • I see. You should able to [eliminate it down to 38](https://www.timeanddate.com/time/current-number-time-zones.html) but it would likely be at the cost of readability. You would need to custom make a Cron job for every currently existing timezone. – Johan Oct 08 '18 at 21:02

2 Answers2

1

In @Scheduled notation you can pass Zone but only single time zone so you need to manage it with custom logic.

I believe your user record has included with time zone so to order to achieve it, You will need to set the cron job to run every half an hour(cover all time zones to include + 1/2 an hour), Get all timezones which has midnight then get users in those time zones to run logic for them..

kj007
  • 6,073
  • 4
  • 29
  • 47
1

You have to convert all the cron job times (12 am) in the user's time zones, to the equivalent times on your server, by adding or subtracting hours to compensate for the time difference. With the server time zone equivalent of the cron job times, you can schedule all the cron jobs.

Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29