After you integrate Apache RocketMQ as an event source with EventBridge and Function Compute, an Apache RocketMQ trigger can execute an associated function to process messages published to Apache RocketMQ. This topic describes how to create an Apache RocketMQ trigger, configure input parameters, and write and test code in the Function Compute console.
Background information
After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates an event stream resource in EventBridge based on the trigger configuration.
After the trigger is created, you can view the trigger information in the Function Compute console and the automatically created resource information in the EventBridge console. When a message is enqueued in Apache RocketMQ, the function is triggered. Based on your batch configuration, one or more message events are pushed to the function in a batch for processing.
Prerequisites
EventBridge
Function Compute
Apache RocketMQ
Apache RocketMQ 5.0: , Quick Start (International).
Apache RocketMQ 4.x: Quick Start.
You have created a self-managed Apache RocketMQ cluster.
Create a topic
Create a Consumer Group
Use the following documents to quickly deploy an Apache RocketMQ cluster and send and receive messages.
Limits
The Apache RocketMQ cluster that serves as the event source must be accessible over the public network or within an Alibaba Cloud VPC.
If the Apache RocketMQ cluster is accessible within an Alibaba Cloud VPC, the VPC-connected instance and the Function Compute function must be in the same region.
If the number of event streams exceeds the limit, you cannot create additional Apache RocketMQ triggers. For more information about event stream limits, see Limits.
Step 1: Create an Apache RocketMQ trigger
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, click the desired service.
On the Functions page, click the name of the desired function.
On the function details page, click the Triggers tab. Select the version or alias for which you want to create a trigger from the Version or Alias drop-down list, and then click Create Trigger.
In the Create Trigger panel, enter the required information and click OK.
Configuration Item
Action
Example
Trigger Type
Select Self-managed Apache RocketMQ.
Self-managed Apache RocketMQ
Name
Enter a custom trigger name.
apache-rocketmq-trigger
Version or Alias
The default value is LATEST. To create a trigger for another version or alias, switch to that version or alias in the upper-right corner of the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.
LATEST
access point
Enter the NameServer address of the cluster.
192.168.X.X:9876
Topic
Select the topic of the created Apache RocketMQ instance.
testTopic
Group ID
Select the consumer group ID of the created Apache RocketMQ instance.
testGroup
FilterType
Select the message filtering type. Valid values:
Tag: Filters messages by tag.
SQL: Filters messages using an SQL statement that can match message properties and their values.
Tag
Filter
After you select a FilterType, you must configure the filter statement for the selected filter type.
TagA
Authentication Mode
Select an authentication mode. Only the ACL mode is supported.
ACL
Username
If you set Authentication Mode to ACL, you must configure the username of the Apache RocketMQ instance for identity verification.
admin
Password
If you set Authentication Mode to ACL, you must configure the password of the Apache RocketMQ instance for identity verification.
******
Consumer Offset
The consumer offset for a message specifies the position in the event bus from which Apache RocketMQ begins retrieving messages. The following section describes valid values.
Latest Offset: Consumption starts from the latest offset.
Earliest Offset: Consumption starts from the earliest offset.
Specified Timestamp: Consumption starts from a specified timestamp.
Latest Offset
Network Configuration
Select the network type for message routing. Valid values:
Public Network: Accesses the Apache RocketMQ cluster over the public network.
VPC: Accesses the Apache RocketMQ cluster over an Alibaba Cloud VPC. You must select the corresponding VPC, vSwitch, and Security Group.
Public Network
Invocation Mode
Select the function invocation mode.
Valid values:
Synchronous Call: The default invocation mode. The event triggers the function. Function Compute returns the execution result after the function is executed. For more information, see Synchronous calls.
Asynchronous Invocation: Suitable for functions with long scheduling latencies. The event triggers the function. Function Compute immediately returns a response and ensures that the function is executed at least once. The specific execution result is not returned. For more information, see Asynchronous invocations.
Synchronous Call
Trigger Status
Specifies whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected, and the trigger is enabled immediately after creation.
Enable Trigger
For more information about advanced configuration items such as push configurations, retries, and dead-letter queues, see Advanced features of triggers.
After the trigger is created, it appears on the Triggers tab. To modify or delete the trigger, see Manage triggers.
Step 2: Configure function input parameters
The self-managed Apache RocketMQ event source passes an event as an input parameter to the function. You can manually pass the event to the function to simulate a triggering event.
- On the function details page, click the Code tab and click the
icon. From the drop-down list that appears, select Configure Test Parameters. In the Configure Test Parameters panel, click the Create New Test Event or Edit Existing Test Event tab. Enter an Event Name and the event content. Then, click OK.
The event is in the following format.
[ { "msgId": "7F0000010BDD2A84AEE70DA49B57****", "topic": "testTopic", "systemProperties": { "UNIQ_KEY": "7F0000010BDD2A84AEE70DA49B57****", "CLUSTER": "DefaultCluster", "MIN_OFFSET": "0", "TAGS": "TagA", "MAX_OFFSET": "128" }, "userProperties": {}, "body": "Hello RocketMQ" } ]The following table describes the parameters in the event field.
Parameter
Type
Example
Description
msgId
String
7F0000010BDD2A84AEE70DA49B57****
The message ID in Apache RocketMQ.
topic
String
testTopic
The topic name.
systemProperties
Map
System properties.
UNIQ_KEY
String
7F0000010BDD2A84AEE70DA49B57****
The unique key of the message.
CLUSTER
String
DefaultCluster
The name of the Apache RocketMQ cluster.
MIN_OFFSET
Int
0
Minimum offset.
MAX_OFFSET
Int
128
The highest offset.
TAGS
String
TagA
Filter properties.
userProperties
Map
None
User properties.
body
String
Hello RocketMQ
The message content.
Step 3: Write and test the function code
After you create the trigger, write and test the function code to verify its correctness. In a real-world scenario, the trigger automatically executes the function when Apache RocketMQ receives a message.
On the function details page, click the Code tab. Write the code in the code editor and click Deploy Code.
This topic uses Node.js code as an example. The sample code is as follows.
'use strict'; /* To enable the initializer feature please implement the initializer function as below: exports.initializer = (context, callback) => { console.log('initializing'); callback(null, ''); }; */ exports.handler = (event, context, callback) => { console.log("event: %s", event); // Parse the event parameters and process the event. callback(null, 'return result'); }- Click the Code tab and click Test Function. After the function is executed, you can view the result on the Code tab.
Additional information
In addition to the Function Compute console, you can configure triggers by using the following methods:
Use Serverless Devs to configure triggers. For more information, see Create a trigger.
Use SDKs to configure triggers. For more information, see SDKs.
To modify or delete an existing trigger, see Manage triggers.