Writing an Abortable Job

Because job execution may take a lot of time, you may wish to abort a job. To do this, add a code snippet for checking if you wish to abort a job. Because you can not easily and safely abort a JobPerformable at any time unless you kill the thread, it is necessary to place hooks for the abort before stages that you expect to be time-consuming.

The tutorial uses the job created in the HelloWorldCronJob Tutorial that is adequately adjusted as described below.

Adjusting MyJobPerformable

The execution of MyJobPerformable from HelloWorldCronJob Tutorial does not take a lot of time. Let us modify the job to extend its execution time by adding a for loop:

MyJobPerformable.java

public class MyJobPerformable extends AbstractJobPerformable<HelloWorldCronJobModel>
{
  private L10NService l10nService;

  @Required
  public void setL10nService(final L10NService l10nService)
  {
    this.l10nService = l10nService;
  }

  @Override
  public PerformResult perform(final HelloWorldCronJobModel cronJob)
  {

    for (int i = 0; i <= 1000; i++)
    {
      try
      {
        System.out.println("Greeting '" + l10nService.getLocalizedString(cronJob.getMessage())
	     		+ "' from MyJobPerformable for " + i + " times.");
	Thread.sleep(5000);
      }
      catch (final InterruptedException e)
      {
        Thread.currentThread().interrupt();
      }
    }
    //the following will be executed when the loop is finished
    return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
  }

}

The job displays the message in a console:

Greeting 'Hallo' from MyJobPerformable for 1 times.
Greeting 'Hallo' from MyJobPerformable for 2 times.
Greeting 'Hallo' from MyJobPerformable for 3 times.
Greeting 'Hallo' from MyJobPerformable for 4 times.
Greeting 'Hallo' from MyJobPerformable for 5 times.
	...

After 5000 seconds the job succeeds and the following message is displayed in the console:

Greeting 'Hallo' from MyJobPerformable for 5000 times.

Overriding isAbortable Method

By default each job is not abortable. To enable the abort feature, you should override the isAbortable method in MyJobPerformable:

MyJobPerformable.java

@Override
public boolean isAbortable()
{
  return true;
}

Alternatively, if you extend the AbstractJobPerformable, you may override a bean property value in Spring configuration:

cronjobtutorial-spring.xml

<bean id="myJobPerformable" class="de.hybris.cronjobtutorial.MyJobPerformable" 
parent="abstractJobPerformable" >
  <property name="l10nService" ref="l10nService" />
  <property name="abortable" value="true"/>
</bean>

Checking Abort Request

Currently the job is marked as abortable, which allows to activate the abort flag for the cron job, similarly to the interrupt flag for Thread. When implementing an abortable job it is important to regularly check if the user has requested to abort the job. If true, the abort flag is set, the job should be stopped, and the cron job should have a proper status and result. You should decide when to check the flag and what to do if the flag is set, for example, to clean up. Time of processing between verifications should be moderate.

The following code sample calls the clearAbortRequestedIfNeeded of the AbstractJobPerformable. It checks if the given cronJob is requested to be aborted and has REQUESTABORT flag set to true. If so, the flag is set to false again and the job execution is prematurely stopped.

MyJobPerformable.java

if(clearAbortRequestedIfNeeded(cronJob))
{
   //abort the job 
   
   //do some clean-up
  
  return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
}

After including the if condition in perform method, the MyJobPerformable looks as follows:

MyJobPerformable.java

public class MyJobPerformable extends AbstractJobPerformable<HelloWorldCronJobModel>
{
	private L10NService l10nService;

	@Required
	public void setL10nService(final L10NService l10nService)
	{
		this.l10nService = l10nService;
	}

	@Override
	public PerformResult perform(final HelloWorldCronJobModel cronJob)
	{

        for (int i = 0; i <= 1000; i++)
	{
		try
		{
			System.out.println("Greeting '" + l10nService.getLocalizedString(cronJob.getMessage())
					+ "' from MyJobPerformable  for " + i + " times.");
			Thread.sleep(5000);
		
			if (clearAbortRequestedIfNeeded(cronJob))
			{
				System.out.println("The job is aborted.");
				return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
			}
		}
		catch (final InterruptedException e)
		{
                        Thread.currentThread().interrupt();
		}
	 }

	return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
	}

	@Override
	public boolean isAbortable()
	{
		return true;
	}

}

Aborting the Job

To abort the job, you should call the method:

cronJobService.requestAbortCronJob(cronJob);

The cron job results then with ERROR, its status is set to ABORTED and the following message is displayed in the console:

The job is aborted.