This topic describes how to use the SMC Java SDK to call the CreateReplicationJob operation to create a migration task for a migration source.

Background information

You can create a migration task for a migration source by calling the CreateReplicationJob operation. For more information, see CreateReplicationJob.

Note
  • Migration tasks can only be created for migration sources that are in the Available state.
  • Each migration source can only be associated with a single unfinished migration task. Unfinished statuses include Ready, Running, Stopped, Waiting, InError, and Expired.
  • Each Alibaba Cloud account can create up to 100 migration tasks.
  • When the type of the migration destination is image, you must specify the parameters ImageName, SystemDiskSize, and DataDisk.
  • When you perform a migration within a VPC, the VSwitchId parameter is required and the VpcId parameter is optional.

Sample code

The following code can be used to create a migration task and bind it with the usage:production tag. In the migration task, you must specify the VPC and set the system disk size of the target ECS instance to 80 GiB.

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.aliyuncs.smc.model.v20190601.CreateReplicationJobRequest;
import com.aliyuncs.smc.model.v20190601.CreateReplicationJobResponse;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

public class CreateReplicationJob {
    public static void main(String[] args) {
        // Initialize the AcsClient instance, and set the region ID and your AccessKey pair.
        DefaultProfile profile = DefaultProfile.getProfile("<region-Id>", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        // Create an API request and configure its parameters.
        CreateReplicationJobRequest request = new CreateReplicationJobRequest();
        // The ID of the migration source.
        request.setSourceId("s-bp152mkfswviw7sd****");// The ID of VPC.
        request.setVpcId("vpc-bp1vwnn14rqpyiczj****");
        // The ID of the VSwitch in the VPC.
        request.setVSwitchId("vsw-bp1ddbrxdlrcbim46****");
        // The system disk size of the target Alibaba Cloud ECS instance. Unit: GiB.
        request.setSystemDiskSize(80);
        // Specify whether to create a one-time migration task or an incremental migration task.
        request.setRunOnce(true);
        // The tag key-value pair of the migration task.
        List<CreateReplicationJobRequest.Tag> tags = new ArrayList<>();
        CreateReplicationJobRequest.Tag tag = new CreateReplicationJobRequest.Tag();
        tag.setKey("usage");
        tag.setValue("production");
        tags.add(tag);
        request.setTags(tags);
        // The name of the migration task.
        //request.setName("MySourceServer");
        // The description of the migration task.
        //request.setDescription("This_is_a_migration_task");
        // The time when the migration task will be executed.
        //request.setScheduledStartTime("2019-06-04T13:35:00Z");
        // The time when the migration task will expire.
        //request.setValidTime("2019-06-04T13:35:00Z");
        // The ID of the target instance.
        //request.setInstanceId("i-bp12uyz75shn6jsp****");
        // The name of the target Alibaba Cloud image to be delivered in the migration task.
        //request.setImageName("smc-image");
        // To ensure the idempotence of the request, you can use the client to generate a value of up to 64 ASCII characters, and assign the value to ClientToken.
        //request.setClientToken(UUID.randomUUID().toString());
        // The interval of running an incremental migration task.
        //request.setFrequency(12);
        // The index number and size of the data disk of the target Alibaba Cloud ECS instance.
        //List<CreateReplicationJobRequest.DataDisk> datadisks = new ArrayList<>();
        //CreateReplicationJobRequest.DataDisk datadisk = new CreateReplicationJobRequest.DataDisk();
        // The index number of the data disk of the target Alibaba Cloud ECS instance.
        //datadisk.setIndex(1);
        // The size of the data disk of the target Alibaba Cloud ECS instance.
        //datadisk.setSize(100);
        //datadisks.add(datadisk);
        //request.setDataDisks(datadisks);
        // The maximum number of images to be retained by the incremental migration task by default.
        //request.setMaxNumberOfImageToKeep(10);
        // The mode of data transmission network.
        //request.setNetMode(0);
        // Copy the parameter information of the driver.
        //request.setReplicationParameters("{\"bandwidth_limit\":0,\"compress_level\":1,\"checksum\":true}");
        // The type of the target that will be delivered in the migration task.
        //request.setTargetType("Image");

        // Send the request to obtain the return value or handle the exception.
        try {
            CreateReplicationJobResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}          

Result

{
    "RequestId":"C8B26B44-0189-443E-9816-D951F59623A9",
    "JobId":"j-bp152mkfswviwbqj****"
}