Java SDK user guide

Updated at:

This topic describes how to install the Java SDK, provides sample code, and shows you how to publish events using the Java SDK.

SDK introduction

The EventBridge SDKs are divided into management API SDKs and data API SDKs. The sample code and dependencies are different for each type of SDK.

  • Management API SDK: Used to perform operations that are also available on the console.

  • Data API SDK: Used as a data channel for events. Currently, only the PutEvents operation is supported.

Prerequisites

You have completed the following operations:

Prepare the environment

  • Environment requirements

    • Install JDK 1.8 or later. For more information, see Install JDK.

    • Install Maven 2.5 or later. For more information, see Install Maven.

  • Check the language version

    Run the java -version command to check your Java version.

Management API SDK

Install the dependency library

Add the following dependency to the pom.xml file.

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>eventbridge20200401</artifactId>
  <version>2.0.0</version>
</dependency> 

SDK example

You can call an API operation in the OpenAPI Explorer to automatically generate an SDK example. For more information, see Automatically generate an SDK example.

Data API SDK

Install the dependency library

Add the following dependency to the pom.xml file.

<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>eventbridge-client</artifactId>
      <version>1.2.6</version>
</dependency>

SDK example

The Data API SDK currently supports only the PutEvents operation. The following sample code shows you how to publish one or more events using the Java SDK.

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 = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");// The AccessKey ID for Alibaba Cloud identity verification. Create it in the Resource Access Management (RAM) console. For more information, see Create an AccessKey.
        authConfig.accessKeySecret =System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");// The AccessKey secret for Alibaba Cloud identity verification. Create it in the RAM console. For more information, see Create an AccessKey.
        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();
        }
    }
}