Route events to Message Queue for RocketMQ
Use event rules to filter and route events to Message Queue for Apache RocketMQ. This topic explains the prerequisites, procedure, and verification for routing a custom event to Message Queue for Apache RocketMQ.
Prerequisites
-
EventBridge
-
Message Queue for Apache RocketMQ
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
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 Source, select the custom event source that you created in Step 1 from the Event Source drop-down list, enter an event pattern in the Pattern Content code editor, and then click Next Step.
For more information, see Event patterns.
- In the Configure Targets step, configure the event target, and then click Create.
Note An event rule can have a maximum of five event targets.
The following parameters are available when you configure Message Queue for Apache RocketMQ as an event target:
Parameter Description Service Type Select ApsaraMQ for RocketMQ. Instance ID Select the ApsaraMQ for RocketMQ instance that you created. Topic Select the topic that you created. Message Body EventBridge uses a JSONPath expression to extract event content for the message body. Partial event
$.data.bodyCustom Property EventBridge uses JSONPath expressions to define variables from the event and a template to create the custom properties. Template
Variables:
Template:{ "userProperties":"$.data.userProperties", "msgId":"$.data.systemProperties.UNIQ_KEY" }{ "EB_SYS_EMBED_OBJECT":"${userProperties}", "UNIQ_KEY":"${msgId}" }Message Key EventBridge uses a JSONPath expression to extract event content for the message keys. Partial event
$.data.systemProperties.KEYSTags EventBridge uses a JSONPath expression to extract event content for the message tags. Partial event
$.data.systemProperties.TAGSEventBridge provides different transformation types for event content. For more information, see event transformation. The following examples demonstrate each type:
- Partial Event example:
$.data.body - Constant example:
Warning! - Variables and Template example:
Variables example:
{ "type":"$.type" }Template example:
The event type is ${type}.
- Partial Event example:
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.
Verification
To verify that the event was received, query for the message in the Message Queue for Apache RocketMQ console.
Log on to the ApsaraMQ for RocketMQ console.
In the left-side navigation pane, click Instances.
In the top navigation bar, select a region, such as China (Hangzhou).
- On the Instances page, find the target instance, and then in the Actions column, choose .
- On the Message Query page, set Query Method: to By Topic, select the topic that you want to query, specify the Time Range, and then click Search.
The following example shows a received message:
{
"id":"94ebc15f-f0db-4bbe-acce-56fb72fb****",
"source":"acs:mq",
"specversion":"1.0",
"type":"mq:Topic:SendMessage",
"datacontenttype":"application/json; charset=utf-8",
"subject":"acs:mq:cn-hangzhou:123456789098****:MQ_INST_123456789098****_BXhFHryi%TopicName",
"time":"2021-04-08T06:01:20.766Z",
"aliyunpublishtime":"2021-04-08T06:01:20.725Z",
"aliyuneventbusname":"BusName",
"data":{
"topic":"TopicName",
"systemProperties":{
"MIN_OFFSET":"0",
"TRACE_ON":"true",
"MAX_OFFSET":"8",
"MSG_REGION":"cn-hangzhou",
"KEYS":"systemProperties.KEYS",
"CONSUME_START_TIME":1628577790396,
"UNIQ_KEY":"AC14C305069E1B28CDFA3181CDA2****",
"TAGS":"systemProperties.TAGS",
"INSTANCE_ID":"MQ_INST_123456789098****_BXhFHryi"
},
"userProperties":{
},
"body":"TEST"
}
}