| 5128 |
amit.gupta |
1 |
package in.shop2020.catalog.util;
|
|
|
2 |
|
|
|
3 |
import java.util.Calendar;
|
|
|
4 |
import java.util.concurrent.Executors;
|
|
|
5 |
import java.util.concurrent.ScheduledExecutorService;
|
|
|
6 |
import java.util.concurrent.TimeUnit;
|
|
|
7 |
|
|
|
8 |
import javax.servlet.ServletContextEvent;
|
|
|
9 |
import javax.servlet.ServletContextListener;
|
|
|
10 |
|
|
|
11 |
public class ReportSenderScheduler implements ServletContextListener {
|
|
|
12 |
|
|
|
13 |
private ScheduledExecutorService scheduler;
|
|
|
14 |
|
|
|
15 |
@Override
|
|
|
16 |
public void contextDestroyed(ServletContextEvent arg0) {
|
|
|
17 |
scheduler.shutdownNow();
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
@Override
|
|
|
21 |
public void contextInitialized(ServletContextEvent arg0) {
|
|
|
22 |
Calendar calendarNow = Calendar.getInstance();
|
|
|
23 |
Calendar calendarScheduled = Calendar.getInstance();
|
|
|
24 |
int diff = Calendar.MONDAY - calendarNow.get(Calendar.DAY_OF_WEEK);
|
|
|
25 |
if (!(diff > 0)) {
|
|
|
26 |
diff += 7;
|
|
|
27 |
}
|
|
|
28 |
calendarScheduled.add(Calendar.DAY_OF_MONTH, diff);
|
|
|
29 |
calendarScheduled.set(Calendar.HOUR, 6);
|
|
|
30 |
calendarScheduled.set(Calendar.MINUTE, 0);
|
|
|
31 |
calendarScheduled.set(Calendar.AM_PM, Calendar.AM);
|
|
|
32 |
|
|
|
33 |
scheduler = Executors.newSingleThreadScheduledExecutor();
|
|
|
34 |
//Scheduled to run on every Monday 6am.
|
|
|
35 |
scheduler.scheduleAtFixedRate(new ReportSender(), calendarScheduled.getTimeInMillis() - calendarNow.getTimeInMillis(), 7 * 86400*1000,
|
|
|
36 |
TimeUnit.MILLISECONDS);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
}
|