Attempt to resolve the problem with java on cluster...
[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.ScheduledFuture;
7 import java.util.concurrent.TimeUnit;
8
9 import javax.servlet.ServletContextEvent;
10 import javax.servlet.ServletContextListener;
11 import javax.servlet.annotation.WebListener;
12
13 import org.apache.log4j.Logger;
14
15 import compbio.cassandra.CassandraNativeConnector;
16 import compbio.cassandra.CassandraNewTableWriter;
17 import compbio.cassandra.JpredParserHTTP;
18 import compbio.cassandra.JpredParserLocalFile;
19 import compbio.engine.ProteoCachePropertyHelperManager;
20 import compbio.util.PropertyHelper;
21 import compbio.util.Util;
22
23 /**
24  * Application Lifecycle Listener implementation class ContextListener
25  * 
26  */
27 @WebListener
28 public class ContextListener implements ServletContextListener {
29         //private ScheduledExecutorService webjob_scheduler;
30         //private ScheduledExecutorService localjob_scheduler;
31         //private ScheduledExecutorService update_scheduler;
32         private ScheduledExecutorService executor;
33         private ScheduledFuture<?> webjobs;
34         private ScheduledFuture<?> localjobs;
35         private ScheduledFuture<?> updates;
36         CassandraNativeConnector db = new CassandraNativeConnector();
37         private static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
38         private static Logger log = Logger.getLogger(ContextListener.class);
39         public static boolean READ_WEB_JPRED = false;
40         public static boolean READ_LOCALFILE_JPRED = false;
41         public static boolean FILL_NEWTABLE = false;
42
43         private static boolean initBooleanValue(String key) {
44                 assert key != null;
45                 String status = ph.getProperty(key);
46                 log.debug("Loading property: " + key + " with value: " + status);
47                 if (Util.isEmpty(status)) {
48                         return false;
49                 }
50                 return new Boolean(status.trim()).booleanValue();
51         }
52
53         /**
54          * @see ServletContextListener#contextInitialized(ServletContextEvent)
55          */
56         public void contextInitialized(ServletContextEvent arg0) {
57                 System.out.println("ProteoCache session start......");
58                 // connect to the db and create table if needed
59                 db.Connect();
60                 final CassandraNewTableWriter updater = new CassandraNewTableWriter();
61
62                 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
63                 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
64                 FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
65
66                 executor = Executors.newScheduledThreadPool(3);
67                 if (FILL_NEWTABLE) {
68                         System.out.println("Initializating new table update scheduler");
69                         //update_scheduler = Executors.newSingleThreadScheduledExecutor();
70                         //update_scheduler.schedule(new Runnable() {
71                         executor.schedule(new Runnable() {
72                                 @Override
73                                 public void run() {
74                                         updater.FillNewTable();
75                                 }
76                         }, 10, TimeUnit.SECONDS);
77                 }
78
79                 if (READ_WEB_JPRED) {
80                         // get data from real Jpred production server
81                         final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
82                         final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
83                         final JpredParserHTTP parser = new JpredParserHTTP(prefix);
84
85                         int initialdelay = 300;
86                         int updaterate = 600;
87                         int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
88                         if (0 <= newinitialdelay) {
89                                 initialdelay = newinitialdelay;
90                         }
91                         int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
92                         if (0 < newupdaterate) {
93                                 updaterate = newupdaterate;
94                         }
95                         final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
96
97                         //webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
98                         System.out.println("Initializating web job scheduler");
99                         System.out.println("    initial delay = " + initialdelay + " seconds");
100                         System.out.println("    update rate = " + updaterate + " seconds");
101                         if (0 < updateperiod)
102                                 System.out.println("    update period = " + updateperiod + " days");
103                         else
104                                 System.out.println("    update period = 5 days");
105
106                         //webjob_scheduler.scheduleAtFixedRate(new Runnable() {
107                         webjobs = executor.scheduleAtFixedRate(new Runnable() {
108                                 @Override
109                                 public void run() {
110                                         try {
111                                                 if (0 < updateperiod) {
112                                                         parser.Parsing(datasrc, updateperiod);
113                                                 } else {
114                                                         parser.Parsing(datasrc, 5);
115                                                 }
116                                         } catch (IOException e) {
117                                                 // TODO Auto-generated catch block
118                                                 e.printStackTrace();
119                                         }
120                                 }
121                         }, initialdelay, updaterate, TimeUnit.SECONDS);
122                 }
123
124                 if (READ_LOCALFILE_JPRED) {
125                         // get irtifical data generated for the DB stress tests
126                         final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
127                         final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
128                         final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
129
130                         int initialdelay = 300;
131                         int updaterate = 600;
132                         int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
133                         if (0 <= newinitialdelay) {
134                                 initialdelay = newinitialdelay;
135                         }
136                         int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
137                         if (0 < newupdaterate) {
138                                 updaterate = newupdaterate;
139                         }
140                         final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
141
142                         //localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
143                         System.out.println("Initializating local job scheduler");
144                         System.out.println("    initial delay = " + initialdelay + " seconds");
145                         System.out.println("    update rate = " + updaterate + " seconds");
146                         if (0 < updateperiod)
147                                 System.out.println("    update period = " + updateperiod + " days");
148                         else
149                                 System.out.println("    update period = 5 days");
150                         //localjob_scheduler.scheduleAtFixedRate(new Runnable() {
151                         localjobs = executor.scheduleAtFixedRate(new Runnable() {
152                                 @Override
153                                 public void run() {
154                                         try {
155                                                 if (0 < updateperiod) {
156                                                         parser.Parsing(datasrc, updateperiod);
157                                                 } else {
158                                                         parser.Parsing(datasrc, 100);
159                                                 }
160                                         } catch (IOException e) {
161                                                 // TODO Auto-generated catch block
162                                                 e.printStackTrace();
163                                         }
164                                 }
165                         }, initialdelay, updaterate, TimeUnit.SECONDS);
166                 }
167
168         }
169
170         /**
171          * @see ServletContextListener#contextDestroyed(ServletContextEvent)
172          */
173         /*
174         public void contextDestroyed(ServletContextEvent arg0) {
175                 db.Closing();
176                 System.out.println("Shut down ProteoCache......");
177                 if (READ_WEB_JPRED) {
178                         webjob_scheduler.shutdownNow();
179                 }
180                 if (READ_LOCALFILE_JPRED) {
181                         localjob_scheduler.shutdownNow();
182                 }
183                 update_scheduler.shutdownNow();
184         }
185         */
186         public void contextDestroyed(ServletContextEvent arg0) {
187                 db.Closing();
188         try {
189                 System.out.println("Shut down ProteoCache......");
190                 if (READ_WEB_JPRED) {
191                         webjobs.cancel(true);
192                 }
193                 if (READ_LOCALFILE_JPRED) {
194                         localjobs.cancel(true);
195                 }
196                 executor.shutdown();
197                 executor.awaitTermination(3, TimeUnit.SECONDS);
198         } catch (InterruptedException e) {
199                 log.warn(e.getMessage(), e);
200         }
201         }
202 }