Showing posts with label BatchJob. Show all posts
Showing posts with label BatchJob. Show all posts

Batch Job Multi threading D365FO AX7


Multi-threaded batch job



Normally, when we schedule our "Run Base Batch" class as a batch job, it will create a single task. Which will be responsible to execute the complete process in a single thread.

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

In this demo, I will show, How to create a multi-threading Batch job.
following are the steps
Step-1 Create a table for demo purposes in our demo we keep the table name is SLD_DemoTable.





Step-2 Create a class with the name SLD_DemoBusinessLogic and write your business logic in this class.
In the demo, I write only to insert records in a 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 the name SLD_DemoMultiThreadTask.
This class we will use as Task in the main batch job.




Step-4 In this class We Create a getter setter to store vend table buffer;
Step-5 Create an object of our business logic class and execute the 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 the main batch job which will respond to create multiple tasks in the 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 your 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 screenshot 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 issues during the implementation of this blog



Support Faryal's Cusine


Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...