SysOperation Multithreading - AX 2012

//This is a service class which runs controller that has contract( with query as a parameter) , service class and that can be multi threaded to run parallel process.
multi threading will improve the performance when you process larger number of records and consume less time than the normal sysoperation controller class process.
public class afmFieldsToProductAttributeBatchService extends SysOperationServiceBase


//Entry Point for Sys Operation
[SysEntryPointAttribute (true),
AifCollectionTypeAttribute('afmFieldsToProductAttributeContract', Types::Class, classStr(afmFieldsToProductAttributeContract)) ]
public void processFieldtoAttributeConfig(afmFieldsToProductAttributeContract _afmFieldsToProductAttributeContract)
{
    BatchHeader                                 mBatchHeader;
    SysOperationServiceController               mTaskController;
    afmFieldsToProductAttributeContract         mTaskContract;
    DictEnum                                    dataType;
    int                                         i;
    boolean                                     runMultiThreading = _afmFieldsToProductAttributeContract.parmMultithreading();
    ;

    if(this.isExecutingInBatch())
    {
        dataType = new DictEnum(enumName2Id('AttributeDataType'));

        for (i= 0; i < dataType.values(); i++)
        {
            if(dataType.index2Label(i) == enum2str(AttributeDataType::Reference))
                continue;

            // Create a service controller to run the task for processing on product attributes
            mTaskController = new SysOperationServiceController(classStr(afmFieldsToProductAttributeService), methodStr(afmFieldsToProductAttributeService, ProcessFieldToAttributeMapping));

            // Fetch the data contract from within the controller
            mTaskContract = mTaskController.getDataContractObject('_afmFieldsToProductAttributeContract');
            mTaskContract.setQuery(_afmFieldsToProductAttributeContract.getQuery());

            if(!mBatchHeader)
            {
                mBatchHeader = BatchHeader::getCurrentBatchHeader();
            }

            if(runMultiThreading == boolean::true)
            {
                // Put the current data type in the controller's data contract
                mTaskContract.parmAttributeDataTypeValue(dataType.index2Value(i));
            }

            // Create a runTimeTask within the current batch job
            mBatchHeader.addRuntimeTask(mTaskController, this.getCurrentBatchTask().RecId);

            // If we're processing in batch, then save the batch header
            if(mBatchHeader)
            {
                mBatchHeader.save();

                if(runMultiThreading == boolean::false)
                    break;
            }
        }
    }
    else
    {
        // Create a service controller to run the task for processing on product attributes
        mTaskController = new SysOperationServiceController(classStr(afmFieldsToProductAttributeService), methodStr(afmFieldsToProductAttributeService, ProcessFieldToAttributeMapping));

        // Fetch the data contract from within the controller
        mTaskContract = mTaskController.getDataContractObject('_afmFieldsToProductAttributeContract');

        mTaskContract.setQuery(_afmFieldsToProductAttributeContract.getQuery());
        mTaskController.parmExecutionMode(SysOperationExecutionMode::Synchronous);

        mTaskController.run();
    }
}


Comments