All Products
Search
Document Center

EventBridge:SDK for Java

Last Updated:Nov 14, 2023

This topic describes how to install the SDK for Java provided by EventBridge and how to use the SDK for Java to publish events. Sample code is also provided in this topic.

Overview

EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code and dependencies that are used by different types of SDKs vary.

  • Management API SDKs: the SDKs that are used to perform operations on the EventBridge console.

  • Data API SDKs: the channel for event data. Only the PutEvents API operation is called by using this type of SDK.

Before you start

Perform the following operations:

Environment preparations

  • Environment requirements

  • Check the Java version

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

Management API SDKs

Add dependency

Add the following dependency to the pom.xml file:

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

Sample code

You can call the corresponding operation in OpenAPI Explorer to automatically generate sample code. For more information, see Automatic generation of SDK examples.

Data API SDKs

Add dependency

Add the following dependency to the pom.xml file:

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

Sample code

Data API SDKs support only the PutEvents API operation. If you want to use the SDK for Java to publish one or more events, refer to the following 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=System.getenv ("ALIBABA_CLOUD_ACCESS_KEY_ID");// The AccessKey ID that you created in the Resource Access Management (RAM) console for identity authentication. For information about how to obtain an AccessKey ID, see Create an AccessKey pair. 
        authConfig.accessKeySecret =System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");// The AccessKey secret that you created in the RAM console for identity authentication. For information about how to obtain an AccessKey secret, see Create 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();
        }
    }
}