文档

配置剪辑完成时的回调

更新时间:

在使用智能媒体服务云剪辑服务时,您可通过设置回调事件及时获取任务处理进展和状态,以便进行其他业务操作。通过阅读本文,您可以了解配置剪辑完成时回调的方法。

回调方式说明

智能媒体服务支持通过HTTP请求MNS队列两种回调方式获取事件通知,详情请参见回调方式说明

实现说明

配置剪辑完成时的回调可以通过自定义单任务回调地址配置全局回调地址来实现:

  • 自定义单任务回调地址:是在提交剪辑合成任务时指定的回调地址,您需要调用SubmitMediaProducingJob接口,传入HTTP(S)回调地址或MNS回调地址。

    • 如果选择HTTP(S)回调,您需要部署一个HTTP服务来接收回调消息,并在剪辑合成请求中配置回调URL;当剪辑合成任务完成时,IMS服务端会对该URL发起HTTP POST请求,任务完成情况内容将通过HTTP Body送达给用户。

    • 如果选择MNS回调,则需要用户在任务完成的地域(Region)配置以ice-callback开头的消息队列,IMS服务器会将消息内容发送至您的MNS消息队列中。

  • 配置全局回调地址:适用于所有媒体处理任务的回调通知。您可以在智能媒体服务的全局设置中指定一个MNS消息队列地址作为全局回调地址。当任何任务完成时,智能媒体服务会将回调通知发送到配置的全局回调地址。

    说明

    仅支持MNS消息的配置。

回调事件的消息结构

关于剪辑合成任务完成时的消息结构,请参见事件列表

前提条件

如果回调方式为MNS队列,您需要先授权MNS访问权限。具体操作,请参见开通消息服务MNS并授权

自定义单任务回调地址

您可以使用自定义单任务回调地址来处理剪辑任务完成时的回调通知。在调用SubmitMediaProducingJob提交剪辑合成任务时,可以在请求中传入一个包含HTTP(S)或MNS回调地址的UserData参数。示例如下所示:

  • HTTP(S)回调地址

    {"NotifyAddress":"http(s)://**.**.***"}
  • MNS回调地址(必须以ice-callback开头的消息队列才可以进行回调)

    {"NotifyAddress":"ice-callback-****"}

配置全局回调地址

  1. 消息服务MNS控制台创建回调消息队列,队列名称必须以ice-callback开头。具体操作,请参见创建队列

  2. 调用SetEventCallback设置事件回调,传入CallbackQueueName,即步骤 1中的队列名称。

  3. 监听步骤 1中的消息队列,并对消息进行处理。示例代码如下所示:

    说明

    以下示例代码中引入的服务端SDK版本号仅供参考,获取最新的版本请参见服务端SDK

    <dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.9</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun.mns</groupId>
        <artifactId>aliyun-sdk-mns</artifactId>
        <version>1.1.9</version>
    </dependency>
    </dependencies>
    import java.util.List;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    import com.aliyun.mns.client.CloudAccount;
    import com.aliyun.mns.client.CloudQueue;
    import com.aliyun.mns.client.MNSClient;
    import com.aliyun.mns.common.ClientException;
    import com.aliyun.mns.common.ServiceException;
    import com.aliyun.mns.common.utils.ServiceSettings;
    import com.aliyun.mns.model.Message;
    
    public class IceProduceMessageConsumerDemo {
    
        public static void main(String[] args) {
    
            // 读取 ~/.aliyun-mns.properties  文件中的mns 配置:
            // mns.accesskeyid, mns.accesskeysecret, mns.queue.endpoint
            // ak/sk 所属子用户拥有 MNS 的完全读写权限
            CloudAccount account = new CloudAccount(
                ServiceSettings.getMNSAccessKeyId(),
                ServiceSettings.getMNSAccessKeySecret(),
                ServiceSettings.getMNSAccountEndpoint());
            MNSClient client = account.getMNSClient(); //this client need only initialize once
    
            // Demo for receive message code
            try{
                CloudQueue queue = client.getQueueRef("ice-callback-test");// replace with your queue name
                List<Message> messages = queue.batchPopMessage(10, 30);
                for(Message popMsg : messages)
                {
                    if (popMsg != null){
                        System.out.println("message handle: " + popMsg.getReceiptHandle());
                        System.out.println("message body: " + popMsg.getMessageBodyAsString());
                        System.out.println("message id: " + popMsg.getMessageId());
                        System.out.println("message dequeue count:" + popMsg.getDequeueCount());
    
                        JSONObject jsonObject = JSON.parseObject(popMsg.getMessageBodyAsString());
    
                        // 只处理 ProduceMediaComplete 类型消息:
                        if("ProduceMediaComplete".equals(jsonObject.getString("EventType"))){
                            JSONObject messageBody = jsonObject.getJSONObject("MessageBody");
    
                            System.out.println("ProjectId:" + messageBody.getString("ProjectId"));
                            System.out.println("JobId:" + messageBody.getString("JobId"));
                            System.out.println("Status:" + messageBody.getString("Status"));
                            System.out.println("MediaId:" + messageBody.getString("MediaId"));
                            System.out.println("MediaURL:" + messageBody.getString("MediaURL"));
    
                            //<<to add your special logic.>>
    
                            //remember to  delete message when consume message successfully.
                            //queue.deleteMessage(popMsg.getReceiptHandle());
                            System.out.println("delete message successfully.\n");
                        }
    
    
                    }
                }
            } catch (ClientException ce)
            {
                System.out.println("Something wrong with the network connection between client and MNS service."
                    + "Please check your network and DNS availability.");
                ce.printStackTrace();
            } catch (ServiceException se)
            {
                if (se.getErrorCode().equals("QueueNotExist"))
                {
                    System.out.println("Queue is not exist.Please create queue before use");
                } else if (se.getErrorCode().equals("TimeExpired"))
                {
                    System.out.println("The request is time expired. Please check your local machine timeclock");
                }
                /*
                you can get more MNS service error code in following link.
                https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html?spm=5176.docmns/api_reference/error_code/error_response
                */
                se.printStackTrace();
            } catch (Exception e)
            {
                System.out.println("Unknown exception happened!");
                e.printStackTrace();
            }
    
            client.close();
        }
    }