Batch Task Dependency in Dynamics AX 2009

Hi,

Scenario : 
You want to schedule 2-3 tasks,  but you would like to define dependency among tasks.
Means Task1 should be executed only after completion of Task 3 and Task 2.


Advantage :
You all might have heard of "Divide and Conquer Rule", So this also implements the same.
Generally if you create batch to process 1000 Purchase orders then it might slow down the AOS performance and delay the overall batch performance.
But if you create batch and create 10 tasks, each process 100 Purchase orders then will be fast compared to above procedure and will improve overall batch performance.

How to :
In Dynamics AX 2009,  Batch process will make use of below tables
  •      BatchJob   : Main Job scheduler, consists of task
  •      BatchTask : Task in batch which specifies what class to execute in order to achieve    certain  functionality.
  •      BatchConstraint : Used to build dependencies among the tasks.

Create a Job, by using below code

static void Batch_CreateDependency(Args _args)
{
    BatchJob                batchJob;
    Batch                   batch;
    Tutorial_runBaseBatch   testDependency;
    BatchConstraints        batchConstraints;
    RefRecId                RecId_Task1, recId_task2, recId_task3;
   
    // Create task
    RefRecId createTask(str _msg)
    {
        ;
        batch.clear();
        batch.initValue();
        batch.GroupId           = '';
        batch.BatchJobId        = batchJob.RecId;
        batch.RunType           = BatchRunType::Server;
        batch.RetriesOnFailure  = 0;
        batch.AutomaticTransaction = NoYes::No;
        batch.Caption           = _msg;
        batch.Status            = BatchStatus::Waiting;
        batch.Company           = curext();
        testDependency          = Tutorial_runBaseBatch::construct();
        batch.ClassNumber       = classidget(testDependency);
        batch.insert();
       
        return batch.RecId;
    }
   
    // Building dependencies
    void buildDependency(RefRecId _batchId, RefRecid _dependendRecId)
    {
        ;
        batchConstraints.clear();
        batchConstraints.initValue();
        batchConstraints.BatchId = _batchId;
        batchConstraints.DependsOnBatchId = _dependendRecId;
        batchConstraints.ExpectedStatus = BatchDependencyStatus::FinishedOrError;
        batchConstraints.insert();
    }
   
    ;

    batchJob.clear();
    batchJob.initValue();
    batchJob.OrigStartDateTime = DateTimeUtil::newDateTime(systemDateGet(), DateTimeUtil::time(DateTimeUtil::utcNow()) + 10);
    batchJob.Status = BatchStatus::Waiting;
    batchJob.Caption = "Test Batch Dependency";
    batchJob.insert();

    recId_Task1 = createTask("Task 1");
    recId_Task2 = createTask("Task 2");
    recId_Task3 = createTask("Task 3");
   
    buildDependency(recId_Task1, recId_Task2);
    buildDependency(recId_Task1, recId_Task3);
   
    info(strfmt("The %1 Job scheduled to run 3 tasks", batchJob.Caption));
}

When you execute this Job, Go to Basic-->Inquiries-->Batch Job and you should see as below,

As per the snap, you can see Task 1 will be executed once Task 2 and Task 3 gets completed.

Enjoy Multi Threading  :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.