mcpField
R
rap-business-events

rap-business-events

github.com/likweitan开发7285 次安装2 周前更新
代码仓库

关于 rap-business-events

rap-business-events 是一项指导技能,帮助 ABAP Cloud 开发者使用 RAP 业务事件和 SAP Event Mesh 实现事件驱动模式。内容涵盖本地与跨系统场景中的事件生产和消费。

主要功能

  1. 01定义 RAP 业务事件及载荷
  2. 02在处理器和保存器方法中触发事件
  3. 03将事件绑定到企业主题
  4. 04配置 SAP Event Mesh 投递
  5. 05指导本地及外部事件消费

适用场景

  1. 01为 RAP 业务对象设计事件
  2. 02跨系统发布事件
  3. 03在 ABAP 系统内处理事件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/likweitan/abap-skills --skill rap-business-events

安装后在 Claude Code 中输入/rap-business-events确认已加载。

附带文件references/event-patterns.md

详细介绍

RAP 业务事件与企业事件机制

本指南介绍如何在 ABAP Cloud 中使用 RAP 业务事件和 SAP Event Mesh 实现事件驱动模式。

工作流

  1. 确定用户的目标:

    • 在 RAP BO 中定义业务事件
    • 从 RAP 处理程序方法中引发事件
    • 绑定事件以供使用
    • 使用来自外部系统的事件
    • 与 SAP Event Mesh 集成
    • 了解 ABAP 中的事件驱动架构
  2. 识别场景:

    • 本地事件(在同一 ABAP 系统内)
    • 企业事件(通过 Event Mesh 跨系统)
    • 事件生产者与事件使用者
  3. 按照 RAP 事件模式指导实现

业务事件概述

概念描述
业务事件在 BDEF 中声明;在发生重要事项时引发
事件定义在行为定义中正式声明并指定参数
事件引发通过 RAISE ENTITY EVENT 在处理程序/保存程序方法中触发
事件绑定将 RAP 事件映射到企业事件主题,以便向外部传递
事件使用外部系统订阅已发布的事件并作出响应

定义业务事件

在行为定义(BDL)中
managed implementation in class zbp_r_travel unique;
strict ( 2 );

define behavior for ZR_Travel alias Travel
persistent table ztravel_tab
lock master
authorization master ( instance )
etag master LocalLastChangedAt
{
  create;
  update;
  delete;

  "Define business events
  event travel_created parameter ZD_TravelCreatedEvt;
  event travel_accepted;
  event travel_rejected;
}
事件参数结构

为事件载荷定义一个 CDS 抽象实体:

@EndUserText.label: 'Travel Created Event'
define abstract entity ZD_TravelCreatedEvt
{
  travel_id   : /dmo/travel_id;
  agency_id   : /dmo/agency_id;
  customer_id : /dmo/customer_id;
  description : /dmo/description;
  total_price : /dmo/total_price;
  currency    : /dmo/currency_code;
}

没有 parameter 附加项的事件不包含载荷。

引发业务事件

在处理程序方法中
METHOD on_travel_accept.
  "Read travel data
  READ ENTITIES OF zr_travel IN LOCAL MODE
    ENTITY Travel
    ALL FIELDS
    WITH CORRESPONDING #( keys )
    RESULT DATA(lt_travels).

  "Update status
  MODIFY ENTITIES OF zr_travel IN LOCAL MODE
    ENTITY Travel
    UPDATE FIELDS ( status )
    WITH VALUE #( FOR travel IN lt_travels
      ( %tky   = travel-%tky
        status = 'A' ) )
    REPORTED DATA(lt_reported).

  "Raise event for each accepted travel
  RAISE ENTITY EVENT zr_travel~travel_accepted
    FROM VALUE #( FOR travel IN lt_travels
      ( %key = travel-%key ) ).
ENDMETHOD.
使用事件参数
METHOD on_travel_create.
  "After successful creation
  RAISE ENTITY EVENT zr_travel~travel_created
    FROM VALUE #( FOR travel IN lt_created_travels
      ( %key = travel-%key
        %param = VALUE #(
          travel_id   = travel-travel_id
          agency_id   = travel-agency_id
          customer_id = travel-customer_id
          description = travel-description
          total_price = travel-total_price
          currency    = travel-currency_code ) ) ).
