This topic describes how to publish an event to EventBridge by using SDK for Java to call API operations.

Prerequisites

Before you start, make sure that the following prerequisites are met:

Publish an event

Sample code:

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;

import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PutEventsSample {

    private 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<>();
        cloudEventList.add(EventBuilder.builder()
                .withId("a5074581-7e74-4e4c-868f-47e7afdf****")
                .withSource(URI.create("acs.oss"))
                .withType("oss:ObjectCreated:PostObject")
                .withSubject("acs:oss:cn-hangzhou:{yourAccountId}:xls-papk/game_apk/123.jpg")
                .withTime(new Date())
                .withJsonStringData("{ \"E-Mail\": \"${email}\" }")
                .withAliyunEventBus("demo-bus")
                .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();
        }
    }
}