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 compbio.cassandra.CassandraNativeConnector;
15 import compbio.cassandra.CassandraNewTableWriter;
16 import compbio.cassandra.JpredParserHTTP;
17 import compbio.cassandra.JpredParserLocalFile;
18 import compbio.engine.ProteoCachePropertyHelperManager;
19 import compbio.util.PropertyHelper;
20 import compbio.util.Util;
23 * Application Lifecycle Listener implementation class ContextListener
27 public class ContextListener implements ServletContextListener {
28 private ScheduledExecutorService webjob_scheduler;
29 private ScheduledExecutorService localjob_scheduler;
30 private ScheduledExecutorService update_scheduler;
31 CassandraNativeConnector db = new CassandraNativeConnector();
32 private static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
33 private static Logger log = Logger.getLogger(ContextListener.class);
34 public static boolean READ_WEB_JPRED = false;
35 public static boolean READ_LOCALFILE_JPRED = false;
36 public static boolean FILL_NEWTABLE = 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 final CassandraNewTableWriter updater = new CassandraNewTableWriter();
57 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
58 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
59 FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
62 System.out.println("Initializating new table update scheduler");
63 update_scheduler = Executors.newSingleThreadScheduledExecutor();
64 update_scheduler.schedule(new Runnable() {
67 updater.FillNewTable();
69 }, 10, TimeUnit.SECONDS);
73 // get data from real Jpred production server
74 final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
75 final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
76 final JpredParserHTTP parser = new JpredParserHTTP(prefix);
78 int initialdelay = 300;
80 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
81 if (0 <= newinitialdelay) {
82 initialdelay = newinitialdelay;
84 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
85 if (0 < newupdaterate) {
86 updaterate = newupdaterate;
88 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
90 webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
91 System.out.println("Initializating web job scheduler");
92 System.out.println(" initial delay = " + initialdelay + " seconds");
93 System.out.println(" update rate = " + updaterate + " seconds");
95 System.out.println(" update period = " + updateperiod + " days");
97 System.out.println(" update period = 5 days");
99 webjob_scheduler.scheduleAtFixedRate(new Runnable() {
103 if (0 < updateperiod) {
104 parser.Parsing(datasrc, updateperiod);
106 parser.Parsing(datasrc, 5);
108 } catch (IOException e) {
109 // TODO Auto-generated catch block
113 }, initialdelay, updaterate, TimeUnit.SECONDS);
116 if (READ_LOCALFILE_JPRED) {
117 // get irtifical data generated for the DB stress tests
118 final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
119 final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
120 final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
122 int initialdelay = 300;
123 int updaterate = 600;
124 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
125 if (0 <= newinitialdelay) {
126 initialdelay = newinitialdelay;
128 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
129 if (0 < newupdaterate) {
130 updaterate = newupdaterate;
132 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
134 localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
135 System.out.println("Initializating local job scheduler");
136 System.out.println(" initial delay = " + initialdelay + " seconds");
137 System.out.println(" update rate = " + updaterate + " seconds");
138 if (0 < updateperiod)
139 System.out.println(" update period = " + updateperiod + " days");
141 System.out.println(" update period = 5 days");
142 localjob_scheduler.scheduleAtFixedRate(new Runnable() {
146 if (0 < updateperiod) {
147 parser.Parsing(datasrc, updateperiod);
149 parser.Parsing(datasrc, 100);
151 } catch (IOException e) {
152 // TODO Auto-generated catch block
156 }, initialdelay, updaterate, TimeUnit.SECONDS);
162 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
164 public void contextDestroyed(ServletContextEvent arg0) {
166 System.out.println("Shut down ProteoCache......");
167 if (READ_WEB_JPRED) {
168 webjob_scheduler.shutdownNow();
170 if (READ_LOCALFILE_JPRED) {
171 localjob_scheduler.shutdownNow();
173 update_scheduler.shutdownNow();