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;
43 private static boolean initBooleanValue(String key) {
45 String status = ph.getProperty(key);
46 log.debug("Loading property: " + key + " with value: " + status);
47 if (Util.isEmpty(status)) {
50 return new Boolean(status.trim()).booleanValue();
54 * @see ServletContextListener#contextInitialized(ServletContextEvent)
56 public void contextInitialized(ServletContextEvent arg0) {
57 System.out.println("ProteoCache session start......");
58 // connect to the db and create table if needed
60 final CassandraNewTableWriter updater = new CassandraNewTableWriter();
62 READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update");
63 READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update");
64 FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update");
66 executor = Executors.newScheduledThreadPool(3);
68 System.out.println("Initializating new table update scheduler");
69 //update_scheduler = Executors.newSingleThreadScheduledExecutor();
70 //update_scheduler.schedule(new Runnable() {
71 executor.schedule(new Runnable() {
74 updater.FillNewTable();
76 }, 10, TimeUnit.SECONDS);
80 // get data from real Jpred production server
81 final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
82 final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
83 final JpredParserHTTP parser = new JpredParserHTTP(prefix);
85 int initialdelay = 300;
87 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay"));
88 if (0 <= newinitialdelay) {
89 initialdelay = newinitialdelay;
91 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate"));
92 if (0 < newupdaterate) {
93 updaterate = newupdaterate;
95 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period"));
97 //webjob_scheduler = Executors.newSingleThreadScheduledExecutor();
98 System.out.println("Initializating web job scheduler");
99 System.out.println(" initial delay = " + initialdelay + " seconds");
100 System.out.println(" update rate = " + updaterate + " seconds");
101 if (0 < updateperiod)
102 System.out.println(" update period = " + updateperiod + " days");
104 System.out.println(" update period = 5 days");
106 //webjob_scheduler.scheduleAtFixedRate(new Runnable() {
107 webjobs = executor.scheduleAtFixedRate(new Runnable() {
111 if (0 < updateperiod) {
112 parser.Parsing(datasrc, updateperiod);
114 parser.Parsing(datasrc, 5);
116 } catch (IOException e) {
117 // TODO Auto-generated catch block
121 }, initialdelay, updaterate, TimeUnit.SECONDS);
124 if (READ_LOCALFILE_JPRED) {
125 // get irtifical data generated for the DB stress tests
126 final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat";
127 final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata";
128 final JpredParserLocalFile parser = new JpredParserLocalFile(prefix);
130 int initialdelay = 300;
131 int updaterate = 600;
132 int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay"));
133 if (0 <= newinitialdelay) {
134 initialdelay = newinitialdelay;
136 int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate"));
137 if (0 < newupdaterate) {
138 updaterate = newupdaterate;
140 final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period"));
142 //localjob_scheduler = Executors.newSingleThreadScheduledExecutor();
143 System.out.println("Initializating local job scheduler");
144 System.out.println(" initial delay = " + initialdelay + " seconds");
145 System.out.println(" update rate = " + updaterate + " seconds");
146 if (0 < updateperiod)
147 System.out.println(" update period = " + updateperiod + " days");
149 System.out.println(" update period = 5 days");
150 //localjob_scheduler.scheduleAtFixedRate(new Runnable() {
151 localjobs = executor.scheduleAtFixedRate(new Runnable() {
155 if (0 < updateperiod) {
156 parser.Parsing(datasrc, updateperiod);
158 parser.Parsing(datasrc, 100);
160 } catch (IOException e) {
161 // TODO Auto-generated catch block
165 }, initialdelay, updaterate, TimeUnit.SECONDS);
171 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
174 public void contextDestroyed(ServletContextEvent arg0) {
176 System.out.println("Shut down ProteoCache......");
177 if (READ_WEB_JPRED) {
178 webjob_scheduler.shutdownNow();
180 if (READ_LOCALFILE_JPRED) {
181 localjob_scheduler.shutdownNow();
183 update_scheduler.shutdownNow();
186 public void contextDestroyed(ServletContextEvent arg0) {
189 System.out.println("Shut down ProteoCache......");
190 if (READ_WEB_JPRED) {
191 webjobs.cancel(true);
193 if (READ_LOCALFILE_JPRED) {
194 localjobs.cancel(true);
197 executor.awaitTermination(3, TimeUnit.SECONDS);
198 } catch (InterruptedException e) {
199 log.warn(e.getMessage(), e);