Class BackgroundProcess

java.lang.Object
com.jalios.jcms.backgroundprocess.BackgroundProcess
All Implemented Interfaces:
JcmsConstants, JaliosConstants
Direct Known Subclasses:
AbstractCaddyProcess, AdminSearchEngineProcess, ApplicationDeployProcess, ApplicationResetProcess, CheckIntegrityProcess, LoggingInitPackagesProcess, MemberImportProcess, PublicationClassificationLevelUpdaterProcess, SilentProcess, SilentProgressingProcess, StoreAnonymizerProcess, TrashBulkProcess, UpdatePropertiesProcess, UpdateSiteLanguageProcess

public abstract class BackgroundProcess extends Object implements JcmsConstants
Domain Nomenclature: depending on whether the processes are performed by
  • plugin, use a prefix following the same nomenclature as the plugin properties: examples:
    • plugin.collaborativespace.create-space
    • plugin.newsletter.send-newsletter
    • plugin.jnlp.summarize
  • the JPlatform core, no prefix.
    • core.properties
Since:
jcms-10.0.8 / JCMS-9179
  • Field Details

  • Constructor Details

  • Method Details

    • start

      public final CompletableFuture<Void> start(Member author)
      Start this BackgroundProcess in a new thread.
      Parameters:
      author - author of the process
      Returns:
      a CompletableFuture (never return null)
      Since:
      jcms-10.0.8
    • canStart

      public ControllerStatus canStart(Member author)
      Checks if this process can be started by returning a controller status.

      Prevents unique process to be run simultaneously, depending on author, domain and scope.
      Prevents null-author process to be run, unless it is explicitely allowed using allowNullAuthor();

      Parameters:
      author - author of the process
      Returns:
      a ControllerStatus
      Since:
      jcms-10.0.8
      See Also:
    • save

      protected final void save(boolean forceSave)
      Save BackgroundProcessInfo of current process
      Parameters:
      forceSave - to skip the check of delay between commits
      Since:
      jcms-10.0.8
    • handleComplete

      protected void handleComplete()
      Called after completed.

      Check completedStatus to do custom job.

      Does nothing by default.

      Since:
      jcms-10.0.8
    • canBeNotified

      public boolean canBeNotified(Member member)
      Parameters:
      member - member to be notified
      Returns:
      true if member asked for notification
      Since:
      jcms-10.0.8
    • runProcess

      protected abstract void runProcess() throws BackgroundProcessException
      Override to implement the process.
      Throws:
      BackgroundProcessException - if an error occurs
      Since:
      jcms-10.0.8
    • setCompletedStatus

      protected final void setCompletedStatus(BackgroundProcess.Status status)
      Set the completion status of this BackgroundProcess. You should invoke this method at the end of your process.
      Parameters:
      status - status to set
      Since:
      jcms-10.0.8
    • getCompletedStatus

      public final BackgroundProcess.Status getCompletedStatus()
      Retrieve the completion status of this BackgroundProcess
      Returns:
      the BackgroundProcess status, may return null when BackgroundProcess not yet completed
      Since:
      jcms-10.0.8
    • isCompleted

      public final boolean isCompleted()
      Returns:
      true if process is completed
      Since:
      jcms-10.0.8
    • isCompletedSuccess

      public final boolean isCompletedSuccess()
      Returns true if completedStatus is BackgroundProcess.Status.OK.

      Only test getCompletedStatus(), does not check if completed step was reached.

      Returns:
      true if completedStatus was set to BackgroundProcess.Status.OK
      Since:
      jcms-10.0.8
    • isCompletedWarn

      public final boolean isCompletedWarn()
      Returns true if completedStatus is BackgroundProcess.Status.WARN.

      Only test getCompletedStatus(), does not check if completed step was reached.

      Returns:
      true if completedStatus was set to BackgroundProcess.Status.WARN
      Since:
      jcms-10.0.8
    • isCompletedFailed

      public final boolean isCompletedFailed()
      Returns true if completedStatus is BackgroundProcess.Status.FAILED.

      Only test getCompletedStatus(), does not check if completed step was reached.

      Returns:
      true if completedStatus was set to BackgroundProcess.Status.FAILED
      Since:
      jcms-10.0.8
    • getCompletedMessage

      public final String getCompletedMessage()
      Retrieve the Message that was set at end of BackgroundProcess
      Returns:
      a message localized in default site language
      Since:
      jcms-10.0.8
    • getCompletedMessage

      public final String getCompletedMessage(String lang)
      Retrieve the Message that was set at end of BackgroundProcess
      Parameters:
      lang - an ISO-639 code used to localize the message
      Returns:
      a message localized in default site language
      Since:
      jcms-10.0.8
    • getCompletedMessage

      public final String getCompletedMessage(String lang, boolean useDefault)
      Retrieve a Message indicating BackgroundProcess completion
      Parameters:
      lang - an ISO-639 code used to localize the message
      useDefault - set to true to use a generic completion message if completed message was not set, if false and no message was set, return null
      Returns:
      a message or null
      Since:
      jcms-10.0.8
    • setCompletedMessage

      public final void setCompletedMessage(String completedMessage)
      Set the message property to display to indicate the process has completed
      Parameters:
      completedMessage - an I18N property
      Since:
      jcms-10.0.8
    • getAuthor

      public Member getAuthor()
      Retrieve the member who started this BackgroundProcess
      Returns:
      a Member or null if none specified
      Since:
      jcms-10.0.8
    • isAuthor

      public boolean isAuthor(Member member)
      Check if given member is the process author
      Parameters:
      member - member to check
      Returns:
      true if member is author of the process
      Since:
      jcms-10.0.8
    • allowNullAuthor

      public boolean allowNullAuthor()
      Override to allow a process to be run with no author.
      Returns:
      if process can be run without author
      Since:
      jcms-10.0.8
    • getOriginalContext

      public BackgroundProcess.OriginalContext getOriginalContext()
      Retrieves information about the original context of execution when the process was started.
      Returns:
      an OriginalContext instance if the process was started via an HTTP request, or null otherwise.
      Since:
      jcms-10.0.9 / JCMS-11194
    • getUserLang

      public String getUserLang()
      Retrieve the lang to use for localization
      Returns:
      current userLang if specified, else if process has an author, its language, else channel language
      Since:
      jcms-10.0.8
    • getRunningMessage

      public final String getRunningMessage()
      Returns:
      the runningMessage or null
      Since:
      jcms-10.0.8
    • setRunningMessage

      public final void setRunningMessage(String runningMessage)
      Sets the message that will detail the process at the current step.
      Parameters:
      runningMessage - the runningMessage to set
      Since:
      jcms-10.0.8
    • getDetailsMessage

      public final String getDetailsMessage()
      Returns:
      the detailsMessage or null
      Since:
      jcms-10.0.8
    • setDetailsMessage

      public final void setDetailsMessage(String detailsMessage)
      Sets the message that will detail the process at the end.
      Parameters:
      detailsMessage - the detailsMessage to set
      Since:
      jcms-10.0.8
    • getTotalStep

      public final int getTotalStep()
      Retrieve total number of step that this BackgroundProcess will do before reaching completion.
      Returns:
      a number of step, or 0 if total number of steps is unknown.
    • setTotalStep

      public void setTotalStep(int totalStepNbr)
      Set the total number of step that this BackgroundProcess will do before reaching completion.

      Implementation detail : If this call changes the progress percentage, it triggers an update in database of the BackgroundProcessInfo used to track the process progress

      Parameters:
      totalStepNbr - a number of steps
    • getCurrentStep

      public final int getCurrentStep()
      Retrieve the current step of this processing (from 0 to totalStep).
      Returns:
      the current step number
    • setCurrentStep

      public void setCurrentStep(int stepNbr)
      Set the current step number of this processing.

      Implementation detail : If this step changes the progress percentage, it triggers an update in database of the BackgroundProcessInfo used to track the process progress

      Parameters:
      stepNbr - the step number to set
      Since:
      jcms-10.0.8
    • getStartTime

      protected long getStartTime()
      Returns:
      time when process has started
      Since:
      jcms-10.0.8
    • setStartTime

      protected void setStartTime(long startTime)
      Parameters:
      startTime - time when process has started
      Since:
      jcms-10.0.8
    • incrementStep

      public void incrementStep()
      Increment the step number of this processing by one.

      Implementation detail : If this step changes the progress percentage, it triggers an update in database of the BackgroundProcessInfo used to track the process progress

      Since:
      jcms-10.0.8
    • hasProgression

      public boolean hasProgression()
      Check if this BackgroundProcess has a known total number of steps, allowing precise progression to be provided to end user.
      Returns:
      true if this BackgroundProcess can have a progression status, false if this process is either started or done.
      Since:
      jcms-10.0.8
    • getProgress

      public final int getProgress()
      Retrieve the current progress percentage of this BackgroundProcess
      Returns:
      a percentage (value is updated when current step or total step number are modified)
      Since:
      jcms-10.0.8
    • setProgress

      public void setProgress(int progress)
      Set process progression.

      Progress is limited to max = 100

      Parameters:
      progress - the new progress percentage to set
      Since:
      jcms-10.0.8
    • getLabelProp

      public String getLabelProp()
      Retrieve the I18N property key to use to provide the I18Ned label of this BackgroundProcess to the user.
      Returns:
      a property
      Since:
      jcms-10.0.8
    • setLabelProp

      public void setLabelProp(String prop)
      Set the I18N property key to use to provide the I18Ned label of this BackgroundProcess to the user.
      Parameters:
      prop - a property key
      Since:
      jcms-10.0.8
    • getLabel

      public String getLabel(String lang)
      Retrieve an I18Ned label of this BackgroundProcess, in the specified language.
      Parameters:
      lang - an ISO-639 code
      Returns:
      a label suitable for prestation to the user
      Since:
      jcms-10.0.8
    • getInfoId

      public final String getInfoId()
      Retrieve the id of the BackgroundProcessInfo used to track this BackgroundProcess progression in database.
      Returns:
      a BackgroundProcessInfo id, or null if none was set
      Since:
      jcms-10.0.8
    • getInfo

      public final BackgroundProcessInfo getInfo()
      Retrieve the BackgroundProcessInfo tracking this BackgroundProcess progression in database.
      Returns:
      a BackgroundProcessInfo, loaded from database using current hibernate session/transaction. May return null if none was set or info has already been deleted from database.
      Since:
      jcms-10.0.8
    • getDomain

      public final String getDomain()
      Returns:
      domain of the process
      Since:
      jcms-10.0.8
    • glp

      protected String glp(String prop, Object... params)
    • debugSleep

      protected static void debugSleep(String sleepProperty)
      Parameters:
      sleepProperty - property of a long value to sleep in ms
      Since:
      jcms-10.0.8
    • channel

      protected static Channel channel()
      Returns:
      the channel
      Since:
      jcms-10.0.8
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Since:
      jcms-10.0.8