This topic describes how to use SDK for Java to create one or more event targets for an event rule.

Sample code:

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.CreateTargetsRequest;
import com.aliyun.eventbridge.models.CreateTargetsResponse;
import com.aliyun.eventbridge.models.EBTargetParam;
import com.aliyun.eventbridge.models.TargetEntry;
import com.google.common.collect.Lists;

public class createTargetsSample {

    private EventBridge eventBridgeClient;

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

    public void createTargetsSample() {
        try {
            TargetEntry targetEntry = new TargetEntry();
            targetEntry.setId("dingdtalk.target");
            targetEntry.setType("acs.dingtalk");
            targetEntry.setEndpoint("https://oapi.dingtalk"
                    + ".com/robot/send? access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460");

            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.setParamList(paramList);

            CreateTargetsRequest createTargetsRequest = new CreateTargetsRequest();
            createTargetsRequest.setRuleName("myRule");
            createTargetsRequest.setEventBusName("mybus");
            createTargetsRequest.setTargets(Arrays.asList(targetEntry));
            CreateTargetsResponse response = eventBridgeClient.createTargets(createTargetsRequest);
            System.out.println("add targets success : " + response);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

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