All Products
Search
Document Center

EventBridge:Publish and route custom application events

Last Updated:Aug 08, 2023

You can use a custom event bus of EventBridge to receive events that your applications produce and route the events to an Alibaba Cloud service. This topic describes how to publish marketing-related events from a customer relationship management (CRM) system to EventBridge, use event rules to filter the marketing-related events to obtain user registration and logon events, and then route the filtered events to Message Service (MNS) queues.

Prerequisites

Before you start, make sure that the following prerequisites are met:

Background information

This topic describes how to publish custom application events to EventBridge and build an event-driven architecture in a CRM system.

  • CRM system: a system that is used to collect, manage, analyze, and utilize customer information. The system records various interactions between enterprises and customers in the marketing process and the status of various related activities. To publish marketing-related events to EventBridge, you must describe the attributes and data of the events in the CRM system based on the CloudEvents 1.0 specification. The following table describes the parameters that are configured to define a marketing-related event in the CRM system.

    Parameter

    Example

    Description

    source

    crmabc.newsletter

    The event source. The value of this parameter can be up to 128 bytes in length.

    type

    UserSignUp, UserLogin

    The event type. The value of this parameter can be up to 64 bytes in length. Valid values:

    • UserPayOff: The user pays for the order.

    • UserLogin: The user logs on to the system.

    • UserSignUp: The user creates an account.

    subject

    crmabc/users/1234345

    The event subject. The value of this parameter can be up to 128 bytes in length.

    data

    { \"E-Mail\": \"${email}\" }

    The event content. The additional data of custom application events.

  • Event-driven architecture: used to design and manage services. You can use the architecture to decouple service modules and improve service stability and flexibility. In an event-driven architecture, all business data can be mapped to events, and business events are classified into multiple event types based on business fields. For more information, see the "EDA" section of the Terms topic.

Step 1: Create a custom event bus

  1. Log on to the EventBridge console.
  2. In the left-side navigation pane, click Event Buses.
  3. In the top navigation bar, select the region.
  4. On the Event Bus page, click Quickly Create in the Custom Event Buses section.

  5. In the Create Custom Event Bus panel, perform the following operations:

    1. In the Event Bus step, enter marketing in the Name field and Route marketing-related events in the CRM system in the Description field. Then, click Next Step.

    2. In the Event Source step, enter MyCustomSource in the Event Source Name field and Marketing-related events produced in the CRM system in the Description field. Then, select Custom Application from the Event Provider drop-down list and click Next Step.

    3. In the Event Rule step, enter MyCustomRule in the Event Rule Name field and Route marketing-related events from the CRM system to MNS in the Description field. Then, in the Pattern Content editor, enter the event pattern and click Next Step.

      The following sample code shows the event pattern that is used to filter the marketing-related events to obtain user registration or logon events:

      {
          "source": [
              "crmabc.newsletter"
          ],
          "type": [
              "UserSignUp", "UserLogin"
          ]
      }
    4. In the Event Target step, follow the on-screen instructions to configure the parameters and click Create. The following items describe the parameters:

      • Service Type: Select Message Service.

      • Queue: Select MyQueue.

      • Message Body: Click Complete Event.

Step 2: Use an SDK to publish events

Use an SDK to publish marketing-related events from the CRM system to EventBridge.

  1. Add Maven dependencies.

    The following sample code shows the Maven dependencies:

    <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>eventbridge-client</artifactId>
          <version>1.2.6</version>
    </dependency>
  2. Publish events.

    The following sample code shows how to publish events:

    import java.net.URI;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import com.aliyun.eventbridge.EventBridge;
    import com.aliyun.eventbridge.EventBridgeClient;
    import com.aliyun.eventbridge.models.CloudEvent;
    import com.aliyun.eventbridge.models.Config;
    import com.aliyun.eventbridge.models.PutEventsResponse;
    import com.aliyun.eventbridge.util.EventBuilder;
    import com.google.gson.Gson;
    
    public class PutEventsSample {
    
        private final EventBridge eventBridgeClient;
    
        public PutEventsSample() {
            Config authConfig = new Config();
            authConfig.accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
            authConfig.accessKeySecret =System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
            authConfig.endpoint = "{endpoint}";
            eventBridgeClient = new EventBridgeClient(authConfig);
        }
    
        public void putEventsSample() {
            List<CloudEvent> cloudEventList = new ArrayList<CloudEvent>();
            cloudEventList.add(EventBuilder.builder()
                    .withId("9e9b433c-a89b-4918-896b-7e1b7221****")
                    .withSource(URI.create("crmabc.newsletter"))
                    .withType("UserSignUp")
                    .withSubject("crmabc/users/1234345")
                    .withTime(new Date())
                    .withJsonStringData("{ \"E-Mail\": \"${email}\" }")
                    .withAliyunEventBus("marketing")
                    .build());
            PutEventsResponse putEventsResponse = eventBridgeClient.putEvents(cloudEventList);
            System.out.println(new Gson().toJson(putEventsResponse));
        }
    
        public static void main(String[] args){
            PutEventsSample sample = new PutEventsSample();
            try {
                sample.putEventsSample();
            }
            catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    Parameter

    Description

    accessKeyId

    The AccessKey ID of your Alibaba Cloud account.

    accessKeySecret

    The AccessKey secret of your Alibaba Cloud account.

    endpoint

    The endpoint of EventBridge. To obtain the endpoint, log on to the EventBridge console and click Event Buses in the left-side navigation pane. On the page that appears, find the custom event bus that you created and click Details in the Actions column. Then, view the endpoint information in the Endpoints section of the Overview page.

Verify the result

You can check whether MNS receives the routed marketing-related events in the MNS console.

  1. Log on to the MNS console.
  2. In the left-side navigation pane, click Queues.
  3. In the top navigation bar, select a region.
  4. On the Queues page, find the queue to which you routed the events and choose More > Send Messages in the Actions column.

  5. In the Receive Message section of the Quick Experience page, click Receive Message.

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

    {
        "eventId":"9e9b433c-a89b-4918-896b-7e1b7221****",
        "publishTime":1591272433527,
        "Message":{
            "data":"{ \"E-Mail\": \"${email}\" }",
            "id":"9e9b433c-a89b-4918-896b-7e1b7221****",
            "source":"crmabc.newsletter",
            "specversion":"1.0",
            "type":"UserSignUp",
            "subject":"crmabc/users/1234345",
            "time":"2020-06-04T12:07:11.851Z"
        },
        "eventBusName":"marketing",
        "eventBusOwner":"<yourAccountId>",
        "ruleName":"MyCustomRule",
        "eventBusInvoker":"<yourAccountId>",
        "MessageMD5":"D0256972C35F85409E38D176B7E7****"
    }