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;
Abstract scope aware long running job.

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

  
Predefined key into the job data map
  private static final String KEY_LONG_RUNNING_JOB_ID = "$ph-scope-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.
  protected abstract String getCurrentUserID (@Nonnull final JobDataMap aJobDataMap);

  

Returns:
The LongRunningJobManager to be used. May not return null.
  protected abstract LongRunningJobManager getLongRunningJobManager ();
  protected void beforeExecuteInScope (@Nonnull final JobDataMap aJobDataMap)
  {
    final String sUserID = getCurrentUserID (aJobDataMap);
    final String sLongRunningJobID = getLongRunningJobManager ().startJob (thissUserID);
    // Store in JobDataMap
    aJobDataMap.put (sLongRunningJobID);
  }
  protected void afterExecuteInScope (@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);
    }
  }
New to GrepCode? Check out our FAQ X