Class GenericSearchIndexMasterProcessLauncher<T>

  • Type Parameters:
    T - The type that will be read by the QueueLoader and put on the ReindexQueue.
    All Implemented Interfaces:
    IndexableTypeAware, SearchIndexMasterProcessLauncher, Runnable

    public abstract class GenericSearchIndexMasterProcessLauncher<T>
    extends Object
    implements SearchIndexMasterProcessLauncher, Runnable
    This is an abstract component to allow for the full reindexing of a specific index. This component only operates as a controller for a multi-threaded process. The purpose of this is to obtain a process lock. It then runs a QueueLoader in another thread, which begins to populate a Queue. It creates a Semaphore to block until all background threads are complete. It then raises a process started event. Finally, it waits until the semaphore releases and completes the process.
    Author:
    Kelly Tisdell (ktisdell)
    • Method Detail

      • instance

        public static SearchIndexMasterProcessLauncher instance​(com.broadleafcommerce.search.api.type.IndexableType type)
        Convenient way to access the SearchIndexMasterProcessLauncher associated with a particular IndexableType. Returns null if none was registered for the specified type.
        Parameters:
        type - the IndexableType for which to get the registered master launcher
        Returns:
        the registered master launcher for the type, or null if none was registered
      • registerSearchIndexMasterProcessLauncher

        public static void registerSearchIndexMasterProcessLauncher​(@NonNull
                                                                    @NonNull SearchIndexMasterProcessLauncher launcher)
        This is a static method to register new SearchIndexMasterProcessLauncher objects. Note that you do not need to register your SearchIndexMasterProcessLauncher if it extends this GenericSearchIndexMasterProcessLauncher as the constructor registers the instance automatically.
        Parameters:
        launcher - the master process launcher to register for its IndexableType
      • rebuildIndex

        public final void rebuildIndex()
                                throws com.broadleafcommerce.search.api.SearchIndexException
        Description copied from interface: SearchIndexMasterProcessLauncher
        Rebuilds an entire index. Index is typically Solr or ElasticSearch, but could be any type of search-able index. This is the entry point for rebuilding an index, and is often run in a background thread.
        Specified by:
        rebuildIndex in interface SearchIndexMasterProcessLauncher
        Throws:
        com.broadleafcommerce.search.api.SearchIndexException - if an error occurs preventing indexing
      • rebuildIndex

        public final void rebuildIndex​(@Nullable
                                       Map<String,​Serializable> ctx)
                                throws com.broadleafcommerce.search.api.SearchIndexException
        The default behavior here is check whether this process is already running, and if not, delegate to a background thread.
        Specified by:
        rebuildIndex in interface SearchIndexMasterProcessLauncher
        Parameters:
        ctx - arbitrary contextual information to be passed to the process launcher
        Throws:
        com.broadleafcommerce.search.api.SearchIndexException - if an error occurs preventing indexing
      • run

        public final void run()
        Specified by:
        run in interface Runnable
      • getElapsedTime

        public long getElapsedTime()
        Description copied from interface: SearchIndexMasterProcessLauncher
        Returns the elapsed time in millis, or -1 if the system is not running or if this is not the master instance.
        Specified by:
        getElapsedTime in interface SearchIndexMasterProcessLauncher
        Returns:
        the elapsed time in millis, or -1 if the system is not running or if this is not the master instance
      • executeInternally

        protected final void executeInternally()
        Method in this class that does the heavy lifting. However, for the most part, this delegates to the preProcess, process, and postProcess methods.

        This method is responsible for obtaining (and removing) necessary locks and for starting/ending the process state.

        It is not recommended that you override this method. Instead, implement or override the preProcess, process, and postProcess methods.

      • destroyQueue

        protected void destroyQueue​(@Nullable
                                    ReindexQueue<T> queue)
        Attempts to destroy the queue. If failure occurs, an error is logged.
        Parameters:
        queue - The queue to destroy.
      • addStateHolderProperties

        protected void addStateHolderProperties​(ProcessStateHolder stateHolder)
        Adds properties set on the context map to the ProcessStateHolder. Any properties set on the context are subsequently cleared.
        Parameters:
        stateHolder - The ProcessStateHolder to set properties on
      • process

        protected long process​(ReindexQueue<T> queue,
                               ProcessStateHolder stateHolder)
                        throws com.broadleafcommerce.search.api.SearchIndexException
        Method to populate the ReindexQueue with data. This should be a synchronous call (executing on the current thread) and should return the number of IDs (or records) that were put on the Queue for processing.
        Parameters:
        queue - the queue to populate
        stateHolder - the state holder tracking this process
        Returns:
        the number of IDs that were read and put on the queue
        Throws:
        com.broadleafcommerce.search.api.SearchIndexException - if the queue was unable to be populated
      • pause

        protected void pause()
        Pause this thread for the determined completionPauseTime. Allows other threads to complete their work if necessary.
      • getLockService

        protected LockService getLockService()
      • getBatchIdReader

        protected QueueLoader<T> getBatchIdReader()
      • getCompletionPauseTime

        protected long getCompletionPauseTime()
        This is the time that the main control thread will pause on indexing process completion to allow other threads to finish what they are doing or catch up.
        Returns:
        time to wait on indexing completion for other threads finishing