You can use event rules to filter events and route events to MNS queues. This topic describes the prerequisites and procedure for routing custom events to MNS. This topic also describes how to verify the result.

Step 1: Create a custom event source

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Event Buses.
  3. In the top navigation bar, select a region.
  4. On the Event Buses page, click the name of the target event bus.
  5. In the left-side navigation pane, click Event Sources.
  6. On the Event Sources page, click Add Event Source.
  7. In the Add Custom Event Source panel, set the Name and Description parameters, select Custom Application from the Event Provider drop-down list, and then click OK.

Step 2: Create an event rule

Notice The event targets that you want to configure for an event rule must reside in the same region as the event rule.
  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Event Buses.
  3. In the top navigation bar, select a region.
  4. On the Event Buses page, click the name of the target event bus.
  5. In the left-side navigation pane, click Event Rules.
  6. On the Event Rules page, click Create Rule.
  7. Complete the Create Rule wizard.
    1. In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field, and click Next Step.
    2. In the Configure Event Pattern step, set the Event Source Type parameter to Custom Event Source, select the custom event source that is created in Step 1 from the Event Source drop-down list, specify an event pattern in the Event Pattern Content code editor, and then click Next Step.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure an event target. Then, click Create.
      Note You can configure a maximum of five event targets for an event rule.
      • Service Type: Click Message Service.
      • Queue: Select the queue that you created.
      • Message Content: Click Template.

        The following part shows a sample variable and a sample template:

        Sample variable:

        {
          "source":"$.source"
        }

        Sample template:

        The event comes from ${source} is abnormal.

        For more information, see Event transformation.

Step 3: Publish an event

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Event Buses.
  3. In the top navigation bar, select a region.
  4. On the Event Buses page, find the event bus to which you want to publish an event and click Publish Event in the Operations column.
    Note You can publish events only to custom event buses in the EventBridge console.
  5. In the Publish Event to Custom Event Bus panel, select a custom event source from the Custom Event Source drop-down list, enter the event content in the Event Body code editor, and then click OK.
    For more information about the event parameters, see Overview.

Verify the result

You can check whether the event is received in the MNS console.

  1. Log on to the MNS console.
  2. In the left-side navigation pane, click Queues.
  3. In the top navigation bar, select a region.
  4. On the Queues page, find the queue to which you routed the event and choose More > Send Messages in the Actions column.
  5. In the Receive Message section of the Quick Experience page, click Receive Message.
    Notice When you receive the delivered event by using MNS SDK for Java, the received message appears as garbled characters because the body of the message is not encoded in Base64. Therefore, you must use the getMessageBodyAsRawBytes or getMessageBodyAsRawString method to obtain the body of the event.

    The following sample code shows the content of the received event:

    {
        "id":"22h0iaji-8cf5-2iea-j4a6-i99a5j1a****",
        "source":"acs.oss",
        "specversion":"1.0",
        "type":"oss:ObjectCreated:PostObject",
        "datacontenttype":"application/json;charset=utf-8",
        "dataschema":"http://example.com/item.json",
        "subject":"acs:oss:cn-hangzhou:<yourAccountId>:xls-papk/game_apk/123.jpg",
        "aliyunaccountid":"165228866041****",
        "aliyunpublishtime":"2020-10-14T17:00:03.628Asia/Shanghai",
        "aliyuneventbusname":"MyEventBus",
        "aliyunpublishaddr":"10.0.XX.XX",
        "data":{
            "name":"Eventbridge",
            "number":100
        }
    }