Route events to Simple Message Queue (formerly MNS)
You can use an event rule to filter events and route them to a queue in Message Service (MNS). This topic uses a custom event to describe the prerequisites, procedure, and how to verify the result of routing events to Simple Message Queue (formerly MNS).
Prerequisites
-
EventBridge
-
Message Service (MNS)
Step 1: Add a custom event source
- Log on to the EventBridge console. In the left navigation pane, click Event Buses.
- In the top navigation bar, select a region. On the Event Buses page, click the name of the target custom event bus.
- In the left navigation pane, click Event Sources, and then click Add Event Source.
-
In the Add Custom Event Source panel, enter a Name and Description, select Custom Application for Event Provider, and then click OK.
Step 2: Create an event rule
The event target and the event rule must be in the same region.
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.
-
On the Event Rules page, click Create Rule.
-
On the Create Rule page, complete the following steps.
-
In the Configure Basic Info step, enter a Name and Description for the rule, and then click Next Step.
-
In the Configure Event Pattern step, set Event Source Type to Custom Event Sources. For Event Sources, select the custom event source that you added in Step 1. In the Event Pattern code editor, enter an event pattern, and then click Next Step.
For more information, see Event patterns.
-
In the Configure Targets step, configure the event target, and then click Create.
NoteYou can add up to five targets for a single event rule.
-
Service Type: Select Simple Message Queue (formerly MNS).
-
Queue: Select the queue that you created.
-
Message Content: Select Template.
The following examples show a variable and a template.
Example variable:
{ "source":"$.source" }Example template:
The event 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 target event bus and click Publish Event in the Operations column.
NoteThe EventBridge console supports publishing events to custom event buses only.
In the Publish Event To Custom Event Bus panel, configure the following settings:
Setting Description Custom Event Source Select an existing custom event source from the drop-down list. Event Body Enter the event content. Click OK.
Verify the result
You can verify that the event was received in the Message Service (MNS) 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 target queue and choose in the Actions column.
-
On the Send and Receive Messages page, go to the Receive Message section and click Receive Message.
ImportantWhen you use the Java SDK for Message Service (MNS) to receive events, the message body is not Base64-encoded and may appear as garbled characters. To retrieve the event body correctly, use the getMessageBodyAsRawBytes() or getMessageBodyAsRawString() method.
The following is a sample received message.
{ "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 } }