When you integrate a self-managed Apache RocketMQ instance with Function Compute through EventBridge, you can use a trigger to invoke a function. This allows you to process messages published to Apache RocketMQ with custom code. This topic describes how to create a trigger for a self-managed Apache RocketMQ instance, configure a test event, and write and test function code in the Function Compute console.
Background information
When you create a trigger in the Function Compute console, Function Compute automatically creates a corresponding event stream in EventBridge based on your trigger configuration.
After the trigger is created, you can view its details in the Function Compute console and the automatically created resources in the EventBridge console. When a message is queued in your Apache RocketMQ instance, the trigger invokes your function. Based on your batching configuration, the trigger pushes one or more message events to the function in a batch for processing.
Prerequisites
EventBridge
Function Compute
Apache RocketMQ
Apache RocketMQ 5.0: Quick Start.
Apache RocketMQ 4.x: Quick Start.
A self-managed Apache RocketMQ cluster is deployed.
A topic is created.
A consumer group is created.
You can use the following documents to quickly deploy an Apache RocketMQ cluster and start sending and receiving messages.
Limitations
The Apache RocketMQ instance that acts as the event source must be accessible over the public internet or from an Alibaba Cloud VPC.
If the Apache RocketMQ instance is accessed from a VPC, the VPC must be in the same region as your function.
You cannot create more Apache RocketMQ triggers once the number of event streams reaches the limit. For more information about the limits on event streams, see Limits.
Step 1: Create a self-managed Apache RocketMQ trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Trigger tab and then click Create Trigger.
In the Create Trigger panel, configure the parameters and click OK.
Parameter
Actions
Example
Trigger Type
Select Self-built Apache RocketMQ.
Self-built Apache RocketMQ
Name
Enter a custom name for the trigger.
apache-rocketmq-trigger
Version or Alias
The default value is LATEST. To create a trigger for a specific version or alias, you must first switch to that version or alias on the function details page. For more information, see Manage versions and Manage aliases.
LATEST
Endpoint
Enter the NameServer address of your Apache RocketMQ cluster.
192.168.X.X:9876
Topic
Select the topic in your Apache RocketMQ instance.
testTopic
Group ID
Select the consumer group ID in your Apache RocketMQ instance.
testGroup
Filter type
Select a message filtering method. Valid values:
Tag: Filters messages by tag.
SQL: Filters messages by using an SQL expression. This allows you to match message properties and values.
Tag
Filter
After you select a Filter type, specify the corresponding filter expression.
TagA
Authentication Mode
Select an authentication mode. Only ACL is supported.
ACL
Username
If you set Authentication Mode to ACL, enter the username for authentication.
admin
Password
If you set Authentication Mode to ACL, enter the password for authentication.
******
Consumer Offset
Select the starting point for message consumption. EventBridge will pull messages starting from this offset. Valid values:
Latest Offset: Start consuming from the latest offset.
Earliest Offset: Start consuming from the earliest offset.
Timestamp: Start consuming from a specific point in time.
Latest offset
Networking
Select the network type for message routing. Valid values:
Internet: Access the Apache RocketMQ cluster over the public internet.
VPC: Access the Apache RocketMQ cluster through an Alibaba Cloud VPC. You must select the VPC, vSwitch, and Security Group.
Public network
Invocation Method
Select the invocation method.
Valid values:
Sync Invocation: This is the default method. An event triggers the function. Function Compute waits for the invocation to complete and then returns the execution result. For more information, see Synchronous invocations.
Async Invocation: This method is suitable for long-running functions. After an event triggers the function, Function Compute returns an immediate response. It ensures the function is executed at least once but does not return the execution result. For more information, see Asynchronous invocation overview.
Synchronous invocation
Trigger State
Specifies whether to enable the trigger immediately after it is created. This option is enabled by default.
Enable trigger
For information about advanced settings, such as push configurations, retry policies, 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 a test event
A self-managed Apache RocketMQ event source passes an event to the function as an input parameter. You can manually pass an event to the function to simulate an event trigger.
On the Code tab of the function details page, click the
icon next Test Function and select Configure Test Parameters from the drop-down list. In the Configure Test Parameters panel, select Create New Test Event or Modify Existing Test Event. Enter an event name, provide the event content, and 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 parameters of the event field are described in the following table.
Parameter
Type
Example
Description
msgIdString
7F0000010BDD2A84AEE70DA49B57****
The message ID from Apache RocketMQ.
topicString
testTopic
The topic name.
systemPropertiesMap
System properties.
UNIQ_KEYString
7F0000010BDD2A84AEE70DA49B57****
The unique key of the message.
CLUSTERString
DefaultCluster
The name of the Apache RocketMQ cluster.
MIN_OFFSETInteger
0
The minimum offset.
MAX_OFFSETInteger
128
The maximum offset.
TAGSString
TagA
The filter tag.
userPropertiesMap
None
User-defined properties.
bodyString
Hello RocketMQ
The message body.
Step 3: Write and test the function code
After you create the trigger, you can write and test your function code. In a real-world scenario, the trigger automatically invokes the function when Apache RocketMQ receives a message.
On the function details page, click the Code tab. Write your code in the editor and then click Deploy.
This topic uses Node.js as an example. The following is a code sample.
'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 Test Function.
More information
In addition to the Function Compute console, you can configure triggers by using one of the following methods:
Use Serverless Devs tool to configure triggers. For more operations, please refer to Common Serverless Devs commands.
Use SDKs to configure triggers. For more information, see SDKs.
To modify or delete an existing trigger, see Manage triggers.