Subversion Repositories SmartDukaan

Rev

Rev 3975 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3971 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.creation.util;
5
 
6
import in.shop2020.util.BDBHotBackupTask;
7
 
8
import java.text.ParseException;
9
import java.text.SimpleDateFormat;
10
import java.util.Calendar;
11
import java.util.Date;
12
import java.util.concurrent.Executors;
13
import java.util.concurrent.ScheduledExecutorService;
14
import java.util.concurrent.TimeUnit;
15
 
16
import javax.servlet.ServletContextEvent;
17
import javax.servlet.ServletContextListener;
18
 
19
import org.apache.commons.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21
 
22
/**
23
 * @author mandeep
24
 *
25
 */
26
public class BackgroundTaskExecutor implements ServletContextListener {
27
    private static Log log = LogFactory.getLog(BackgroundTaskExecutor.class);
28
    private ScheduledExecutorService scheduler;
29
 
30
    @Override
31
    public void contextInitialized(ServletContextEvent event) {
32
        log.info("Inside contextInitialized");
33
        scheduler = Executors.newSingleThreadScheduledExecutor();
34
 
3976 mandeep.dh 35
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
3971 mandeep.dh 36
        SimpleDateFormat sdfYYYYMMDD = new SimpleDateFormat("yyyyMMdd");
37
        long initialDelay = 0;
3975 mandeep.dh 38
        long period = 12 * 60 * 60 * 1000; //12 hours in milliseconds
3971 mandeep.dh 39
 
40
        try {
41
            Calendar calendar = Calendar.getInstance();
42
            calendar.add(Calendar.DATE, 1);
43
            initialDelay = sdf.parse(sdfYYYYMMDD.format(calendar.getTime()) + " 15:00:00").getTime() - new Date().getTime();
44
            initialDelay %= period;
45
        } catch (ParseException e) {
46
            log.error("Could not compute initial delay", e);
47
        }
48
 
49
        scheduler.scheduleAtFixedRate(new BDBHotBackupTask(), initialDelay, period, TimeUnit.MILLISECONDS);
50
    }
51
 
52
    @Override
53
    public void contextDestroyed(ServletContextEvent event) {
54
        log.info("Inside contextDestroyed");
55
        scheduler.shutdownNow();
56
    }
57
}