Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 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;
 
 
 
Abstract long running job.

Author(s):
Philip Helger
 
 public abstract class AbstractLongRunningJob extends AbstractJob implements ILongRunningJob
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (AbstractLongRunningJob.class);

  
Predefined key into the job data map
 
   private static final String KEY_LONG_RUNNING_JOB_ID = "$ph-longrunningjobid";

  
Get the ID of the current user who executes the job.

Parameters:
aJobDataMap The current job data map. Never null.
Returns:
The user ID to be used. May be null but it is recommended to deliver "guest" or the like instead.
 
   @Nullable
   protected abstract String getCurrentUserID (@Nonnull final JobDataMap aJobDataMap);

  

Returns:
The LongRunningJobManager to be used. May not return null.
 
   @Nonnull
   protected abstract LongRunningJobManager getLongRunningJobManager ();
 
   @Override
   protected void beforeExecute (@Nonnull final JobDataMap aJobDataMap)
   {
     final String sUserID = getCurrentUserID (aJobDataMap);
     final String sLongRunningJobID = getLongRunningJobManager ().startJob (thissUserID);
 
     // Store in JobDataMap
     aJobDataMap.put (sLongRunningJobID);
 
     super.beforeExecute (aJobDataMap);
   }
 
   @Override
   protected void afterExecute (@Nonnull final JobDataMap aJobDataMap, @Nonnull final ESuccess eExecSuccess)
   {
     // End long running job before the request scope is closed
     try
     {
       // Get long running job ID from JobDataMap
       final String sLongRunningJobID = aJobDataMap.getString ();
       if (sLongRunningJobID != null)
       {
         // Create the main result
         final LongRunningJobResult aJobResult = createResult ();
         getLongRunningJobManager ().endJob (sLongRunningJobIDeExecSuccessaJobResult);
       }
       else
         .error ("Failed to retrieve long running job ID from JobDataMap " + aJobDataMap);
     }
     catch (final Throwable t)
     {
       .error ("Failed to end long running job"t);
 
       // Notify custom exception handler
       triggerCustomExceptionHandler (tgetClass ().getName (), thistrue);
     }
    super.afterExecute (aJobDataMapeExecSuccess);
  }
New to GrepCode? Check out our FAQ X