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 CassandraNativeConnector db = new CassandraNativeConnector();
33 static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
34 private static Logger log = Logger.getLogger(ContextListener.class);
35 public static boolean READ_WEB_JPRED = false;
36 public static boolean READ_LOCALFILE_JPRED = false;
38 private static boolean initBooleanValue(String key) {
40 String status = ph.getProperty(key);
41 log.debug("Loading property: " + key + " with value: " + status);
42 if (Util.isEmpty(status)) {
45 return new Boolean(status.trim()).booleanValue();
49 * @see ServletContextListener#contextInitialized(ServletContextEvent)
51 public void contextInitialized(ServletContextEvent arg0) {
52 System.out.println("ProteoCache session start......");
53 // connect to the db and create table if needed
55 CassandraNewTableWriter updater = new CassandraNewTableWriter();
57 // updater.FillParameters();
58 // updater.FillNewTable();
60 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
61 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
64 // get data from real Jpred production server
65 final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
66 final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
67 final JpredParserHTTP parser = new JpredParserHTTP(prefix);
69 int initialdelay = 300;
71 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
72 if (0 <= newinitialdelay) {
73 initialdelay = newinitialdelay;
75 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
76 if (0 < newupdaterate) {
77 updaterate = newupdaterate;
79 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
81 webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
82 System.out.println("Initializating web job scheduler");
83 System.out.println(" initial delay = " + initialdelay + " seconds");
84 System.out.println(" update rate = " + updaterate + " seconds");
86 System.out.println(" update period = " + updateperiod + " days");
88 System.out.println(" update period = 5 days");
90 webjob_scheduler.scheduleAtFixedRate(new Runnable() {
94 if (0 < updateperiod) {
95 parser.Parsing(datasrc, updateperiod);
97 parser.Parsing(datasrc, 5);
99 } catch (IOException e) {
100 // TODO Auto-generated catch block
104 }, initialdelay, updaterate, TimeUnit.SECONDS);
107 if (READ_LOCALFILE_JPRED) {
108 // get irtifical data generated for the DB stress tests
109 final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
110 final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
111 final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
113 int initialdelay = 300;
114 int updaterate = 600;
115 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
116 if (0 <= newinitialdelay) {
117 initialdelay = newinitialdelay;
119 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
120 if (0 < newupdaterate) {
121 updaterate = newupdaterate;
123 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
125 localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
126 System.out.println("Initializating local job scheduler");
127 System.out.println(" initial delay = " + initialdelay + " seconds");
128 System.out.println(" update rate = " + updaterate + " seconds");
129 if (0 < updateperiod)
130 System.out.println(" update period = " + updateperiod + " days");
132 System.out.println(" update period = 5 days");
133 localjob_scheduler.scheduleAtFixedRate(new Runnable() {
137 if (0 < updateperiod) {
138 parser.Parsing(datasrc, updateperiod);
140 parser.Parsing(datasrc, 100);
142 } catch (IOException e) {
143 // TODO Auto-generated catch block
147 }, initialdelay, updaterate, TimeUnit.SECONDS);
153 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
155 public void contextDestroyed(ServletContextEvent arg0) {
157 System.out.println("Shut down ProteoCache......");
158 if (READ_WEB_JPRED) {
159 webjob_scheduler.shutdownNow();
161 if (READ_LOCALFILE_JPRED) {
162 localjob_scheduler.shutdownNow();