All Products
Search
Document Center

Function Compute (2.0):Create an Apache RocketMQ trigger

Last Updated:Aug 14, 2023

After you integrate Apache RocketMQ with Function Compute by using EventBridge, you can use an Apache RocketMQ trigger to trigger a function and process the messages published to Apache RocketMQ based on your business requirements. This topic describes how to create an Apache RocketMQ trigger in the Function Compute console, configure input parameters, and write and test code.

Background

After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates event stream resources in EventBridge based on the trigger configurations.

After the trigger is created, you can view information about the trigger in the Function Compute console. You can also view information about the created resources in the EventBridge console. When a message is enqueue in Apache RocketMQ, the function in Function Compute are triggered. After that, one or more message events are pushed to the function in batches for processing based on your batch configurations.

Before you start

Limits

  • The source Apache RocketMQ can be accessed over the Internet or an Alibaba Cloud virtual private cloud (VPC).

  • If you want to access Apache RocketMQ over a VPC, the VPC instance must be in the same region as the function in Function Compute.

  • If the number of created event streams exceeds the upper limit, no more Apache RocketMQ triggers can be created. For the upper limit of event streams, see Limits.

Step 1: Create an Apache RocketMQ trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click the name of the desired function.
  4. On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.

  5. In the Create Trigger panel, configure the parameters and click OK.

    Parameter

    Description

    Example

    Trigger Type

    Select Self-built Apache RocketMQ.

    Self-built Apache RocketMQ

    Name

    Enter a trigger name.

    apache-rocketmq-trigger

    Version or Alias

    The default value is LATEST. If you want to create a trigger for another version or alias, switch to the 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

    Endpoint

    Enter the address of the cluster NameServer.

    192.168.X.X:9876

    Topic

    Select the topic of the created Apache RocketMQ instance.

    testTopic

    Group ID

    Select the ID of the consumer group of the created Apache RocketMQ instance.

    testGroup

    FilterType

    Select a message filter type. Valid values:

    • Tag: filters messages by tag.

    • SQL: filters messages by SQL statement. You can match the attribute identifiers and attribute values of messages.

    Tag

    Filter

    After you configure FilterType, you need to configure the filter statements under the corresponding filter type.

    TagA

    Authentication Mode

    Select the authentication mode. ACL mode is supported.

    ACL

    Username

    If you set Authentication Mode to ACL, you must enter the Apache RocketMQ username for authentication.

    admin

    Password

    If you set Authentication Mode to ACL, you must configure the Apache RocketMQ password for authentication.

    ******

    Consumer Offset

    Select the consumer offset of the message. The consumer offset specifies the location where Apache RocketMQ starts to pull messages from the EventBridge. Valid values:

    • Latest Offset: consumes messages from the latest offset.

    • Earliest Offset: consumes messages from the earliest offset.

    • Timestamp: consumes messages from the specified timestamp.

    Latest Offset

    Networking

    Select the type of the network over which you want to route the messages. Valid values:

    • Internet: Access the Apache RocketMQ cluster over the Internet.

    • Virtual Private Cloud: Access the Apache RocketMQ cluster over a VPC. You must configure VPC, vSwitch, and Security Group.

    Internet

    Invocation Method

    Select a method to invoke the function.

    Valid values:

    • Sync Invocation: After an event triggers the function, Function Compute waits for the function invocation to complete before it returns responses. For more information, see Synchronous invocations.

    • Async Invocation: After an event triggers the execution of a function, Function Compute immediately returns a response and ensures that the function is successfully executed at least once. However, the detailed execution result is not returned. This invocation method is suitable for functions that have higher scheduling latency. For more information, see Overview.

    Sync Invocation

    Trigger State

    Specify whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected and the trigger is immediately enabled after it is created.

    Enable Trigger

    For more information about advanced configurations such as push settings, retry policies, and dead letter queues, see Advanced features of triggers.

    The trigger is displayed in the Triggers tab after it is created. To modify or delete a created trigger, see Manage triggers.

Step 2: Configure the input parameters of the function

The self-built Apache RocketMQ event source is passed to the function in the form of event as the input parameters. You can manually pass event to the function to simulate trigger events.

  1. On the function details page, click the Code tab and click the xialatubiao icon. From the drop-down list that appears, select Configure Test Parameters.
  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. Then, click OK.

    Sample code of event:

    [
      {
        "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 parameters that are contained in event.

    Parameter

    Type

    Example

    Description

    msgId

    String

    7F0000010BDD2A84AEE70DA49B57****

    The ID of the Apache RocketMQ message.

    topic

    String

    testTopic

    The topic name.

    systemProperties

    Map

    The 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

    The minimum offset.

    MAX_OFFSET

    Int

    128

    The maximum offset.

    TAGS

    String

    TagA

    The tags that are used to filter messages.

    userProperties

    Map

    None.

    The user properties.

    body

    String

    Hello RocketMQ

    The content of the message.

Step 3: Write and test the function

After you create the trigger, you can write function code and test the function to verify whether the code is correct. In actual operations, when Apache RocketMQ receives a message, the trigger automatically triggers the function.

  1. On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.

    In this topic, sample code in a Node.js runtime is used as an example.

    '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');
    }
  2. 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.