This topic describes how to use the SDK for Java to publish one or more events.

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}";// 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 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();
        }
    }
}