This topic describes how to use the SDK for Java to create 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.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}";// The AccessKey ID for identity authentication. You can create AccessKey IDs in the RAM console. For more information about how to obtain the ID, see Obtain an AccessKey pair.
authConfig.accessKeySecret = "{accessKeySecret}";// The AccessKey secret for identity authentication. You can create AccessKey secrets in the RAM console. For more information about how to obtain the secret, see Obtain an AccessKey pair.
authConfig.endpoint = "{endpoint}";// The endpoint. For more information, see Regions and endpoints.
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();
}
}
}