This topic shows you how to use ActionTrail SDK for Java.

Preparations

To use Alibaba Cloud SDK for Java and ActionTrail SDK for Java in your project, you must add aliyun-java-sdk-core and aliyun-java-sdk-actiontrail to the pom.xml file in the project directory as dependencies.

<dependencies>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.5.20</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-actiontrail</artifactId>
        <version>1.0.5</version>
    </dependency>
</dependencies>
Note For more information about ActionTrail SDKs for other programming languages, see SDK overview.

Example 1: Query events

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.actiontrail.model.v20200706.LookupEventsRequest;
import com.aliyuncs.actiontrail.model.v20200706.LookupEventsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class DemoLookupEvents {
    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance. 
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",          // The ID of the region. 
            "<your-access-key-id>",      // The AccessKey ID of the RAM user. 
            "<your-access-key-secret>"); // The AccessKey secret of the RAM user. 

        IAcsClient client = new DefaultAcsClient(profile);
        // Create a LookupEvents request. 
        LookupEventsRequest request = new LookupEventsRequest();
        // Specify request parameters. 
        List<LookupEventsRequest.LookupAttribute> lookupAttributes = new ArrayList<>();
        LookupEventsRequest.LookupAttribute lookupAttribute = new LookupEventsRequest.LookupAttribute();
        lookupAttribute.setKey("EventRW");
        lookupAttribute.setValue("Write");
        lookupAttributes.add(lookupAttribute);
        request.setLookupAttributes(lookupAttributes);

        // Send the request and handle the response or exception. 
        LookupEventsResponse response;
        try {
            response = client.getAcsResponse(request);
            System.out.println("RequestId: " + response.getRequestId());
            System.out.println("-------------------------------");
            System.out.println("eventTime, eventId, serviceName, eventName");
            for (Map<Object, Object> event : response.getEvents()) {
                System.out.println(event.get("eventTime") + ", " + event.get("eventId") + ", " + event.get("serviceName") + ", " + event.get("eventName"));
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.actiontrail.model.v20171204.DescribeTrailsRequest;
import com.aliyuncs.actiontrail.model.v20171204.DescribeTrailsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;


public class DemoDescribeTrails {
    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance. 
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",          // The ID of the region. 
            "<your-access-key-id>",      // The AccessKey ID of the RAM user. 
            "<your-access-key-secret>"); // The AccessKey secret of the RAM user. 

        IAcsClient client = new DefaultAcsClient(profile);
        // Create a DescribeTrails request and specify request parameters. 
        DescribeTrailsRequest request = new DescribeTrailsRequest();
        // Send the request and handle the response or exception. 
        DescribeTrailsResponse response;
        try {
            response = client.getAcsResponse(request);
            System.out.println("RequestId: " + response.getRequestId());
            System.out.println("-------------------------------");
            System.out.println("name, trailRegion");
            for (DescribeTrailsResponse.TrailListItem trail : response.getTrailList()) {
                System.out.println(trail.getName() + ", " + trail.getTrailRegion());
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}