ENDMETHOD.
在保存程序方法中(额外保存)
METHOD save_modified.
  "Raise events in the save phase for committed data
  IF create-travel IS NOT INITIAL.
    RAISE ENTITY EVENT zr_travel~travel_created
      FROM VALUE #( FOR travel IN create-travel
        ( %key = travel-%key
          %param = VALUE #(
            travel_id = travel-travel_id ) ) ).
  ENDIF.
ENDMETHOD.

事件处理流程

1. User action triggers RAP operation
2. Handler method executes business logic
3. RAISE ENTITY EVENT queues the event
4. RAP framework commits the transaction
5. After successful COMMIT:
   a. Local event handlers are called
   b. Enterprise events are published to Event Mesh

启用企业事件

事件绑定

要向外部发布 RAP 事件,请创建事件绑定:

ADT: New → Other → Event Binding
Name: Z_EVT_BIND_TRAVEL

事件绑定会将 RAP 事件映射到企业事件主题:

属性值
命名空间sap.s4.beh 或自定义命名空间
业务对象ZR_Travel
事件travel_created
主题sap/s4/beh/travel/created/v1
事件主题结构
<namespace>/<business-object>/<event-name>/<version>
Example: z.custom/travel/created/v1
SAP Event Mesh 的通道绑定
  1. 为场景 SAP_COM_0092(企业事件启用)创建一个通信安排
  2. 在 BTP 中配置 Event Mesh 服务实例
  3. 在 Enterprise Event Enablement Fiori 应用中维护通道
  4. 激活事件主题

使用事件

本地事件使用(同一系统)

注册事件处理程序类:

CLASS zcl_travel_event_handler DEFINITION
  PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    "Event handler method
    METHODS on_travel_created
      FOR ENTITY EVENT
      travel_created FOR Travel~travel_created.
ENDCLASS.

CLASS zcl_travel_event_handler IMPLEMENTATION.
  METHOD on_travel_created.
    "React to travel creation
    LOOP AT travel_created INTO DATA(ls_event).
      "Process event data
      DATA(lv_travel_id) = ls_event-travel_id.
      "e.g., send notification, update related records
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.
外部事件使用(通过 Event Mesh)

外部系统可通过以下方式订阅主题:

  • SAP Event Mesh Webhook
  • SAP Integration Suite
  • 使用 AMQP 或 REST API 的自定义应用程序
在 ABAP 中使用来自外部系统的事件
"Using the event consumption model
"1. Create event consumption model in ADT
"   (imports AsyncAPI spec or defines events manually)

"2. Implement the event handler
CLASS zcl_ext_event_handler DEFINITION
  PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_event_handler.
ENDCLASS.

CLASS zcl_ext_event_handler IMPLEMENTATION.
  METHOD if_event_handler~handle.
    "Parse event payload
    DATA(lv_payload) = io_event->get_text( ).
    "Process the event
  ENDMETHOD.
ENDCLASS.

事件模式

发出后不等待响应
Producer raises event → Event Mesh delivers → Consumer processes independently
  • 不需要响应
  • 系统之间松散耦合
  • 最适用于通知、审计日志记录和数据复制触发
事件携带状态转移

在事件载荷中包含完整实体数据,这样使用者就无需回调:

RAISE ENTITY EVENT zr_travel~travel_created
  FROM VALUE #( ( %key = ls_travel-%key
                  %param = CORRESPONDING #( ls_travel ) ) ).
事件溯源

将每次状态变更记录为事件,以便保留完整审计轨迹。

最佳实践

  1. 为具有业务意义的状态变更定义事件,而不是为技术操作定义
  2. 在事件参数中包含足够的数据,避免使用者回调
  3. 使用 CDS 抽象实体定义事件参数类型(明确约定)
  4. 在验证后引发事件——只有在操作将会成功时才引发
  5. 处理事件处理失败的情况——使用者应具备幂等性
  6. 按照 SAP 约定使用有意义的主题命名
  7. 为事件主题设置版本以确保向后兼容(/v1、/v2)

输出格式

在协助处理事件相关主题时,请按以下结构组织回复:

## RAP Business Event Guidance

### Scenario

- Type: [Local event / Enterprise event]
- Role: [Producer / Consumer]

### Implementation

[Event definition, raising, and consumption code]

### Configuration

[Event binding and communication arrangement setup]

参考资料

相关技能

  • rap:用于构建能够触发事件的 RAP 业务对象
  • btp-abap-environment:用于通过通信配置设置 Event Mesh 集成
  • authorization-iam:用于保护事件处理并控制访问

评论

支持 Markdown