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 update_scheduler = Executors.newSingleThreadScheduledExecutor();
65 update_scheduler.schedule(new Runnable() {
68 updater.FillNewTable();
70 }, 10, TimeUnit.SECONDS);
74 // get data from real Jpred production server
75 final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
76 final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
77 final JpredParserHTTP parser = new JpredParserHTTP(prefix);
79 int initialdelay = 300;
81 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
82 if (0 <= newinitialdelay) {
83 initialdelay = newinitialdelay;
85 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
86 if (0 < newupdaterate) {
87 updaterate = newupdaterate;
89 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
91 webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
92 System.out.println("Initializating web job scheduler");
93 System.out.println(" initial delay = " + initialdelay + " seconds");
94 System.out.println(" update rate = " + updaterate + " seconds");
96 System.out.println(" update period = " + updateperiod + " days");
98 System.out.println(" update period = 5 days");
100 webjob_scheduler.scheduleAtFixedRate(new Runnable() {
104 if (0 < updateperiod) {
105 parser.Parsing(datasrc, updateperiod);
107 parser.Parsing(datasrc, 5);
109 } catch (IOException e) {
110 // TODO Auto-generated catch block
114 }, initialdelay, updaterate, TimeUnit.SECONDS);
117 if (READ_LOCALFILE_JPRED) {
118 // get irtifical data generated for the DB stress tests
119 final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
120 final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
121 final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
123 int initialdelay = 300;
124 int updaterate = 600;
125 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
126 if (0 <= newinitialdelay) {
127 initialdelay = newinitialdelay;
129 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
130 if (0 < newupdaterate) {
131 updaterate = newupdaterate;
133 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
135 localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
136 System.out.println("Initializating local job scheduler");
137 System.out.println(" initial delay = " + initialdelay + " seconds");
138 System.out.println(" update rate = " + updaterate + " seconds");
139 if (0 < updateperiod)
140 System.out.println(" update period = " + updateperiod + " days");
142 System.out.println(" update period = 5 days");
143 localjob_scheduler.scheduleAtFixedRate(new Runnable() {
147 if (0 < updateperiod) {
148 parser.Parsing(datasrc, updateperiod);
150 parser.Parsing(datasrc, 100);
152 } catch (IOException e) {
153 // TODO Auto-generated catch block
157 }, initialdelay, updaterate, TimeUnit.SECONDS);
163 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
165 public void contextDestroyed(ServletContextEvent arg0) {
167 System.out.println("Shut down ProteoCache......");
168 if (READ_WEB_JPRED) {
169 webjob_scheduler.shutdownNow();
171 if (READ_LOCALFILE_JPRED) {
172 localjob_scheduler.shutdownNow();
174 update_scheduler.shutdownNow();