本文介绍如何使用Java SDK创建一个事件规则。

示例代码如下:

import java.util.Arrays;
import java.util.List;

import com.aliyun.eventbridge.EventBridge;
import com.aliyun.eventbridge.EventBridgeClient;
import com.aliyun.eventbridge.models.Config;
import com.aliyun.eventbridge.models.CreateRuleRequest;
import com.aliyun.eventbridge.models.CreateRuleResponse;
import com.aliyun.eventbridge.models.EBTargetParam;
import com.aliyun.eventbridge.models.TargetEntry;
import com.aliyun.eventbridge.util.CommonStatus;
import com.google.common.collect.Lists;

public class createEventRuleSample {

    private EventBridge eventBridgeClient;

    public createEventRuleSample() {
        Config authConfig = new Config();
        authConfig.accessKeyId = "{accessKeyId}";
        authConfig.accessKeySecret = "{accessKeySecret}";
        authConfig.endpoint = "{endpoint}";
        eventBridgeClient = new EventBridgeClient(authConfig);
    }

    public void createEventRuleSample() {
        try {
            CreateRuleRequest createEventRuleRequest = new CreateRuleRequest();
            EBTargetParam param1 = new EBTargetParam();
            param1.setResourceKey("URL");
            param1.setForm("CONSTANT");
            param1.setValue("https://oapi.dingtalk"
                    + ".com/robot/send?access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460");

            EBTargetParam param2 = new EBTargetParam();
            param2.setResourceKey("SecretKey");
            param2.setForm("CONSTANT");
            param2.setValue("SEC121a71ff304a65b4f7c**************1f4d9f6c1ca514300d15234");

            EBTargetParam param3 = new EBTargetParam();
            param3.setResourceKey("Body");
            param3.setForm("TEMPLATE");
            param3.setValue("{\n" + "    \"key\":\"$.source\",\n" + "    \"value\":\"$.data\"\n" + "}");
            param3.setTemplate("{\"msgtype\": \"text\",\"text\": " + "{\"content\": \"Hello:${key}\"}}");

            List<EBTargetParam> paramList = Lists.newArrayList(param1, param2, param3);
            TargetEntry targetEntry = new TargetEntry();
            targetEntry.setId("dingdtalk.target");
            targetEntry.setType("acs.dingtalk");
            targetEntry.setParamList(paramList);
            targetEntry.setEndpoint("https://oapi.dingtalk"
                    + ".com/robot/send?access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460");

            List<TargetEntry> targetEntryList = Arrays.asList(targetEntry);
            createEventRuleRequest.setRuleName("myRule");
            createEventRuleRequest.setEventBusName("mybus");
            createEventRuleRequest.setFilterPattern(
                    "{\"source\":[\"crmabc.newsletter\"],\"type\":[\"UserSignUp\", \"UserLogin\"]}");
            createEventRuleRequest.setStatus(CommonStatus.ENABLE.getKey());
            createEventRuleRequest.setTargets(targetEntryList);
            CreateRuleResponse response = eventBridgeClient.createRule(createEventRuleRequest);
            System.out.println("create rule success : " + response);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        createEventRuleSample eventRuleSamples = new createEventRuleSample();
        try {
            eventRuleSamples.createEventRuleSample();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}