f8bcc7572b67d3f6709e9bb01a4266426e9ec145
[proteocache.git] / server / compbio / listeners / ContextListener.java
1 package compbio.listeners;
2
3 import java.io.IOException;
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 import javax.servlet.annotation.WebListener;
11
12 import compbio.cassandra.CassandraNativeConnector;
13
14 /**
15  * Application Lifecycle Listener implementation class ContextListener
16  * 
17  */
18 @WebListener
19 public class ContextListener implements ServletContextListener {
20         private ScheduledExecutorService webjob_scheduler;
21         CassandraNativeConnector db = new CassandraNativeConnector();
22
23         /**
24          * @see ServletContextListener#contextInitialized(ServletContextEvent)
25          */
26         public void contextInitialized(ServletContextEvent arg0) {
27                 System.out.println("ProteoCache session start......");
28                 db.Connect();
29
30                 webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
31                 webjob_scheduler.scheduleAtFixedRate(new Runnable() {
32                         @Override
33                         public void run() {
34                                 try {
35                                         db.Parsing();
36                                 } catch (IOException e) {
37                                         // TODO Auto-generated catch block
38                                         e.printStackTrace();
39                                 }
40                         }
41                 }, 0, 600, TimeUnit.SECONDS);
42
43         }
44
45         /**
46          * @see ServletContextListener#contextDestroyed(ServletContextEvent)
47          */
48         public void contextDestroyed(ServletContextEvent arg0) {
49                 db.Closing();
50                 System.out.println("Shut down ProteoCache......");
51                 webjob_scheduler.shutdownNow();
52         }
53
54 }