You can use event rules to filter events and route the events to Simple Message Queue (formerly MNS) (SMQ). This topic describes the prerequisites and procedure for routing custom events to SMQ. This topic also describes how to verify the result.
Prerequisites
EventBridge
EventBridge is activated, and the required permissions are granted to a Resource Access Management (RAM) user. For more information, see Activate EventBridge and grant permissions to a RAM user.
SMQ
A queue is created. For more information, see Create a queue.
Step 1: Create a custom event source
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region. On the Event Buses page, click the name of the custom event bus that you want to manage.
In the left-side navigation pane, click Event Sources and then click Add Event Source.
In the Add Custom Event Source panel, configure 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
The event targets that you want to configure for an event rule must reside in the same region as the event rule.
Log on to the EventBridge console.
In the left-side navigation pane, click Event Buses.
In the top navigation bar, select a region.
On the Event Buses page, click the name of the event bus that you want to manage.
In the left-side navigation pane, click Event Rules.
In the left-side navigation pane, click Event Rules. On the page that appears, click Create Rule.
In the Create Rule wizard, perform the following steps:
In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field. Then, click Next Step.
In the Configure Event Pattern step, set Event Source Type to Custom Event Source and select the custom event source that you configured in Step 1 from the Event Source drop-down list. Then, in the Pattern Content code editor, specify an event pattern and click Next step.
For more information, see Event patterns.
In the Configure Targets step, configure event targets and click Create.
NoteYou can configure up to five event targets for an event rule.
Service Type: Select SMQ.
Queue: Select the SMQ queue that you created.
Message Body: Select Template.
The following sample code provides examples of variables and templates.
Sample variables:
{ "source":"$.source" }
Sample template:
The event comes from ${source} is abnormal.
For more information, see Event transformation.
Step 3: Publish an event
Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.
- In the top navigation bar, select a region.
- 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.
- 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 events are received in the SMQ console.
Log on to the SMQ console.
In the left-side navigation pane, choose Queue Model > Queues.
In the top navigation bar, select a region.
On the Queues page, find the queue to which you routed the events and choose in the Actions column.
In the Receive Message section of the Quick Experience page, click Receive Message.
ImportantWhen you receive a delivered event by using SMQ 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 provides an example of received events:
{ "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 } }