Project

General

Profile

1
package eu.dnetlib.data.emailSender;
2

    
3
import org.apache.logging.log4j.LogManager;
4
import org.apache.logging.log4j.Logger;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.web.context.support.WebApplicationContextUtils;
7

    
8
import javax.servlet.ServletContextEvent;
9
import javax.servlet.ServletContextListener;
10
import java.util.Calendar;
11
import java.util.concurrent.Executors;
12
import java.util.concurrent.ScheduledExecutorService;
13
import java.util.concurrent.ScheduledFuture;
14

    
15
import static java.util.concurrent.TimeUnit.SECONDS;
16

    
17
/**
18
 * Created by kiatrop on 10/5/2017.
19
 */
20

    
21

    
22
public class EmailScheduler implements ServletContextListener {
23

    
24
    private int delay = 0;
25
    private final Logger logger = LogManager.getLogger(EmailScheduler.class);
26

    
27
    private static boolean sendEmailNotifications;
28

    
29
    @Autowired
30
    private String beautySleep;
31

    
32
    @Autowired
33
    private String targetHour;
34
    @Autowired
35
    private String targetMinute;
36
    @Autowired
37
    private String targetSecond;
38
    @Autowired
39
    private EmailSender emailSender;
40

    
41
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
42

    
43
    @Override
44
    public void contextInitialized(ServletContextEvent servletContextEvent) {
45
        WebApplicationContextUtils
46
                .getRequiredWebApplicationContext(servletContextEvent.getServletContext())
47
                .getAutowireCapableBeanFactory()
48
                .autowireBean(this);
49
        logger.debug("Initializing EmailScheduler with beautySleep " + beautySleep + " and begin time " + targetHour + ":" + targetMinute + ":" + targetSecond + " and email sender " + emailSender);
50
        if(sendEmailNotifications) {
51
            logger.debug("Initializing EmailScheduler with beautySleep " + beautySleep + " and begin time " + targetHour + ":" + targetMinute + ":" + targetSecond + " and email sender " + emailSender);
52
            delay = getInitialDelaySeconds(Integer.parseInt(targetHour), Integer.parseInt(targetMinute), Integer.parseInt(targetSecond));
53
            logger.debug("EmailScheduler will wait " + delay + " seconds");
54
            logger.debug("Send mail to community managers: " +this.emailSender.isNotifyCommunityManagers());
55
            logger.debug("Send mail to project managers: " +this.emailSender.isNotifyProjectManagers());
56
            final ScheduledFuture<?> resetHandle = scheduler.scheduleAtFixedRate(emailSender, delay, Integer.parseInt(beautySleep), SECONDS);
57
        } else {
58
            logger.debug("Shutting down EmailScheduler for claim notifications");
59
            scheduler.shutdown();
60
        }
61
    }
62

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

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

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

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

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

    
93
        return delay;
94
    }
95

    
96
    public void setEmailSender(EmailSender emailSender) {
97
        this.emailSender = emailSender;
98
    }
99

    
100
    public void setSendEmailNotifications(boolean sendEmailNotifications) { this.sendEmailNotifications = sendEmailNotifications; }
101

    
102
    public boolean getSendEmailNotifications() { return sendEmailNotifications; }
103

    
104
    public void setBeautySleep(String beautySleep) {
105
        this.beautySleep = beautySleep;
106
    }
107

    
108
    public String getBeautySleep() {
109
        return beautySleep;
110
    }
111

    
112
    public EmailSender getEmailSender() {
113
        return emailSender;
114
    }
115

    
116
    public String getTargetHour() {
117
        return targetHour;
118
    }
119

    
120
    public void setTargetHour(String targetHour) {
121
        this.targetHour = targetHour;
122
    }
123

    
124
    public String getTargetMinute() {
125
        return targetMinute;
126
    }
127

    
128
    public void setTargetMinute(String targetMinute) {
129
        this.targetMinute = targetMinute;
130
    }
131

    
132
    public String getTargetSecond() {
133
        return targetSecond;
134
    }
135

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