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 these events to Message Service (MNS) queues.

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, 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 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: used to design and manage services. You can use this architecture to decouple services and improve the stability and flexibility of services. 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 EDA.

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 a region.
  4. On the Event Bus page, click Create in the Custom Event Buses section.
  5. Complete the Create Custom Event Bus wizard.
    1. In the Event Bus step, enter marketing in the Name field and Route marketing-related events from a 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 Produce marketing-related events in a CRM system in the Description field, select Custom Application from the Event Provider drop-down list, and then click Next Step.
    3. In the Event Rule step, enter MyCustomRule in the Event Rule Name field and Route marketing-related events from a CRM system to MNS in the Description field, 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"
          ]
      }
    4. In the Event Target step, configure the event target and click Create.
      • Service Type: Select Message Service.
      • Queue: Select MyQueue.
      • Message Content: 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 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 and click Event Buses in the left-side navigation pane. Find the custom event bus that you created and click Details in the Actions column. On the Event Bus Overview page, view the endpoints in the Endpoints section.

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 a routed 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****"
    }