1
|
package eu.dnetlib.data.emailSender;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.springframework.beans.factory.annotation.Autowired;
|
5
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
6
|
|
7
|
import javax.servlet.ServletContextEvent;
|
8
|
import javax.servlet.ServletContextListener;
|
9
|
import java.util.Calendar;
|
10
|
import java.util.concurrent.Executors;
|
11
|
import java.util.concurrent.ScheduledExecutorService;
|
12
|
import java.util.concurrent.ScheduledFuture;
|
13
|
|
14
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
15
|
|
16
|
/**
|
17
|
* Created by kiatrop on 10/5/2017.
|
18
|
*/
|
19
|
|
20
|
|
21
|
public class EmailScheduler implements ServletContextListener {
|
22
|
|
23
|
private int delay = 0;
|
24
|
private final Logger logger = Logger.getLogger(EmailScheduler.class);
|
25
|
|
26
|
private static boolean sendEmailNotifications;
|
27
|
|
28
|
@Autowired
|
29
|
private String beautySleep;
|
30
|
|
31
|
@Autowired
|
32
|
private String targetHour;
|
33
|
@Autowired
|
34
|
private String targetMinute;
|
35
|
@Autowired
|
36
|
private String targetSecond;
|
37
|
@Autowired
|
38
|
private EmailSender emailSender;
|
39
|
|
40
|
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
41
|
|
42
|
@Override
|
43
|
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
44
|
WebApplicationContextUtils
|
45
|
.getRequiredWebApplicationContext(servletContextEvent.getServletContext())
|
46
|
.getAutowireCapableBeanFactory()
|
47
|
.autowireBean(this);
|
48
|
logger.debug("Initializing EmailScheduler with beautySleep " + beautySleep + " and begin time " + targetHour + ":" + targetMinute + ":" + targetSecond + " and email sender " + emailSender);
|
49
|
if(sendEmailNotifications) {
|
50
|
logger.debug("Initializing EmailScheduler with beautySleep " + beautySleep + " and begin time " + targetHour + ":" + targetMinute + ":" + targetSecond + " and email sender " + emailSender);
|
51
|
delay = getInitialDelaySeconds(Integer.parseInt(targetHour), Integer.parseInt(targetMinute), Integer.parseInt(targetSecond));
|
52
|
logger.debug("EmailScheduler will wait " + delay + " seconds");
|
53
|
logger.debug("Send mail to community managers: " +this.emailSender.isNotifyCommunityManagers());
|
54
|
logger.debug("Send mail to project managers: " +this.emailSender.isNotifyProjectManagers());
|
55
|
final ScheduledFuture<?> resetHandle = scheduler.scheduleAtFixedRate(emailSender, delay, Integer.parseInt(beautySleep), SECONDS);
|
56
|
} else {
|
57
|
logger.debug("Shutting down EmailScheduler for claim notifications");
|
58
|
scheduler.shutdown();
|
59
|
}
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public void contextDestroyed(ServletContextEvent servletContextEvent) {
|
64
|
if(!scheduler.isShutdown()) {
|
65
|
logger.info("Shutting down EmailScheduler.");
|
66
|
scheduler.shutdown();
|
67
|
} else {
|
68
|
logger.info("EmailScheduler is already shutdown");
|
69
|
}
|
70
|
}
|
71
|
|
72
|
private int getInitialDelaySeconds(int targetHour, int targetMinute, int targetSecond) {
|
73
|
int delay = 0; // delay in seconds
|
74
|
|
75
|
// calculate second of target run time within a day
|
76
|
int target = targetHour*60*60+targetMinute*60+targetSecond;
|
77
|
|
78
|
// calculate second of current time within a day
|
79
|
Calendar calendar = Calendar.getInstance();
|
80
|
int second = calendar.get(Calendar.SECOND);
|
81
|
int minute = calendar.get(Calendar.MINUTE);
|
82
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
83
|
int current = hour*60*60+minute*60+second;
|
84
|
|
85
|
if(target > current) {
|
86
|
delay = target - current;
|
87
|
} else {
|
88
|
// one day has 86400 seconds
|
89
|
delay = 86400 - (current - target);
|
90
|
}
|
91
|
|
92
|
return delay;
|
93
|
}
|
94
|
|
95
|
public void setEmailSender(EmailSender emailSender) {
|
96
|
this.emailSender = emailSender;
|
97
|
}
|
98
|
|
99
|
public void setSendEmailNotifications(boolean sendEmailNotifications) { this.sendEmailNotifications = sendEmailNotifications; }
|
100
|
|
101
|
public boolean getSendEmailNotifications() { return sendEmailNotifications; }
|
102
|
|
103
|
public void setBeautySleep(String beautySleep) {
|
104
|
this.beautySleep = beautySleep;
|
105
|
}
|
106
|
|
107
|
public String getBeautySleep() {
|
108
|
return beautySleep;
|
109
|
}
|
110
|
|
111
|
public EmailSender getEmailSender() {
|
112
|
return emailSender;
|
113
|
}
|
114
|
|
115
|
public String getTargetHour() {
|
116
|
return targetHour;
|
117
|
}
|
118
|
|
119
|
public void setTargetHour(String targetHour) {
|
120
|
this.targetHour = targetHour;
|
121
|
}
|
122
|
|
123
|
public String getTargetMinute() {
|
124
|
return targetMinute;
|
125
|
}
|
126
|
|
127
|
public void setTargetMinute(String targetMinute) {
|
128
|
this.targetMinute = targetMinute;
|
129
|
}
|
130
|
|
131
|
public String getTargetSecond() {
|
132
|
return targetSecond;
|
133
|
}
|
134
|
|
135
|
public void setTargetSecond(String targetSecond) {
|
136
|
this.targetSecond = targetSecond;
|
137
|
}
|
138
|
}
|