package compbio.listeners; import java.io.IOException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import org.apache.log4j.Logger; import compbio.cassandra.CassandraNativeConnector; import compbio.cassandra.CassandraNewTableWriter; import compbio.cassandra.JpredParserHTTP; import compbio.cassandra.JpredParserLocalFile; import compbio.engine.ProteoCachePropertyHelperManager; import compbio.util.PropertyHelper; import compbio.util.Util; /** * Application Lifecycle Listener implementation class ContextListener * */ @WebListener public class ContextListener implements ServletContextListener { //private ScheduledExecutorService webjob_scheduler; //private ScheduledExecutorService localjob_scheduler; //private ScheduledExecutorService update_scheduler; private ScheduledExecutorService executor; private ScheduledFuture webjobs; private ScheduledFuture localjobs; private ScheduledFuture updates; CassandraNativeConnector db = new CassandraNativeConnector(); private static PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper(); private static Logger log = Logger.getLogger(ContextListener.class); public static boolean READ_WEB_JPRED = false; public static boolean READ_LOCALFILE_JPRED = false; public static boolean FILL_NEWTABLE = false; private static boolean initBooleanValue(String key) { assert key != null; String status = ph.getProperty(key); log.debug("Loading property: " + key + " with value: " + status); if (Util.isEmpty(status)) { return false; } return new Boolean(status.trim()).booleanValue(); } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { System.out.println("ProteoCache session start......"); // connect to the db and create table if needed db.Connect(); final CassandraNewTableWriter updater = new CassandraNewTableWriter(); READ_WEB_JPRED = initBooleanValue("cassandra.jpred.web.update"); READ_LOCALFILE_JPRED = initBooleanValue("cassandra.jpred.local.update"); FILL_NEWTABLE = initBooleanValue("cassandra.newtables.update"); executor = Executors.newScheduledThreadPool(3); if (FILL_NEWTABLE) { System.out.println("Initializating new table update scheduler"); //update_scheduler = Executors.newSingleThreadScheduledExecutor(); //update_scheduler.schedule(new Runnable() { executor.schedule(new Runnable() { @Override public void run() { updater.FillNewTable(); } }, 10, TimeUnit.SECONDS); } if (READ_WEB_JPRED) { // get data from real Jpred production server final String datasrc = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat"; final String prefix = "http://www.compbio.dundee.ac.uk/www-jpred/results"; final JpredParserHTTP parser = new JpredParserHTTP(prefix); int initialdelay = 300; int updaterate = 600; int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.inidelay")); if (0 <= newinitialdelay) { initialdelay = newinitialdelay; } int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.updaterate")); if (0 < newupdaterate) { updaterate = newupdaterate; } final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.web.period")); //webjob_scheduler = Executors.newSingleThreadScheduledExecutor(); System.out.println("Initializating web job scheduler"); System.out.println(" initial delay = " + initialdelay + " seconds"); System.out.println(" update rate = " + updaterate + " seconds"); if (0 < updateperiod) System.out.println(" update period = " + updateperiod + " days"); else System.out.println(" update period = 5 days"); //webjob_scheduler.scheduleAtFixedRate(new Runnable() { webjobs = executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { if (0 < updateperiod) { parser.Parsing(datasrc, updateperiod); } else { parser.Parsing(datasrc, 5); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, initialdelay, updaterate, TimeUnit.SECONDS); } if (READ_LOCALFILE_JPRED) { // get irtifical data generated for the DB stress tests final String datasrc = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/data.dat"; final String prefix = "/home/asherstnev/Projects/Java.projects/proteocache/data_stress_test/Jpreddata"; final JpredParserLocalFile parser = new JpredParserLocalFile(prefix); int initialdelay = 300; int updaterate = 600; int newinitialdelay = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.inidelay")); if (0 <= newinitialdelay) { initialdelay = newinitialdelay; } int newupdaterate = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.updaterate")); if (0 < newupdaterate) { updaterate = newupdaterate; } final int updateperiod = ProteoCachePropertyHelperManager.getIntProperty(ph.getProperty("cassandra.jpred.local.period")); //localjob_scheduler = Executors.newSingleThreadScheduledExecutor(); System.out.println("Initializating local job scheduler"); System.out.println(" initial delay = " + initialdelay + " seconds"); System.out.println(" update rate = " + updaterate + " seconds"); if (0 < updateperiod) System.out.println(" update period = " + updateperiod + " days"); else System.out.println(" update period = 5 days"); //localjob_scheduler.scheduleAtFixedRate(new Runnable() { localjobs = executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { if (0 < updateperiod) { parser.Parsing(datasrc, updateperiod); } else { parser.Parsing(datasrc, 100); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, initialdelay, updaterate, TimeUnit.SECONDS); } } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ /* public void contextDestroyed(ServletContextEvent arg0) { db.Closing(); System.out.println("Shut down ProteoCache......"); if (READ_WEB_JPRED) { webjob_scheduler.shutdownNow(); } if (READ_LOCALFILE_JPRED) { localjob_scheduler.shutdownNow(); } update_scheduler.shutdownNow(); } */ public void contextDestroyed(ServletContextEvent arg0) { db.Closing(); try { System.out.println("Shut down ProteoCache......"); if (READ_WEB_JPRED) { webjobs.cancel(true); } if (READ_LOCALFILE_JPRED) { localjobs.cancel(true); } executor.shutdown(); executor.awaitTermination(3, TimeUnit.SECONDS); } catch (InterruptedException e) { log.warn(e.getMessage(), e); } } }