1 package compbio.listeners;
3 import java.io.IOException;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.TimeUnit;
8 import javax.servlet.ServletContextEvent;
9 import javax.servlet.ServletContextListener;
10 import javax.servlet.annotation.WebListener;
12 import org.apache.log4j.Logger;
14 import com.datastax.driver.core.Session;
16 import compbio.cassandra.CassandraNativeConnector;
17 import compbio.cassandra.CassandraNewTableWriter;
18 import compbio.cassandra.JpredParserHTTP;
19 import compbio.cassandra.JpredParserLocalFile;
20 import compbio.engine.ProteoCachePropertyHelperManager;
21 import compbio.util.PropertyHelper;
22 import compbio.util.Util;
25 * Application Lifecycle Listener implementation class ContextListener
29 public class ContextListener implements ServletContextListener {
30 private ScheduledExecutorService webjob_scheduler;
31 private ScheduledExecutorService localjob_scheduler;
32 private ScheduledExecutorService update_scheduler;
33 CassandraNativeConnector db = new CassandraNativeConnector();
34 static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
35 private static Logger log = Logger.getLogger(ContextListener.class);
36 public static boolean READ_WEB_JPRED = false;
37 public static boolean READ_LOCALFILE_JPRED = false;
38 public static boolean FILL_NEWTABLE = false;
40 private static boolean initBooleanValue(String key) {
42 String status = ph.getProperty(key);
43 log.debug("Loading property: " + key + " with value: " + status);
44 if (Util.isEmpty(status)) {
47 return new Boolean(status.trim()).booleanValue();
51 * @see ServletContextListener#contextInitialized(ServletContextEvent)
53 public void contextInitialized(ServletContextEvent arg0) {
54 System.out.println("ProteoCache session start......");
55 // connect to the db and create table if needed
57 final CassandraNewTableWriter updater = new CassandraNewTableWriter();
59 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
60 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
61 FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
64 System.out.println("Initializating new table update scheduler");
65 update_scheduler = Executors.newSingleThreadScheduledExecutor();
66 update_scheduler.schedule(new Runnable() {
69 updater.FillNewTable();
71 }, 10, TimeUnit.SECONDS);
75 // get data from real Jpred production server
76 final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
77 final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
78 final JpredParserHTTP parser = new JpredParserHTTP(prefix);
80 int initialdelay = 300;
82 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
83 if (0 <= newinitialdelay) {
84 initialdelay = newinitialdelay;
86 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
87 if (0 < newupdaterate) {
88 updaterate = newupdaterate;
90 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
92 webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
93 System.out.println("Initializating web job scheduler");
94 System.out.println(" initial delay = " + initialdelay + " seconds");
95 System.out.println(" update rate = " + updaterate + " seconds");
97 System.out.println(" update period = " + updateperiod + " days");
99 System.out.println(" update period = 5 days");
101 webjob_scheduler.scheduleAtFixedRate(new Runnable() {
105 if (0 < updateperiod) {
106 parser.Parsing(datasrc, updateperiod);
108 parser.Parsing(datasrc, 5);
110 } catch (IOException e) {
111 // TODO Auto-generated catch block
115 }, initialdelay, updaterate, TimeUnit.SECONDS);
118 if (READ_LOCALFILE_JPRED) {
119 // get irtifical data generated for the DB stress tests
120 final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
121 final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
122 final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
124 int initialdelay = 300;
125 int updaterate = 600;
126 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
127 if (0 <= newinitialdelay) {
128 initialdelay = newinitialdelay;
130 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
131 if (0 < newupdaterate) {
132 updaterate = newupdaterate;
134 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
136 localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
137 System.out.println("Initializating local job scheduler");
138 System.out.println(" initial delay = " + initialdelay + " seconds");
139 System.out.println(" update rate = " + updaterate + " seconds");
140 if (0 < updateperiod)
141 System.out.println(" update period = " + updateperiod + " days");
143 System.out.println(" update period = 5 days");
144 localjob_scheduler.scheduleAtFixedRate(new Runnable() {
148 if (0 < updateperiod) {
149 parser.Parsing(datasrc, updateperiod);
151 parser.Parsing(datasrc, 100);
153 } catch (IOException e) {
154 // TODO Auto-generated catch block
158 }, initialdelay, updaterate, TimeUnit.SECONDS);
164 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
166 public void contextDestroyed(ServletContextEvent arg0) {
168 System.out.println("Shut down ProteoCache......");
169 if (READ_WEB_JPRED) {
170 webjob_scheduler.shutdownNow();
172 if (READ_LOCALFILE_JPRED) {
173 localjob_scheduler.shutdownNow();
175 update_scheduler.shutdownNow();