Using a JobStarter
The Summer.Batch.Core.JobStarter
is a facility to help users write their entry points. It contains two public methods: Start and Restart, both taking a job XML file path and a UnityLoader implementation as parameters.
Writing a correct job XML configuration is explained in chapter 4.
The JobStarter can be easily used in a typical Main entry point method :
Example 5.1. Sample entry point method using JobStarter
¶
using Summer.Batch.Core;
using System;
using System.Diagnostics;
namespace Com.Netfective.Bluage.Batch.Jobs
{
/// <summary>
/// Class for launching the restart sample job.
/// To restart use -restart as the first argument of the Main function.
/// </summary>
public static class RestartSampleLauncher
{
public static int Main(string[] args)
{
#if DEBUG
var stopwatch = new Stopwatch();
stopwatch.Start();
#endif
JobExecution jobExecution;
if (args != null && args.Length >= 1)
{
// restarting a job is handled through an argument. Using '-restart' is
// the only permitted argument for the entry point.
if (args[0] != "-restart")
{
Console.WriteLine("Unknown option :["+args[0]+"]. Only option is -restart");
return (int) JobStarter.Result.InvalidOption;
}
jobExecution = JobStarter.ReStart(@"Batch\Jobs\restart_sample_job.xml",
new RestartSampleUnityLoader());
}
else
{
jobExecution = JobStarter.Start(@"Batch\Jobs\restart_sample_job.xml",
new RestartSampleUnityLoader());
}
#if DEBUG
stopwatch.Stop();
Console.WriteLine(Environment.NewLine + "Done in {0} ms.",
stopwatch.ElapsedMilliseconds);
Console.WriteLine("Press a key to end.");
Console.ReadKey();
#endif
//returns a integer code value; clients using this Main
//will be aware of the batch termination result.
return (int) (jobExecution.Status == BatchStatus.Completed ?
JobStarter.Result.Success
: JobStarter.Result.Failed);
}
}
JobStarter Methods.¶
Code snippet above displays two typical uses of JobStarter
class, with Start
and Restart
methods. In addition to these, two other methods exist that enable to stop or abandon a current job execution.
-
Start
: Enables to start a new job execution for the specified job; -
Restart
: Enables to restart a failed job execution. This requires that a failed or stopped job execution exists for the job in parameter. If several such executions exist, last one will be restarted; -
Stop
: Stops a running job execution for the job in parameter. This requires that such a running execution exists. If several running job executions are found for the specified job, they will all be stopped; -
Abandon
: Abandons a stopped job execution for the job in parameter. This requires that such a stopped execution exists. If several stopped executions are found, they will all be abandoned. While a stopped job may be restarted, and abandoned one cannot.
Note
Restart
, Stop
and Abandon
all require a persisted job repository to operate.
Fine grained control over job executions.¶
As seen above, in the JobStarter
class, Restart
restarts the last failed or stopped job execution, while Stop
and Abandon
operate on all eligible executions. In order to get a fine grained control over job executions and be able to restart, stop or abandon a specific one, key class to use is Summer.Batch.Core.Launch.BatchRuntime
. Given a job XML file path and IUnityContainer implementation, one can get the corresponding job operator (A class that implements Summer.Batch.Core.Launch.IJobOperator
interface) using GetJobOperator(UnityLoader loader, XmlJob job) method.
The IJobOperator
holds all needed operations to get a full control over job executions. In particular, Restart
, Stop
and Abandon
methods all take an executionId as a parameter.
-
long? Start(string jobName, string parameters)
: Starts a new job execution with a job name and a list of parameters (comma (or newline) separated 'name=value' pairs string); -
long? Restart(long executionId)
: Restart a failed job execution corresponding to the provided executionId; -
bool Stop(long executionId)
: Stops a running job execution corresponding to the provided executionId; -
JobExecution Abandon(long jobExecutionId)
: Abandons a stopped job execution corresponding to the provided executionId.
Note
In most cases, such a direct use of job operator is not needed.