changeset 42:a807a99a7e20

add servlet context listener to ServiceRegistry to shut down Spring bean. Can now be used as <listener> in web.xml.
author casties
date Mon, 17 Oct 2016 19:50:46 +0200
parents 2079c35739c8
children a270b5e73bda
files src/main/java/org/mpi/openmind/repository/services/ServiceRegistry.java
diffstat 1 files changed, 31 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/repository/services/ServiceRegistry.java	Mon Oct 17 12:28:39 2016 +0200
+++ b/src/main/java/org/mpi/openmind/repository/services/ServiceRegistry.java	Mon Oct 17 19:50:46 2016 +0200
@@ -2,12 +2,16 @@
 
 import java.io.Serializable;
 
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
 import org.apache.log4j.Logger;
 import org.mpi.openmind.cache.WrapperService;
 import org.mpi.openmind.configuration.ConfigurationService;
 import org.mpi.openmind.search.SearchService;
 import org.mpi.openmind.security.SecurityService;
 import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.scheduling.quartz.CronTriggerBean;
 
@@ -15,7 +19,7 @@
  * 
  * @author jurzua
  */
-public class ServiceRegistry implements Serializable {
+public class ServiceRegistry implements Serializable, ServletContextListener {
 	
 	private static final long serialVersionUID = 3883885508641723220L;
 	
@@ -33,12 +37,10 @@
 	private transient ApplicationContext context;
 	private transient WrapperService ws = null;
 
-	public ServiceRegistry() {
-		logger.info("Initialize Service Registry");
-		context = new ClassPathXmlApplicationContext(
-				new String[] { "openmind-context.xml" });
-
-	}
+    public ServiceRegistry() {
+        logger.info("Initializing Service Registry");
+        context = new ClassPathXmlApplicationContext(new String[] {"openmind-context.xml"});
+    }
 	
 	public WrapperService getWrapper(){
 		if(this.ws == null){
@@ -76,4 +78,26 @@
 	}
 	
 	public static void main(String[] args) {}
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
+     */
+    @Override
+    public void contextInitialized(ServletContextEvent arg0) {
+        logger.info("Servlet context initialized.");        
+    }
+    
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
+     */
+    @Override
+    public void contextDestroyed(ServletContextEvent arg0) {
+        if (context != null) {
+            // run Spring shutdown hook
+            logger.info("Closing Spring context.");
+            ((AbstractApplicationContext) context).close();
+        }
+        logger.info("Servlet context destroyed.");
+    }
+
 }