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.ScheduledFuture;
7 import java.util.concurrent.TimeUnit;
9 import javax.servlet.ServletContextEvent;
10 import javax.servlet.ServletContextListener;
11 import javax.servlet.annotation.WebListener;
13 import org.apache.log4j.Logger;
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;
24 * Application Lifecycle Listener implementation class ContextListener
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 public static boolean UPDATE_TABLE = false;
44 private static boolean initBooleanValue(String key) {
46 String status = ph.getProperty(key);
47 log.debug("Loading property: " + key + " with value: " + status);
48 if (Util.isEmpty(status)) {
51 return new Boolean(status.trim()).booleanValue();
55 * @see ServletContextListener#contextInitialized(ServletContextEvent)
57 public void contextInitialized(ServletContextEvent arg0) {
58 System.out.println("ProteoCache session start......");
59 // connect to the db and create table if needed
61 final CassandraNewTableWriter updater = new CassandraNewTableWriter();
63 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
64 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
65 FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
66 UPDATE_TABLE = initBooleanValue("cassandra.version.update");
68 executor = Executors.newScheduledThreadPool(3);
70 System.out.println("Initializating update version parameter");
71 //update_scheduler = Executors.newSingleThreadScheduledExecutor();
72 //update_scheduler.schedule(new Runnable() {
73 executor.schedule(new Runnable() {
76 updater.FillNewParameter();
78 }, 10, TimeUnit.SECONDS);
81 System.out.println("Initializating new table update scheduler");
82 //update_scheduler = Executors.newSingleThreadScheduledExecutor();
83 //update_scheduler.schedule(new Runnable() {
84 executor.schedule(new Runnable() {
87 updater.FillNewTable();
89 }, 10, TimeUnit.SECONDS);
93 // get data from real Jpred production server
94 String theprefix = ph.getProperty("cassandra.jpred.web.prefix");
95 if (null == theprefix) {
96 theprefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
99 final String datasrc = theprefix + "/usage-new/alljobs.dat";
100 final String prefix = theprefix;
101 final JpredParserHTTP parser = new JpredParserHTTP(prefix);
103 int initialdelay = 300;
104 int updaterate = 600;
105 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
106 if (0 <= newinitialdelay) {
107 initialdelay = newinitialdelay;
109 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
110 if (0 < newupdaterate) {
111 updaterate = newupdaterate;
113 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
115 //webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
116 System.out.println("Initializating web job scheduler");
117 System.out.println(" initial delay = " + initialdelay + " seconds");
118 System.out.println(" update rate = " + updaterate + " seconds");
119 if (0 < updateperiod)
120 System.out.println(" update period = " + updateperiod + " days");
122 System.out.println(" update period = 5 days");
124 //webjob_scheduler.scheduleAtFixedRate(new Runnable() {
125 webjobs = executor.scheduleAtFixedRate(new Runnable() {
129 if (0 < updateperiod) {
130 parser.Parsing(datasrc, updateperiod);
132 parser.Parsing(datasrc, 5);
134 } catch (IOException e) {
135 // TODO Auto-generated catch block
139 }, initialdelay, updaterate, TimeUnit.SECONDS);
142 if (READ_LOCALFILE_JPRED) {
143 // get irtifical data generated for the DB stress tests
144 final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
145 final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
146 final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
148 int initialdelay = 300;
149 int updaterate = 600;
150 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
151 if (0 <= newinitialdelay) {
152 initialdelay = newinitialdelay;
154 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
155 if (0 < newupdaterate) {
156 updaterate = newupdaterate;
158 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
160 //localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
161 System.out.println("Initializating local job scheduler");
162 System.out.println(" initial delay = " + initialdelay + " seconds");
163 System.out.println(" update rate = " + updaterate + " seconds");
164 if (0 < updateperiod)
165 System.out.println(" update period = " + updateperiod + " days");
167 System.out.println(" update period = 5 days");
168 //localjob_scheduler.scheduleAtFixedRate(new Runnable() {
169 localjobs = executor.scheduleAtFixedRate(new Runnable() {
173 if (0 < updateperiod) {
174 parser.Parsing(datasrc, updateperiod);
176 parser.Parsing(datasrc, 100);
178 } catch (IOException e) {
179 // TODO Auto-generated catch block
183 }, initialdelay, updaterate, TimeUnit.SECONDS);
189 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
192 public void contextDestroyed(ServletContextEvent arg0) {
194 System.out.println("Shut down ProteoCache......");
195 if (READ_WEB_JPRED) {
196 webjob_scheduler.shutdownNow();
198 if (READ_LOCALFILE_JPRED) {
199 localjob_scheduler.shutdownNow();
201 update_scheduler.shutdownNow();
204 public void contextDestroyed(ServletContextEvent arg0) {
207 System.out.println("Shut down ProteoCache......");
208 if (READ_WEB_JPRED) {
209 webjobs.cancel(true);
211 if (READ_LOCALFILE_JPRED) {
212 localjobs.cancel(true);
215 executor.awaitTermination(3, TimeUnit.SECONDS);
216 } catch (InterruptedException e) {
217 log.warn(e.getMessage(), e);