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. Then, you can use event rules to filter the marketing-related events to obtain user registration and logon events and route these events to Message Service (MNS) queues.

Prerequisites

Before you begin, make sure that the following operations are complete:

Background information

This topic shows you 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, the CRM system must describe the attributes and data of the events based on the CloudEvents 1.0 specification. The following table describes the parameters for defining 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 type of the event. 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 subject of the event. The value of this parameter can be up to 128 bytes in length.
    data { \"E-Mail\": \"${email}\" } The content of the event. The additional data of custom application events.
  • Event-driven architecture: You can design and manage services based on the event-driven architecture. This helps you decouple service modules and improve the stability and flexibility of the services. All business data can be mapped to events. All business events are classified into multiple types of events based on business fields. For more information, see EDA.

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. In the Add Custom Event Source section, click Custom Application.
  5. In the Add Custom Event Source panel, enter MyCustomSource in the Name field and Produce marketing-related events in the CRM system in the Description field, select Custom Application from the Event Provider drop-down list, click Create Custom Event Bus, enter marketing in the Custom Event Buses field, and then click OK.

Step 2: Create an 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 marketing and click Create Rule.
  5. On the Create Rule page, perform the following steps:
    1. In the Configure Basic Info step, enter MyCustomRule in the Name field and Route marketing-related events from the CRM system to MNS 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 MyCustomSource from the Event Source drop-down list, specify an event pattern in the Event Pattern Content code editor, and then 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"
          ]
      }
    3. In the Configure Targets step, configure an event target as required. Then, click Create.
      • Service Type: Click Message Service.
      • Queue: Select MyQueue.
      • Message Content: Click Complete Event.

Step 3: Use an SDK to publish events

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

  1. Add a Maven dependency.
    The following sample code shows the Maven dependency:
    <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>eventbridge-client</artifactId>
          <version>1.2.6</version>
    </dependency>
  2. Publish events.
    The following code provides an example on 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 = "{accessKeyId}";
            authConfig.accessKeySecret = "{accessKeySecret}";
            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. Log on to the EventBridge console. Choose Event-driven Operations > Event Buses. Find the custom event bus that you require and click Details in the Operations column. On the Event Bus Detail page, click the Endpoints tab to obtain the endpoint.

Verify the result

You can check whether the marketing-related events about which you are concerned are received 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 MyQueue in the Name column 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****"
    }