最近需要用ABAP触发处理链,并且还要控制处理链的执行次数,找了很多资料,终于成功实现了。我目
前知道的方法有两种。一种是直接通过SAP提供的API函数去触发处理链,另外一种就是通过事件触发处
理链。下面分别对两种触发处理链的方法做大致说明。
Method1:API
在BW中,找到Function group : RSPC_API
里面有如下函数:RSPC_API_CHAIN_GET_ERRORS
RSPC_API_CHAIN_GET_LOG
RSPC_API_CHAIN_GET_PROCESSES
RSPC_API_CHAIN_GET_RUNS
RSPC_API_CHAIN_GET_STARTCOND
RSPC_API_CHAIN_GET_STATUS
RSPC_API_CHAIN_GET_VARIABLES
RSPC_API_CHAIN_INTERRUPT
RSPC_API_CHAIN_MAINTAIN
RSPC_API_CHAIN_RESTART
RSPC_API_CHAIN_SCHEDULE
RSPC_API_CHAIN_SET_STARTCOND
RSPC_API_CHAIN_START
RSPC_API_GET_CHAINS
RSPC_API_PROCESS_GET_INFO
RSPC_API_PROCESS_GET_JOBS
RSPC_API_PROCESS_GET_LOG
RSPC_API_PROCESS_RESTART
RSPC_API_SYSTEM_MESSAGE_GET
相信大家通过函数名称也大概知道了函数的功能。放一个PDF介绍这些函数的,有兴趣的可以研究一下。
/Files/clsoho/ICCJobSchedulingBWCertificationScenarios.pdf
这里我使用RSPC_API_CHAIN_START来触发处理链,这些函数都是RFC函数。
当然,要想多次执行处理链,必须保证上次的处理链执行已经结束。所以如何获取处理链的状态,也是个问题。
我还是使用这里面的函数RSPC_API_CHAIN_GET_STATUS来获得处理链的当前状态。以此判断处理链是否执行完成。
下面是示例程序代码:
REPORT ztest888 MESSAGE-ID ZXXX.
DATA: logid TYPE rspc_logid.
DATA: rs_status TYPE rspc_state.
DATA: nsec TYPE i.
PARAMETERS: p_chain TYPE rspc_chain OBLIGATORY.
PARAMETERS: times TYPE int1 OBLIGATORY DEFAULT 1.
INITIALIZATION.
"initial wait time
nsec = 60.
AT SELECTION-SCREEN.
START-OF-SELECTION.
"RUN PROCESS CHAINP_CHAIN TIMES 。
DO times TIMES.
CLEAR: logid,
rs_status.
CALL FUNCTION 'RSPC_API_CHAIN_START'
EXPORTING
i_chain = p_chain
IMPORTING
e_logid = logid
EXCEPTIONS
system_failure = 1
communication_failure = 2
failed = 3
OTHERS = 4.
IF sy-subrc EQ 0. "START PROCESS CHAIN SUCESSFUL
"SUCESS START PROCESS CHAIN,READ PROCESS CHAIN STATUS
CALL FUNCTION 'RSPC_API_CHAIN_GET_STATUS'
EXPORTING
i_chain = 'ZBW_ALL_MONTH'
i_logid = logid
IMPORTING
e_status = rs_status
EXCEPTIONS
system_failure = 1
communication_failure = 2.
IF sy-subrc <> 0.
MESSAGE e000(zcrm) WITH 'READ PROCESS CHAIN' p_chain 'STATUS WRONG!'.
ENDIF.
WHILE rs_status EQ 'A'. "PROCESS CHAIN STATUS "A",PROCESSING
MESSAGE s000 WITH 'PROCESS CHAIN' p_chain '正在执行中!请等待……'.
WAIT UP TO nsec SECONDS.
CALL FUNCTION 'RSPC_API_CHAIN_GET_STATUS'
EXPORTING
i_chain = p_chain
i_logid = logid
IMPORTING
e_status = rs_status
EXCEPTIONS
system_failure = 1
communication_failure = 2.
IF sy-subrc <> 0.
MESSAGE e000(zcrm) WITH 'READ PROCESS CHAIN' p_chain 'STATUS WRONG!'.
ENDIF.
ENDWHILE.
"IF PROCESS CHAIN'S STATUS IS NOT "A",COMPLETE SUCESSFUL OR WITH ERROR
CASE rs_status.
WHEN 'G'.
"PROCESS CHAIN COMPLETE SUCESSFULLY
MESSAGE i000 WITH 'PROCESS CHAIN' p_chain 'SUCESSFULLY COMPLETE-' sy-index.
WHEN 'R'.
"PROCESS CHAIN INCLUDE ERRORS
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'COMPLETE WITH ERRORS!'.
EXIT.
WHEN 'X'.
"PROCESS CHAIN INTERUPT
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'INTERUPT'.
EXIT.
WHEN OTHERS.
"OTHER ERRORS
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'UNKONW ERROR!'.
EXIT.
ENDCASE.
ELSE.
"PROCESS CHAIN STARTED WRONG
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'START UNSUCESSFUL-' sy-index.
"ADD 1 TO TIMES
times = times + 1.
ENDIF.
ENDDO.
METHOD2:通过定义事件触发处理链。
这种方法在Software Developer Network上已经有教材了。这里上传一PDF文档,只需要根据此文档操作,就不会有问题了。
但这种访求需要将处理链开始变式设置为:直接计划->事件后->输入事件和参数。事件用事务码SM62定义。
并且调度作业 。PDF文件下载地址:/Files/clsoho/TriggeringtheProcessChainsatParticularDateusingEvents.pdf
方法2的示例代码如下:
REPORT ztest999 MESSAGE-ID zxxx.
DATA: eventid TYPE btceventid.
DATA: eventpara TYPE btcevtparm.
DATA: logid TYPE rspc_logid.
DATA: rs_status TYPE rspc_state.
DATA: nsec TYPE i.
DATA: lt_logs TYPE TABLE OF rspc_s_log_f4.
DATA: ls_logs LIKE LINE OF lt_logs.
DATA: rs_flag TYPE c VALUE ''.
PARAMETERS: p_chain TYPE rspc_chain OBLIGATORY.
PARAMETERS: times TYPE int1 OBLIGATORY DEFAULT 1.
INITIALIZATION.
"INITIAL WAIT TIME
nsec = 60.
AT SELECTION-SCREEN.
START-OF-SELECTION.
"RUN PROCESS CHAINP_CHAIN TIMES 。
eventid = 'ZPC_MONTH'.
eventpara = 'ZPC_MONTH_PARA'.
DO times TIMES.
CALL METHOD cl_batch_event=>raise
EXPORTING
i_eventid = eventid
i_eventparm = eventpara
EXCEPTIONS
excpt_raise_failed = 1
excpt_server_accepts_no_events = 2
excpt_raise_forbidden = 3
excpt_unknown_event = 4
excpt_no_authority = 5
OTHERS = 6.
IF sy-subrc EQ 0.
MESSAGE s000 WITH 'EVENT TRIGGER SUCESSFUL !'.
rs_flag = 'X'.
ELSE.
MESSAGE s000 WITH 'EVENT TRIGGER WITH WRONG!'.
EXIT.
ENDIF.
"EVENT TRIGGER SUCESSFUL,READ PROCESS CHAIN STATUS
IF rs_flag EQ 'X'. "START PROCESS CHAIN SUCESSFUL
WAIT UP TO 30 SECONDS.
CALL FUNCTION 'RSPC_API_CHAIN_GET_RUNS'
EXPORTING
i_chain = p_chain
i_date = sy-datum
TABLES
e_t_logs = lt_logs
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
"SORT LOG TABLE
SORT lt_logs BY datum zeit.
" READ LOG TABLE LATEST LOGID
READ TABLE lt_logs INDEX 1 INTO ls_logs.
logid = ls_logs-log_id.
"READ PROCESS CHAIN STATUS BY PROCESS CHAIN AND LOGID
CALL FUNCTION 'RSPC_API_CHAIN_GET_STATUS'
EXPORTING
i_chain = p_chain
i_logid = logid
IMPORTING
e_status = rs_status
EXCEPTIONS
system_failure = 1
communication_failure = 2.
IF sy-subrc <> 0.
MESSAGE e000(zcrm) WITH 'READ PROCESS CHAIN' p_chain 'STATUS WRONG!'.
ENDIF.
WHILE rs_status EQ 'A'. "PROCESS CHAIN STATUS "A",PROCESSING
MESSAGE s000 WITH 'PROCESS CHAIN' p_chain 'IS RUNNING!'.
WAIT UP TO nsec SECONDS.
CALL FUNCTION 'RSPC_API_CHAIN_GET_STATUS'
EXPORTING
i_chain = p_chain
i_logid = logid
IMPORTING
e_status = rs_status
EXCEPTIONS
system_failure = 1
communication_failure = 2.
IF sy-subrc <> 0.
MESSAGE e000(zcrm) WITH 'READ PROCESS CHAIN' p_chain 'STATUS WRONG!'.
ENDIF.
ENDWHILE.
"IF PROCESS CHAIN'S STATUS IS NOT "A",COMPLETE SUCESSFUL OR WITH ERROR
CASE rs_status.
WHEN 'G'.
"PROCESS CHAIN COMPLETE SUCESSFULLY
MESSAGE i000 WITH 'PROCESS CHAIN' p_chain 'SUCESSFULLY COMPLETE-' sy-index.
WHEN 'R'.
"PROCESS CHAIN INCLUDE ERRORS
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'COMPLETE WITH ERRORS!'.
EXIT.
WHEN 'X'.
"PROCESS CHAIN INTERUPT
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'INTERUPT'.
EXIT.
WHEN OTHERS.
"OTHER ERRORS
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'UNKONW ERROR!'.
EXIT.
ENDCASE.
ELSE.
"PROCESS CHAIN STARTED WRONG
MESSAGE e000 WITH 'PROCESS CHAIN' p_chain 'START UNSUCESSFUL-' sy-index.
"ADD 1 TO TIMES
times = times + 1.
ENDIF.
ENDDO.
这两种方法的区别在于一个是直接通过API函数去触发处理链的,需要将处理链开始变式设置为使用元链或API。但是第二种方法是通过
事件触发处理链的,所以需要将开始变式设置为直接计划,事件后输入事件和事件参数。并且需要调度处理链后,执行程序才能触发。
当然,代码不是很完善,有可能会产生死循环等问题。可以根据需要自行修改。BW处理链完全可能进行集成,以减少IT人员的维护压力。
以后我会持续关注这些问题。