diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java index 8cbb9ae1c..824581d97 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java @@ -41,6 +41,8 @@ import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilterUtils; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters; import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; +import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder; +import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexingEventListener; import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup; import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread; @@ -52,7 +54,7 @@ import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread; * As of VIVO release 1.4, the counts come from the solr index. Before that they * came from the DAOs. */ -public class VClassGroupCache { +public class VClassGroupCache implements IndexingEventListener { private static final Log log = LogFactory.getLog(VClassGroupCache.class); private static final String ATTRIBUTE_NAME = "VClassGroupCache"; @@ -453,6 +455,10 @@ public class VClassGroupCache { } /* ****************** Jena Model Change Listener***************************** */ + + /** + * Listen for changes to what class group classes are in and their display rank. + */ protected class VClassGroupCacheChangeListener extends StatementListener { public void addedStatement(Statement stmt) { checkAndDoUpdate(stmt); @@ -488,11 +494,15 @@ public class VClassGroupCache { public static class Setup implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { - ServletContext servletContext = sce.getServletContext(); - VClassGroupCache vcgc = new VClassGroupCache(servletContext); + ServletContext context = sce.getServletContext(); + VClassGroupCache vcgc = new VClassGroupCache(context); vcgc.requestCacheUpdate(); - servletContext.setAttribute(ATTRIBUTE_NAME,vcgc); - log.info("VClassGroupCache added to context"); + context.setAttribute(ATTRIBUTE_NAME,vcgc); + log.info("VClassGroupCache added to context"); + + IndexBuilder indexBuilder = IndexBuilder.getBuilder(context); + indexBuilder.addIndexBuilderListener(vcgc); + log.info("VClassGroupCache set to listen to events from IndexBuilder"); } @Override @@ -512,4 +522,22 @@ public class VClassGroupCache { return (VClassGroupCache) sc.getAttribute(ATTRIBUTE_NAME); } + /** + * Handle notification of events from the IndexBuilder. + */ + @Override + public void notifyOfIndexingEvent(EventTypes event) { + switch( event ){ + case FINISH_FULL_REBUILD: + case FINISHED_UPDATE: + log.debug("rebuilding because of IndexBuilder " + event.name()); + requestCacheUpdate(); + break; + default: + log.debug("ignoring event type " + event.name()); + break; + + } + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java index 19d55b5f1..5f7a8d75d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; @@ -73,6 +74,10 @@ public class IndexBuilder extends VitroBackgroundThread { /** Number of threads to use during indexing. */ protected int numberOfThreads = 10; + /** List of IndexingEventListeners */ + protected LinkedList indexingEventListeners = + new LinkedList(); + public static final int MAX_REINDEX_THREADS= 10; public static final int MAX_UPDATE_THREADS= 10; public static final int MAX_THREADS = Math.max( MAX_UPDATE_THREADS, MAX_REINDEX_THREADS); @@ -132,7 +137,7 @@ public class IndexBuilder extends VitroBackgroundThread { //set flag for full index rebuild this.reindexRequested = true; //wake up - this.notifyAll(); + this.notifyAll(); } /** @@ -142,7 +147,15 @@ public class IndexBuilder extends VitroBackgroundThread { //wake up thread and it will attempt to index anything in changedUris this.notifyAll(); } - + + /** + * Add a listener for indexing events. Methods on listener will be called when + * events happen in the IndexBuilder. This is not a Jena ModelListener. + */ + public synchronized void addIndexBuilderListener(IndexingEventListener listener){ + indexingEventListeners.add(listener); + } + /** * This is called when the system shuts down. */ @@ -159,13 +172,22 @@ public class IndexBuilder extends VitroBackgroundThread { if( reindexRequested ){ setWorkLevel(WorkLevel.WORKING, FLAG_REBUILDING); log.debug("full re-index requested"); + + notifyListeners( IndexingEventListener.EventTypes.START_FULL_REBUILD ); indexRebuild(); + notifyListeners( IndexingEventListener.EventTypes.FINISH_FULL_REBUILD ); + setWorkLevel(WorkLevel.IDLE); }else if( !changedStmtQueue.isEmpty() ){ - setWorkLevel(WorkLevel.WORKING, FLAG_UPDATING); - Thread.sleep(WAIT_AFTER_NEW_WORK_INTERVAL); //wait a bit to let a bit more work to come into the queue + setWorkLevel(WorkLevel.WORKING, FLAG_UPDATING); + + //wait a bit to let a bit more work to come into the queue + Thread.sleep(WAIT_AFTER_NEW_WORK_INTERVAL); log.debug("work found for IndexBuilder, starting update"); + + notifyListeners( IndexingEventListener.EventTypes.START_UPDATE ); updatedIndex(); + notifyListeners( IndexingEventListener.EventTypes.FINISHED_UPDATE ); setWorkLevel(WorkLevel.IDLE); } else { log.debug("there is no indexing working to do, waiting for work"); @@ -179,7 +201,9 @@ public class IndexBuilder extends VitroBackgroundThread { } if( indexer != null) - indexer.abortIndexingAndCleanUp(); + indexer.abortIndexingAndCleanUp(); + + notifyListeners( IndexingEventListener.EventTypes.SHUTDOWN ); } @@ -440,5 +464,16 @@ public class IndexBuilder extends VitroBackgroundThread { private final List updatedUris = new ArrayList(); private final List deletedUris = new ArrayList(); } + + protected void notifyListeners(IndexingEventListener.EventTypes event){ + for ( IndexingEventListener listener : indexingEventListeners ){ + try{ + if(listener != null ) + listener.notifyOfIndexingEvent( event ); + }catch(Throwable th){ + log.error("problem during NotifyListeners(): " , th); + } + } + } }