ckangtai

AX2009 批处理作业中使用多线程---批量捆绑

批量捆绑
   由于Ax服务器中批处理线程是可以多个的,而实际批处理作业中线程往往只使用了一个


 Class:

/*
批量捆绑
*/
/*class Code*/
public class DemoBatchBundles extends RunBaseBatch
{
    str 20 fromSalesOrder, toSalesOrder;
    #define.CurrentVersion(1)
    #localmacro.CurrentList
        fromSalesOrder, toSalesOrder
    #endmacro
}

public void new()
{
    super();
}

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

}

private void parmEndBlock( str _toSalesOrder)
{
    toSalesOrder = _toSalesOrder;
}

private void parmStartBlock(str _fromSalesOrder)
{
    fromSalesOrder= _fromSalesOrder;
}


void run()
{
    SalesTable                          salesTable;
    SalesFormLetter                     formletter;
    Map                                 SalesMap;
    ;

    info(fromSalesOrder+':'+toSalesOrder);

    /* Each task knows the range of work items it needs to process. This range information is already packed when the task is created */
    while select * from salesTable
    where salesTable.salesId >= fromSalesOrder
            && salesTable.salesId <= toSalesOrder
            && salesTable.documentStatus == DocumentStatus::none
    {
        formletter = SalesFormLetter::construct(DocumentStatus::Invoice);
        formletter.getLast();
        formletter.resetParmListCommonCS();
        formletter.allowEmptyTable(formletter.initAllowEmptyTable(true));
        SalesMap = new Map(Types::Int64,Types::Record);
        SalesMap.insert(salesTable.recid,salesTable);
//        formletter.parmDataSourceRecordsPacked(SalesMap.pack());
//        formletter.createParmUpdateFromParmUpdateRecord(SalesFormletterParmData::initSalesParmUpdateFormletter(DocumentStatus::Invoice, FormLetter.pack()));
        formletter.showQueryForm(false);
        formletter.initLinesQuery();
        formletter.update(salesTable, systemDateGet(), SalesUpdate::All, AccountOrder::None, false, false);
    }
}

public boolean unpack(container packedClass)
{
    Version version = RunBase::getVersion(packedClass);
    switch(version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            break;
        default:
            return false;
    }
    return true;
}


public static DemoBatchBundles construct(str _fromSalesOrder, str _toSalesOrder)
{
    DemoBatchBundles  c;
    ;
    c = new DemoBatchBundles();
    c.parmStartBlock(_fromSalesOrder);
    c.parmEndBlock(_toSalesOrder);
    return c;
}

  

 Job 测试

/* Here tasks are created to process work items equivalent to the bundle size.
   The range between the fromSalesOrder and toSalesOrder is a bundle of work items.
    */

static void scheduleDemoBundlesJob(Args _args)
{
    int                             blockSize=30; //My bundle size
    BatchHeader                     batchHeader;
    DemoBatchBundles                demoBatchBundles;
    SalesTable                      salesTable;
    str                             fromSalesOrder, toSalesOrder;
    str                             lastSalesId;
    BatchInfo                       batchInfo;
    int                             Counter=0;

    SalesId                         _SalesIdFrom,_SalesIdTo;
    ;


    _SalesIdFrom = "SSO000001";
    _SalesIdTo = "SSO001000";

    ttsBegin;

    select count(RecId) from salesTable
        where salesTable.salesId >= _SalesIdFrom &&
            salesTable.salesId <= _SalesIdTo   &&
            salesTable.documentStatus == DocumentStatus::none;

    info(Strfmt("%1",salesTable.recid ));

    if (salesTable.recid > 0)
    {
        batchHeader = BatchHeader::construct();
        batchHeader.parmCaption(strFmt('Batch job for DemoBundlesBatch Invoice SalesOrders %1 thru %2', 'SO-00400001', 'SO-00500000'));

        while select salesid from salesTable
            order by salesTable.SalesId
            where salesTable.salesId >= _SalesIdFrom &&
                salesTable.salesId <= _SalesIdTo   &&
                salesTable.documentStatus == DocumentStatus::none
        {


            Counter += 1;
            if (Counter ==1)
            {
                fromSalesOrder = salesTable.salesid;
            }
            if (Counter == blockSize)
            {
                toSalesOrder = salesTable.salesid;

                /* Each task is created to process a bundle of work items (in this case a range of sales Orders)*/

                demoBatchBundles = DemoBatchBundles::construct(fromSalesOrder, toSalesOrder);
                info(fromSalesOrder+' : ' + toSalesOrder);
                batchInfo = DemoBatchBundles.batchInfo();
                BatchInfo.parmCaption(fromSalesOrder+' : ' + toSalesOrder);
                batchInfo.parmGroupId("COUNTING");
                batchHeader.addTask(demoBatchBundles);
                Counter = 0;
            }
            lastSalesId = salesTable.SalesId;
        }

        // This is to handle the spillover
        // #SalesOrders in this last bundle will be less than the bundle size

        if (Counter > 0)
        {
            toSalesOrder = lastSalesId;
            demoBatchBundles = DemoBatchBundles::construct(     fromSalesOrder, toSalesOrder);
            info(fromSalesOrder+' : ' + toSalesOrder);
            batchInfo = DemoBatchBundles.batchInfo();
            BatchInfo.parmCaption(fromSalesOrder+' : ' + toSalesOrder);
            batchInfo.parmGroupId("COUNTING");
            batchHeader.addTask(demoBatchBundles);
        }

        batchHeader.save();
    }
    ttsCommit;
    info('Done');
}
/*
批量捆绑

假设,我们处理10万条记录

#已创建任务    #批处理线程(在我的测试服务器)    #可并行执行的并行任务
100            10                                  10

前10个任务完成后,批处理框架将加载下10个任务并执行,以此类推;
在这种情况下,可能需要加载10次或更多次。
*/

  

posted on 2018-09-12 09:05  ckangtai  阅读(331)  评论(0编辑  收藏  举报

导航