You can use event rules to filter events and route events to Message Queue for RabbitMQ. This topic describes the prerequisites and procedure for routing custom events to Message Queue for RabbitMQ. This topic also describes how to verify the result.

Step 1: Create a custom event source

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, choose Event-driven Operations > Event Sources.
  3. In the top navigation bar, select a region.
  4. On the Event Sources page, click the Custom Event Sources tab. On the Custom Event Sources tab, click Add Event Source.
  5. In the Add Custom Event Source panel, enter a name and a description, select an event provider and an existing custom event bus, and then click OK.
    You can also click Create Custom Event Bus to associate the custom event source with a new custom event bus.

Step 2: Create an event rule

Notice The event targets that you want to configure for an event rule must reside in the same region as the event rule.
  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, choose Event-driven Operations > Event Rules.
  3. In the top navigation bar, select a region.
  4. On the Event Rules page, select the custom event bus with which the custom event source created in Step 1 is associated, and then click Create Rule.
  5. Complete the Create Rule wizard.
    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 the Event Source Type parameter to Custom Event Source, select the custom event source that is created in Step 1 from the Event Source drop-down list, specify an event pattern in the Event Pattern Content code editor, and then click Next Step.

      For more information, see Event patterns.

    3. In the Configure Targets step, configure an event target and click Create.
      Note You can configure a maximum of five event targets for an event rule.

      The following table describes the parameters that you can set if you select Message Queue for RabbitMQ as the event target.

      Parameter Description
      Service Type Click Message Queue for RabbitMQ.
      Instance ID Select the ID of the created Message Queue for RabbitMQ instance.
      vhost Select the created vhost.
      Destination Type Click Exchange Mode or Queue Mode.
      Exchange Select the created exchange. This parameter is required only when you set the Destination Type parameter to Exchange.
      Routing Key Click Partial Event or Constant. This parameter is required only when you set the Destination Type parameter to Exchange.
      Queue Select the created queue. This parameter is required only when you set the Destination Type parameter to Queue Mode.
      Body Click Complete Event, Partial Event, Constant, or Template.
      MessageId Click Partial Event, Constant, or Template.
      Properties Click Partial Event or Template.
      Note The value of this parameter is of the MAP type.

      The following part provides examples of event transformation methods of EventBridge. For more information, see Event transformation.

      • Example of partial event
        $.data.body
      • Example of constant
        Pay attention to the alert. 
      • Examples of variable and template

        Sample variable:

        {
          "type":"$.type"
        }

        Sample template:

        The event type is ${type}.

Step 3: Publish an event

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, choose Event-driven Operations > Event Buses.
  3. In the top navigation bar, select a region.
  4. In the Custom Event Buses section, find the event bus to which you want to publish an event and click Publish Event in the Operations column.
    Note You can publish events only to custom event buses in the EventBridge console.
  5. In the Publish Event to Custom Event Bus panel, select a custom event source from the Custom Event Source drop-down list, enter the event content in the Event Body code editor, and then click OK.
    For more information about the event parameters, see Overview.

Verify the result

You can check whether the event is received in the Message Queue for RabbitMQ console.

  1. Log on to the Message Queue for RabbitMQ console.
  2. In the Resource Distribution section of the Overview page, select the region where your instance is located.
  3. On the Instances page, find the instance that you require, and choose More > Message Query in the Actions column.
  4. On the Message Query page, select Query by Queue from the Query Method drop-down list, the vhost whose messages you want to query from the Vhost drop-down list, and the queue whose messages you want to query from the Queue drop-down list, set the time range to query, and then click Query.

The following sample code shows the content of the received event:

{
    "datacontenttype":"application/json;charset=utf-8",
    "aliyunaccountid":"143998900779****",
    "data":{
        "envelope":{
            "deliveryTag":98,
            "exchange":"",
            "redeliver":false,
            "routingKey":"housekeeping"
        },
        "body":{
            "Hello":"EventBridge"
        },
        "props":{
            "contentEncoding":"UTF-8",
            "messageId":"f7622d51-e198-41de-a072-77c1ead7****"
        }
    },
    "subject":"acs:amqp:cn-hangzhou:143998900779****:/instances/amqp-cn-zvp2av0m****/vhosts/eb-connect/queues/housekeeping",
    "aliyunoriginalaccountid":"143998900779****",
    "source":"housekeeping.rabbitmq",
    "type":"amqp:Queue:SendMessage",
    "aliyunpublishtime":"2021-08-12T06:56:50.783Z",
    "specversion":"1.0",
    "aliyuneventbusname":"Housekeeping-Connect",
    "id":"8b09e19b-8cbd-4c46-ab77-9cc39c44****",
    "time":"2021-08-12T06:56:40.709Z",
    "aliyunregionid":"cn-hangzhou"
}