Project

General

Profile

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

    
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

    
54
            final ScheduledFuture<?> resetHandle = scheduler.scheduleAtFixedRate(emailSender, delay, Integer.parseInt(beautySleep), SECONDS);
55
        } else {
56
            logger.debug("Shutting down EmailScheduler for claim notifications");
57
            scheduler.shutdown();
58
        }
59
    }
60

    
61
    @Override
62
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
63
        if(!scheduler.isShutdown()) {
64
            logger.info("Shutting down EmailScheduler.");
65
            scheduler.shutdown();
66
        } else {
67
            logger.info("EmailScheduler is already shutdown");
68
        }
69
    }
70

    
71
    private int getInitialDelaySeconds(int targetHour, int targetMinute, int targetSecond) {
72
        int delay = 0;  // delay in seconds
73

    
74
        // calculate second of target run time within a day
75
        int target = targetHour*60*60+targetMinute*60+targetSecond;
76

    
77
        // calculate second of current time within a day
78
        Calendar calendar = Calendar.getInstance();
79
        int second = calendar.get(Calendar.SECOND);
80
        int minute = calendar.get(Calendar.MINUTE);
81
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
82
        int current = hour*60*60+minute*60+second;
83

    
84
        if(target > current) {
85
            delay = target - current;
86
        } else {
87
            // one day has 86400 seconds
88
            delay = 86400 - (current - target);
89
        }
90

    
91
        return delay;
92
    }
93

    
94
    public void setEmailSender(EmailSender emailSender) {
95
        this.emailSender = emailSender;
96
    }
97

    
98
    public void setSendEmailNotifications(boolean sendEmailNotifications) { this.sendEmailNotifications = sendEmailNotifications; }
99

    
100
    public boolean getSendEmailNotifications() { return sendEmailNotifications; }
101

    
102
    public void setBeautySleep(String beautySleep) {
103
        this.beautySleep = beautySleep;
104
    }
105

    
106
    public String getBeautySleep() {
107
        return beautySleep;
108
    }
109

    
110
    public EmailSender getEmailSender() {
111
        return emailSender;
112
    }
113

    
114
    public String getTargetHour() {
115
        return targetHour;
116
    }
117

    
118
    public void setTargetHour(String targetHour) {
119
        this.targetHour = targetHour;
120
    }
121

    
122
    public String getTargetMinute() {
123
        return targetMinute;
124
    }
125

    
126
    public void setTargetMinute(String targetMinute) {
127
        this.targetMinute = targetMinute;
128
    }
129

    
130
    public String getTargetSecond() {
131
        return targetSecond;
132
    }
133

    
134
    public void setTargetSecond(String targetSecond) {
135
        this.targetSecond = targetSecond;
136
    }
137
}
(1-1/2)