本文介绍如何使用Java SDK删除一个事件规则的一个或多个投递目标。

示例代码如下:

import java.util.Arrays;

import com.aliyun.eventbridge.EventBridge;
import com.aliyun.eventbridge.EventBridgeClient;
import com.aliyun.eventbridge.models.Config;
import com.aliyun.eventbridge.models.DeleteTargetsRequest;
import com.aliyun.eventbridge.models.DeleteTargetsResponse;

public class deleteTargetsSample {

    private EventBridge eventBridgeClient;

    public deleteTargetsSample() {
        Config authConfig = new Config();
    authConfig.accessKeyId = "{accessKeyId}";// AccessKey ID,阿里云身份验证,在RAM控制台创建。获取方式,请参见获取AccessKey。
        authConfig.accessKeySecret = "{accessKeySecret}";// AccessKey Secret,阿里云身份验证,在RAM控制台创建。获取方式,请参见获取AccessKey。
        authConfig.endpoint = "{endpoint}";// 接入点信息。具体信息,请参见地域和接入点。
        eventBridgeClient = new EventBridgeClient(authConfig);
    }

    public void deleteTargetsSample() {
        try {
            DeleteTargetsRequest deleteTargetsResponse = new DeleteTargetsRequest();
            deleteTargetsResponse.setRuleName("myRule");
            deleteTargetsResponse.setEventBusName("mybus");
            deleteTargetsResponse.setTargetIds(Arrays.asList("httpTarget2"));
            DeleteTargetsResponse response = eventBridgeClient.deleteTargets(deleteTargetsResponse);
            System.out.println("remove targets success : " + response);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

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