Tuesday, July 31, 2018

Batch Multi threading D365FO AX7


Batch Multi threading D365FO AX7



Normally, when we schedule our RunBaseBatch class as a batch job, it will create single task. Which will responsible to execute complete process in single thread.


But, As you know like AX-2009 and AX-2012. In D365FO We can add multiple AOS server in single batch group and they can process the multiple task simultaneously. 


In this demo I will show, How to create multi threading Batch job.

following are the steps

Step-1 Create a table for demo purpose in our demo we keep table name is SLD_DemoTable.




Step-2 create a class with name SLD_DemoBusinessLogic and write your business logic in this class.

In demo I write only insert records in table and show you guys how it works.





Reference Code
class SLD_DemoBusinessLogic
{

    public void processInit(VendTable _vend)
    {
        SLD_DemoTable   SLDDemoTable;

        SLDDemoTable.clear();
        SLDDemoTable.VendName=_vend.name();
        SLDDemoTable.VendAccount=_vend.AccountNum;
        SLDDemoTable.insert();

    }

}

Step-3 create a batch job class with name SLD_DemoMultiThreadTask.
This class we will use as Task in main batch job.




Step-4 In this class We Create a getter setter to store vend table buffer;
Step-5 Create object of our business logic class and execute method


Reference Code
public class SLD_DemoMultiThreadTask extends RunBaseBatch
{
  
  VendTable ventTableBuffer;
    #DEFINE.CurrentVersion(1)
    #LOCALMACRO.CurrentList
        ventTableBuffer
    #ENDMACRO

        public ClassDescription caption()
       
        {
              ClassDescription ret;
          
              ret = 'Update Multiple Thread task';
          
              return ret;
          
        }

    container pack()
    {
        return [#CurrentVersion,#CurrentList];
    }

    public boolean unpack(container _packedClass)
    {
        int version = conPeek(_packedClass,1);
   
        switch (version)
        {
            case #CurrentVersion:
                [version,#CurrentList] = _packedClass;
                break;
            default:
                return false;
        }
        return true;
    }

    public void pramVendBuffer(VendTable _vend)
    {
        ventTableBuffer=_vend;
    }

    public void run()
    {
        try
        {
            SLD_DemoBusinessLogic       SLDDemoBusinessLogic=new SLD_DemoBusinessLogic();

            SLDDemoBusinessLogic.processInit(ventTableBuffer);
     
        }
        catch
        {
            info(strFmt("%1",xSession::xppCallStack()));
        }
    }

}

Step-6 Now Create main batch job which will responsible to create multiple task in single batch job.




Reference Code
class SLD_DemoMultiThreadBatch extends RunBaseBatch
{
    BatchHeader                             batchHeader;

    VendTable        vendTable;
 

    public static void main(Args args)
    {
        SLD_DemoMultiThreadBatch  objMultiThread = new SLD_DemoMultiThreadBatch();

        if (objMultiThread.prompt())
        {
            objMultiThread.run();
        }
    }

    void run()
    {
        SLD_DemoMultiThreadTask                 SLDDemoMultiThreadTask;
       

        try
        {
          
            while select firstonly10 vendTable
            {
                if(this.isInBatch())
                {
                    if(!batchHeader)
                    {
                        batchHeader = BatchHeader::construct(this.parmCurrentBatch().BatchJobId);
                    }
                    batchHeader.parmCaption("Multiple thread batch jobs");
                    SLDDemoMultiThreadTask =  new SLD_DemoMultiThreadTask();
                    SLDDemoMultiThreadTask.pramVendBuffer(vendTable.data());
                    // add tasks to the batch header
                    batchHeader.addRuntimeTask(SLDDemoMultiThreadTask, this.parmCurrentBatch().RecId);
                }

                else
                {

                    // execute your code here when not running in batch
                }
            }
            if(batchHeader)
            {
                // save the batchheader with added tasks
                batchHeader.save();
            }

        }
        catch
        {
            info(strFmt("%1",xSession::xppCallStack()));
        }
    }

    public container pack()

    {

        return conNull();

    }

    public boolean unpack(container packedClass)

    {

        return true;

    }

}


Step-7 Now create an Action menu item and reference our batch job SLD_DemoMultiThreadBatch .




Step-8 Build and sync database and module and run the job.
Step-9 Navigate to you batch job page and find your job and check Job Task.





Step-10  before execution of the batch job check the demo table. 
you can check the below screen shot currently table is empty.


After execution of the job table filled with vendors names.



Please feel free to contact me if you are facing any issue during implementation of this blog



2 comments:

  1. Can you please provide the same logic using Sysoperation Framework in D 365 F&O

    ReplyDelete
  2. can you please provide the same logic for sysoperation frame work

    ReplyDelete