This topic describes how to use the SMC Java SDK to call the CreateReplicationJob and StartReplicationJob operations to create and run a one-time server migration task, and then call the DescribeReplicationJobs operation to query information about the migration task.
Prerequisites
- You have imported the migration sources. For more information, see Step 1: Import migration source information.
- You have added the Maven dependencies to the pom.xml file. For more information, see Install the Java SDK.
Sample code
package com.aliyun.smc;
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.*;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Demo {
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 a migration task.
String jobId = createReplicationJob(client);
// Start the migration task.
startReplicationJob(client,jobId);
// Query the status of the migration task.
while(true){
String result = describeReplicationJobs(client,jobId);
System.out.println("The migration task is in progress...");
if("Finished".equals(result)){
System.out.println("The migration task is completed.") ;
break;
}
try {
Thread.sleep(600000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static String createReplicationJob (IAcsClient client){
// Create an API request and configure its parameters.
CreateReplicationJobRequest request = new CreateReplicationJobRequest();
// The ID of the migration source.
request.setSourceId("s-bp152mkfswviw7sd****");
// The system disk size of the target Alibaba Cloud ECS instance. Unit: GiB.
request.setSystemDiskSize(80);
// 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 description of the migration task.
request.setDescription("smcDescription");
// The name of the migration task.
request.setName("MigrationTask");
// Specify whether to create a one-time migration task or an incremental migration task.
request.setRunOnce(true);
// The time when the migration task will be executed.
//request.setScheduledStartTime("2019-06-04T13:35:00Z");
// 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 type of the target that will be delivered in the migration task.
//request.setTargetType("Image");
// The ID of the VSwitch in the VPC.
request.setVSwitchId("vsw-bp1ddbrxdlrcbim46****");
// The time when the migration task will expire.
//request.setValidTime("2019-06-04T13:35:00Z");
// The ID of the VPC. The specified VPC must have Express Connect or VPN Gateway configured.
request.setVpcId("vpc-bp1vwnn14rqpyiczj****");
// Send the request to obtain the return value or handle the exception.
CreateReplicationJobResponse response = null;
try {
response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
System.out.println("**********The migration task is created**********");
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return response.getJobId();
}
private static void startReplicationJob(IAcsClient client,String jobId){
// Create an API request and configure its parameters.
StartReplicationJobRequest request = new StartReplicationJobRequest();
// The ID of the migration task.
request.setJobId(jobId);
// Send the request to obtain the return value or handle the exception.
try {
StartReplicationJobResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
System.out.println("**********Start the migration task**********");
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
private static String describeReplicationJobs(IAcsClient client,String jobId){
// Create an API request and configure its parameters.
DescribeReplicationJobsRequest request = new DescribeReplicationJobsRequest();
// Set the page number of the migration task.
request.setPageNumber(1);
// Set the number of entries to return on each page.
request.setPageSize(10);
// Set the name of the migration task.
request.setName("MigrationTask");
// Set the list of migration task IDs.
List<String> jobIds = new ArrayList<>();
jobIds.add(jobId);
request.setJobIds(jobIds);
// Set the list of migration source IDs.
List<String> sourceIds = new ArrayList<>();
sourceIds.add("s-bp152mkfswviw7sd****");
request.setSourceIds(sourceIds);
// Send the request to obtain the return value or handle the exception.
DescribeReplicationJobsResponse response = null;
try {
response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
System.out.println("**********Query the status of the migration task**********");
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
String status = null;
List<DescribeReplicationJobsResponse.ReplicationJob> replicationJobList = response.getReplicationJobs();
for(DescribeReplicationJobsResponse.ReplicationJob rl : replicationJobList){
status = rl.getStatus();
}
return status;
}
}
Result
{
"requestId": "667E03A3-7D16-4822-8B8D-1F3883B25903",
"jobId": "j-bp152mkfswviwbqj****"
}
**********The migration task is created**********
{
"requestId": "3A236693-5FF9-4F76-8F7D-A405113A6B55"
}
**********Start the migration task**********
{
"requestId": "0E9F3604-D010-4EDE-995C-F123ED9CEB5B",
"totalCount": 1,
"pageNumber": 1,
"pageSize": 10,
"replicationJobs": [{
"jobId": "j-bp152mkfswviwbqj****",
"sourceId": "s-bp152mkfswviw7sd****",
"name": "MigrationTask",
"description": "smcDescription",
"regionId": "cn-hangzhou",
"targetType": "Image",
"status": "Running",
"businessStatus": "Preparing",
"progress": 0.0,
"creationTime": "2020-01-08T06:44:04Z",
"validTime": "2020-02-07T06:44:05Z",
"startTime": "2020-01-08T06:44:05Z",
"netMode": 0,
"systemDiskSize": 80,
"vpcId": "vpc-bp1vwnn14rqpyiczj****",
"vSwitchId": "vsw-bp1ddbrxdlrcbim4****j",
"runOnce": true,
"dataDisks": [],
"replicationJobRuns": []
}]
}
**********Query the status of the migration task**********
The migration task is in progress...
{
"requestId": "0E9F3604-D010-4EDE-995C-F123ED9CEB5B",
"totalCount": 1,
"pageNumber": 1,
"pageSize": 10,
"replicationJobs": [{
"jobId": "j-bp152mkfswviwbqj****",
"sourceId": "s-bp152mkfswviw7sd****",
"name": "MigrationTask",
"description": "smcDescription",
"regionId": "cn-hangzhou",
"targetType": "Image",
"status": "Running",
"businessStatus": "Preparing",
"progress": 0.0,
"creationTime": "2020-01-08T06:44:04Z",
"validTime": "2020-02-07T06:44:05Z",
"startTime": "2020-01-08T06:44:05Z",
"netMode": 0,
"systemDiskSize": 80,
"vpcId": "vpc-bp1vwnn14rqpyiczj****",
"vSwitchId": "vsw-bp1ddbrxdlrcbim46****",
"runOnce": true,
"dataDisks": [],
"replicationJobRuns": []
}]
}
**********Query the status of the migration task**********
The migration task is in progress...
{
"requestId": "0E9F3604-D010-4EDE-995C-F123ED9CEB5B",
"totalCount": 1,
"pageNumber": 1,
"pageSize": 10,
"replicationJobs": [{
"jobId": "j-bp152mkfswviwbqj****",
"sourceId": "s-bp152mkfswviw7sd****",
"name": "MigrationTask",
"description": "smcDescription",
"regionId": "cn-hangzhou",
"targetType": "Image",
"status": "Finished",
"businessStatus": "Preparing",
"progress": 0.0,
"creationTime": "2020-01-08T06:44:04Z",
"validTime": "2020-02-07T06:44:05Z",
"startTime": "2020-01-08T06:44:05Z",
"netMode": 0,
"systemDiskSize": 80,
"vpcId": "vpc-bp1vwnn14rqpyiczj****",
"vSwitchId": "vsw-bp1ddbrxdlrcbim46****",
"runOnce": true,
"dataDisks": [],
"replicationJobRuns": []
}]
}
**********Query the status of the migration task**********
The migration task is completed.