CL_ABAP_PARALLEL :并行处理模板

class CL_ABAP_PARALLEL definition
  public
  abstract
  create public .

  public section.

    types:
      T_IN_TAB type standard table of XSTRING with non-unique default key .
    types:
      begin of T_OUT,
        RESULT  type XSTRING,
        INDEX   TYPE INT4,
        TIME    type INT4,
        MESSAGE type STRING,
      end   of T_OUT .
    types:
      T_OUT_TAB type standard table of T_OUT with non-unique default key .

    methods CONSTRUCTOR
      importing
        !P_NUM_TASKS           type I default 10
        !P_TIMEOUT             type I default 200
        !P_PERCENTAGE          type I default 50
        value(P_NUM_PROCESSES) type I default 20 .
    methods RUN
      importing
        !P_IN_TAB  type T_IN_TAB
      exporting
        !P_OUT_TAB type T_OUT_TAB .
    methods DO
          abstract
      importing
        !P_IN  type XSTRING
      exporting
        !P_OUT type XSTRING .
    class-methods END_TASK
      importing
        !P_TASK type CLIKE .
  protected section.
  private section.

    types:
      begin of T_TASK,
        NAME  type SYCHAR32,
        START type SYUZEIT,
      end   of T_TASK .

    class-data:
      TASKS   type hashed table of T_TASK with unique key NAME .
    class-data OUT_TAB type T_OUT_TAB .
    data TIME_OUT type I .
    data NUM_PROCESSES type I .

    class-methods GET_NUMBER_OF_PROCESSES
      importing
        !P_PERCENTAGE   type I
        !P_NUM_TASKS    type I
      returning
        value(P_RESULT) type I .
    class-methods GET_NUMBER_OF_FREE_PROCESSES
      returning
        value(P_RESULT) type I .
ENDCLASS.

CLASS CL_ABAP_PARALLEL IMPLEMENTATION.


  method CONSTRUCTOR.
    TIME_OUT  = P_TIMEOUT.
    if P_NUM_TASKS is supplied.
      P_NUM_PROCESSES = P_NUM_TASKS.
    endif.
    NUM_PROCESSES = GET_NUMBER_OF_PROCESSES( P_NUM_TASKS = P_NUM_PROCESSES P_PERCENTAGE = P_PERCENTAGE ).

  endmethod.


  method END_TASK.
    data:
      L_OUT       like line of OUT_TAB,
      L_TASK_NAME type SYCHAR32,
      L_MESSAGE   type SYCHAR80 ##NEEDED.
    field-symbols:
      <L_TASK>   like line of TASKS[].

    receive results from function 'RS_ABAP_PARALLEL'
      importing
        P_OUT                 = L_OUT-RESULT
      exceptions
        COMMUNICATION_FAILURE = 1 message L_MESSAGE
        SYSTEM_FAILURE        = 2 message L_MESSAGE.

    L_OUT-MESSAGE = L_MESSAGE.
    read table TASKS[] with table key NAME = P_TASK assigning <L_TASK> .
    get time.
    L_OUT-TIME  = SY-UZEIT - <L_TASK>-START.
    L_OUT-INDEX = P_TASK.
    L_TASK_NAME = P_TASK.
    delete TASKS[] where NAME = L_TASK_NAME.
    append L_OUT to OUT_TAB.

  endmethod.


  method GET_NUMBER_OF_FREE_PROCESSES.
    constants:
      C_OPCODE_WP_GET_INFO type SYHEX01 value 25.

    call 'ThWpInfo' id 'OPCODE'     field C_OPCODE_WP_GET_INFO
                    id 'FREE_DIAWP' field P_RESULT.

  endmethod.


  method GET_NUMBER_OF_PROCESSES.
    constants:
      C_OPCODE_WP_GET_INFO type SYHEX01 value 25.
    data:
      L_MAX_PROCS        type I,
      L_NUM_DIA_WPS      type I,
      L_NUM_FREE_DIA_WPS type I.

    call 'ThWpInfo' id 'OPCODE'     field C_OPCODE_WP_GET_INFO
                    id 'DIAWP'      field L_NUM_DIA_WPS
                    id 'FREE_DIAWP' field L_NUM_FREE_DIA_WPS.

    L_MAX_PROCS = L_NUM_DIA_WPS.

    P_RESULT = P_PERCENTAGE * L_MAX_PROCS / 100.

    if P_RESULT < 1.
      P_RESULT = 1.
    endif.

    if P_RESULT > P_NUM_TASKS.
      P_RESULT = P_NUM_TASKS.
    endif.

  endmethod.


  method RUN.

    data:
      L_DESCR_REF_CLASS type ref to CL_ABAP_CLASSDESCR.

    L_DESCR_REF_CLASS ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_OBJECT_REF( ME ).
    data(L_CLASS) = L_DESCR_REF_CLASS->ABSOLUTE_NAME.
    clear TASKS.

    loop at P_IN_TAB assigning field-symbol(<L_IN>).

      data:
        L_TASK    like line of TASKS,
        L_MESSAGE type SYCHAR80 ##NEEDED.

      L_TASK-START = SY-UZEIT.
      L_TASK-NAME  = SY-TABIX.

      call function 'RS_ABAP_PARALLEL'
        starting new task L_TASK-NAME
        "DESTINATION IN GROUP DEFAULT
        calling END_TASK on end of task
        exporting
          P_CLASS               = L_CLASS
          P_IN                  = <L_IN>
        exceptions
          COMMUNICATION_FAILURE = 1 message L_MESSAGE
          SYSTEM_FAILURE        = 1 message L_MESSAGE.

      if SY-SUBRC = 0.
        insert L_TASK into table TASKS.
      endif.

      if LINES( TASKS ) >= NUM_PROCESSES or GET_NUMBER_OF_FREE_PROCESSES( ) < 2.
        wait until not ( LINES( TASKS ) >= NUM_PROCESSES or GET_NUMBER_OF_FREE_PROCESSES( ) < 2 ) up to TIME_OUT seconds.
      endif.

    endloop.

    wait until LINES( TASKS ) = 0 up to TIME_OUT seconds.

    loop at TASKS assigning field-symbol(<L_TASK>).
      data L_OUT like line of P_OUT_TAB.
      get time.
      L_OUT-TIME  = SY-UZEIT - <L_TASK>-START.
      L_OUT-MESSAGE = 'Timeout'(001).
      append L_OUT to OUT_TAB.
    endloop.

    sort OUT_TAB by INDEX.
    P_OUT_TAB = OUT_TAB.
    CLEAR OUT_TAB.

  endmethod.
ENDCLASS.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值