All Products
Search
Document Center

Use Alibaba Cloud SDK for Java

Last Updated: May 19, 2022

This topic describes how to use Elastic Container Instance SDK for Java and provides operation examples.

Install the SDK

You can install Elastic Container Instance SDK for Java by adding the following dependencies to the pom.xml file in Maven:

<dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <optional>true</optional>
            <version>4.5.16</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-eci</artifactId>
            <version>1.2.7</version>
        </dependency>
</dependencies>
Note

  • You can also visit the Maven website to download aliyun-java-sdk-eci.

  • To use the new features of Elastic Container Instance, upgrade SDK for Java to the latest version.

Sample code

Sample code that is used to create an elastic container instance

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.eci.model.v20180808.*;
public class CreateContainerGroup {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        CreateContainerGroupRequest request = new CreateContainerGroupRequest();
        request.setRegionId("cn-beijing");
        request.setSecurityGroupId("sg-2ze50xxve5raqetki8at");
        request.setVSwitchId("vsw-2ze462895gttskw2yc6wh");
        request.setContainerGroupName("test");
        List<CreateContainerGroupRequest.Container> containerList = new ArrayList<CreateContainerGroupRequest.Container>();
        CreateContainerGroupRequest.Container container1 = new CreateContainerGroupRequest.Container();
        container1.setImage("nginx");
        container1.setName("nginx");
        containerList.add(container1);
        request.setContainers(containerList);
        try {
            CreateContainerGroupResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

Sample success response:

{"containerGroupId":"eci-bp1h4oxf96kajk0kdfl1","requestId":"8E93976E-E73B-410F-B048-F6E8488E6D51"}

Sample code that is used to delete an elastic container instance

Note

The following sample code shows how to delete a single elastic container instance. For information about how to delete multiple elastic container instances at a time, go to the official GitHub website to view the sample code.

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.eci.model.v20180808.*;
public class DeleteContainerGroup {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        DeleteContainerGroupRequest request = new DeleteContainerGroupRequest();
        request.setRegionId("cn-beijing");
        request.setContainerGroupId("eci-2ze4uq1ecwkm2nwf8odf");
        try {
            DeleteContainerGroupResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

Sample success response:

{"requestId":"30E08AC3-5BEE-4721-9E9C-BE0EC63AB494"}
Notice

If your SDK version does not support some input parameters, you can run the following command to add the parameters and make them take effect:

request.putQueryParameter("VSwitchId", "vsw-uf6mhqg2wiq9iif*****");