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

Sample code

from aliyunsdkcore.client import AcsClient
from aliyunsdksmc.request.v20190601 import CreateReplicationJobRequest
from aliyunsdksmc.request.v20190601 import StartReplicationJobRequest
from aliyunsdksmc.request.v20190601 import DescribeReplicationJobsRequest
import json
import time
import uuid

# Initialize the AcsClient instance, and set the region ID and your AccessKey pair.
client = AcsClient('<accessKeyId>', '<accessSecret>', '<region-Id>')

# Create a migration task.
def create_replication_job():
    # Create an API request and configure its parameters.
    request = CreateReplicationJobRequest.CreateReplicationJobRequest()
    # The IDs of the migration sources.
    request.set_SourceId('s-bp152mkfswvidyr3****')
    # The system disk size of the target Alibaba Cloud ECS instance. Unit: GiB.
    request.set_SystemDiskSize(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.set_ClientToken(uuid.uuid4())
    # The name of the migration task.
    request.set_Name('smcName')
    # The description of the migration task.
    request.set_Description('setDescription')
    # The ID of the VPC. The specified VPC must have Express Connect or VPN Gateway configured.
    request.set_VpcId('vpc-bp1vwnn14rqpyiczj****')
    # The ID of the VSwitch in the VPC.
    request.set_VSwitchId('vsw-bp1ddbrxdlrcbim46****')

    # Send the request to obtain the return value.
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    print('**********The migration task is created**********')
    return json.loads(response)['JobId']

# Start the migration task.
def start_replication_job(job_id):
    # Create an API request and configure its parameters.
    request = StartReplicationJobRequest.StartReplicationJobRequest()
    # The ID of the migration task.
    request.set_JobId(job_id)

    # Send the request to obtain the return value.
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    print('**********Start the migration task**********')

# Query the status of the migration task.
def describe_replication_jobs(job_ids):
    # Create an API request and configure its parameters.
    request = DescribeReplicationJobsRequest.DescribeReplicationJobsRequest()
    # Set the list of migration task IDs.
    jobIds = [job_ids]
    request.set_JobIds(jobIds)
    # Set the name of the migration task.
    request.set_Name('smcName')
    # Set the page number of the migration task.
    request.set_PageNumber(1)
    # Set the number of entries to return on each page.
    request.set_PageSize(10)
    # Set the list of migration source IDs.
    sourceIds = ['s-bp152mkfswvidyr3****']
    request.set_SourceIds(sourceIds)

    # Send the request to obtain the return value.
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    print('**********View the status of the migration task**********')
    jobslist = json.loads(response)['ReplicationJobs']['ReplicationJob']
    for i in jobslist:
        return (i['Status'])

# Execute the method for creating a migration task.
job_id = create_replication_job()
# Execute the method for starting a migration task.
start_replication_job(job_id)
# Execute the method for querying the status of the migration task every 10 minutes to check whether the migration task is completed.
while True:
    status = describe_replication_jobs(job_id)
    Print('The migration task is in progress...')
    time.sleep(600)
    if status == 'Finished':
        Print ('The migration task is completed.')
        break

Result

{
    "JobId": "j-bp15dpodoftnvm1s****",
    "RequestId": "1042DD41-FEF1-4267-8733-C910B765B9CF"
}
**********The migration task is created**********
{
    "RequestId": "2A7930A1-56B2-4B5D-9874-4CB48F5C5095"
}
**********Start the migration task**********
{
    "PageNumber": 1,
    "TotalCount": 1,
    "PageSize": 10,
    "ReplicationJobs": {
        "ReplicationJob": [{
            "Description": "setDescription",
            "SystemDiskSize": 80,
            "TargetType": "Image",
            "VSwitchId": "vsw-bp1ddbrxdlrcbim46****",
            "SourceId": "s-bp152mkfswvidyr3****",
            "NetMode": 0,
            "VpcId": "vpc-bp1vwnn14rqpyiczj****",
            "Progress": 0.0,
            "Name": "smcName",
            "CreationTime": "2020-01-08T08:27:45Z",
            "Status": "Running",
            "BusinessStatus": "Preparing",
            "JobId": "j-bp15dpodoftnvm1s****",
            "RegionId": "cn-hangzhou",
            "RunOnce": true,
            "StartTime": "2020-01-08T08:27:46Z",
            "ValidTime": "2020-02-07T08:27:45Z"
        }]
    },
    "RequestId": "B62A8AEE-95AA-400F-8A6C-4822E3F4B690"
}
**********View the status of the migration task**********
The migration task is in progress...
{
    "PageNumber": 1,
    "TotalCount": 1,
    "PageSize": 10,
    "ReplicationJobs": {
        "ReplicationJob": [{
            "Description": "setDescription",
            "SystemDiskSize": 80,
            "TargetType": "Image",
            "VSwitchId": "vsw-bp1ddbrxdlrcbim46****",
            "SourceId": "s-bp152mkfswvidyr3****",
            "NetMode": 0,
            "VpcId": "vpc-bp1vwnn14rqpyiczj****",
            "Progress": 0.0,
            "Name": "smcName",
            "CreationTime": "2020-01-08T08:27:45Z",
            "Status": "Running",
            "BusinessStatus": "Preparing",
            "JobId": "j-bp15dpodoftnvm1s****",
            "RegionId": "cn-hangzhou",
            "RunOnce": true,
            "StartTime": "2020-01-08T08:27:46Z",
            "ValidTime": "2020-02-07T08:27:45Z"
        }]
    },
    "RequestId": "B62A8AEE-95AA-400F-8A6C-4822E3F4B690"
}
**********View the status of the migration task**********
The migration task is completed.