Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.appbasics.longrun;
 
 import java.util.Map;
 
 
 
 
 public final class LongRunningJobManager
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (LongRunningJobManager.class);
 
   private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock ();
   @GuardedBy ("m_aRWLock")
   private final Map <StringLongRunningJobDatam_aRunningJobs = new HashMap <StringLongRunningJobData> ();
 
   public LongRunningJobManager (@Nonnull final LongRunningJobResultManager aResultMgr)
   {
      = ValueEnforcer.notNull (aResultMgr"ResultMgr");
   }

  
Start a long running job

Parameters:
aJob The job that is to be started
sStartingUserID The ID of the user who started the job
Returns:
The internal long running job ID
 
   @Nonnull
   @Nonempty
   public String startJob (@Nonnull final ILongRunningJob aJob, @Nullable final String sStartingUserID)
   {
     ValueEnforcer.notNull (aJob"Job");
 
     // Create a new unique in-memory ID
     final String sJobID = GlobalIDFactory.getNewStringID ();
     .writeLock ().lock ();
     try
     {
       .put (sJobIDnew LongRunningJobData (sJobIDaJob.getJobDescription (), sStartingUserID));
       return sJobID;
     }
     finally
     {
       .writeLock ().unlock ();
     }
   }

  
End a job.

Parameters:
sJobID The internal long running job ID created from startJob(com.helger.schedule.longrun.ILongRunningJob,java.lang.String).
eExecSucess Was the job execution successful or not from a technical point of view? May not be null. If a JobExecutionException was thrown, this should be com.helger.commons.state.ESuccess.FAILURE.
aResult The main job results.
 
  public void endJob (@Nullable final String sJobID,
                      @Nonnull final ESuccess eExecSucess,
                      @Nonnull final LongRunningJobResult aResult)
  {
    ValueEnforcer.notNull (eExecSucess"ExecSuccess");
    ValueEnforcer.notNull (aResult"Result");
    // Remove from running job list
    LongRunningJobData aJobData;
    .writeLock ().lock ();
    try
    {
      aJobData = .remove (sJobID);
      if (aJobData == null)
        throw new IllegalArgumentException ("Illegal job ID '" + sJobID + "' passed!");
      // End the job - inside the writeLock
      aJobData.onJobEnd (eExecSucessaResult);
    }
    finally
    {
      .writeLock ().unlock ();
    }
    // Remember it
    .addResult (aJobData);
  }
  public int getRunningJobCount ()
  {
    .readLock ().lock ();
    try
    {
      return .size ();
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newList (.values ());
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  {
    // Wait until all long running jobs are finished
    int nWaitSeconds = 1;
    while (true)
    {
      final int nRunningJobCount = getRunningJobCount ();
      if (nRunningJobCount == 0)
        break;
      .error ("There are still " +
                       nRunningJobCount +
                       " long running jobs in the background! Waiting for them to finish...");
      // Wait some time
      Thread.sleep (nWaitSeconds * .);
      if (nWaitSeconds < 10)
        nWaitSeconds++;
    }
  }
New to GrepCode? Check out our FAQ X