Alibaba Cloud Message Queue for RabbitMQ is developed based on the open-source AMQP protocol. This topic describes how to push data from Message Queue for RabbitMQ to Function Compute by using EventBridge.

Notes

The regions that are supported by the connector are subject to the regions where the Message Queue for RabbitMQ is activated.

Step 1: Create a connector

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Custom Event Buses.
  3. In the top navigation bar, select a region.
  4. On the Custom Event Buses page, find the required event bus and click Connectors in the Operations column.
  5. On the Connectors page, click Create Connector.
  6. Click message queue for Apache RabbitMQ, perform the following operations, and then click create and start.
    • Name: Enter the name of the connector.
    • RabbitMQ instance: Select the created instance.
    • Vhost: Select the created Vhost.
    • Queue: Select the created Queue.

Step 2: Create an event rule

Notice The event targets that you want to add for an event rule must be in the same region as the event rule.
  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Custom Event Buses.
  3. In the top navigation bar, select a region.
  4. On the Custom Event Buses page, find the required event bus and click Rules in the Operations column.
  5. On the Rules page, click Create Rule.
  6. On the Create Rule page, perform the following steps:
    1. In the Configure Basic Info step, enter a rule name in the Name field and a rule description in the Description field, and click Next Step.
    2. In the Configure Event Pattern step, set Event Pattern Type to Customized Pattern, specify an event pattern in the Event Pattern Content field, and then click Next Step.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure an event target. Then, click Create.
      Note You can add a maximum of five event targets for an event rule.
      • Service Type: Click Function Compute.
      • Service: Select the service that you created.
      • Function: Select the function that you created.
      • Event: Select the type of event transformer.
        • Complete Event: The complete data structure is routed without transformation. The data structure is defined in the CloudEvents 1.0 protocol.
        • Partial Event: JSONPath is used to extract the content that needs to be routed to the event target.
        • Constant: The event serves as a trigger. Only constants are routed regardless of the event content.
        • Template: The event is routed in the format that is defined in a custom template to the event target. You can customize variables in the template.

          The following part shows sample variables and a sample template:

          Sample variables:

          {
            "source":"$.source",
            "type":"$.type"
          }

          Sample template:

          The event comes from ${source},event type is ${type}.

        For more information, see Event transformation.

      • Service Version and Alias: Select a service version and alias.

Step 3: Publish an event

import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
import java.util.UUID;

public class ProducerTest {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("xxx.xxx.aliyuncs.com");
        factory.setUsername("${UserName}");
        factory.setPassword("${PassWord}");
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(5000);
        factory.setVirtualHost("${VhostName}");
        factory.setPort(5672);
        factory.setConnectionTimeout(30 * 1000);
        factory.setHandshakeTimeout(30 * 1000);
        factory.setShutdownTimeout(0);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        for (int i = 0; i < 100; i++  ) {
            AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId(UUID.randomUUID().toString()).build();
            channel.basicPublish("${ExchangeName}", "BindingKey", true, props,
                    ("message Body"  + i).getBytes(StandardCharsets.UTF_8));
        }
        connection.close();
    }
}