上传完成后,ResumableGDataFileUploader不会结束

时间:2014-01-24 12:05:11

标签: java youtube-api gdata-api

当我为视频上传配置ResumableGDataFileUploader时,即使我的程序上传完毕,它也不会结束。我一直试图找出杀死它的方法,但到目前为止还没有运气。我该怎么做?

private String postYoutubeVideo() throws IOException, ServiceException, GeneralSecurityException, InterruptedException, ExecutionException, TimeoutException {

    String resumableVideoUploadURL = "http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads";
    // constants

    UploadProgressListener progressListener = new UploadProgressListener();
    YouTubeService service = getYouTubeService();       
    File file = getFile();
    String mimeType = new MimetypesFileTypeMap().getContentType(file);
    MediaFileSource mediaFile = new MediaFileSource(file, mimeType);
    VideoEntry newEntry = new VideoEntry();
    YouTubeMediaGroup mediaGroup = newEntry.getOrCreateMediaGroup();

    mediaGroup.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, category));
    mediaGroup.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, devtag));
    mediaGroup.setTitle(new MediaTitle());
    mediaGroup.getTitle().setPlainTextContent(title);
    mediaGroup.setKeywords(new MediaKeywords());
    mediaGroup.getKeywords().addKeyword(keyword);
    mediaGroup.setDescription(new MediaDescription());
    mediaGroup.getDescription().setPlainTextContent(description);
    mediaGroup.setPrivate(false);
    newEntry.setMediaSource(mediaFile);

    ResumableGDataFileUploader resumableUploader = new ResumableGDataFileUploader.Builder(service, new URL(
        resumableVideoUploadURL), mediaFile, newEntry)
        .title(title)
        .chunkSize(chunkSize)
        .build();

    resumableUploader.start();

    while (!resumableUploader.isDone())
    {
        Thread.sleep(progressInterval);
    }

    ResponseMessage response = resumableUploader.getResponse();
    VideoEntry newVideo = new VideoEntry();
    newVideo.parseAtom(new ExtensionProfile(), response.getInputStream());
    extMsgId = newVideo.getHtmlLink().getHref();


    return extMsgId;
}

1 个答案:

答案 0 :(得分:0)

问题是,在创建的ExecutorService实例上没有调用shutdown()。 因此,在完成工作后向Builder提供我们自己的执行程序并将其关闭,就可以解决问题了。

.....

ExecutorService executorService = Executors.newSingleThreadExecutor();

.....

ResumableGDataFileUploader resumableUploader = new ResumableGDataFileUploader.Builder(service, new URL(
resumableVideoUploadURL), mediaFile, newEntry)
    .title(title)
    .chunkSize(chunkSize)
    .executor(executorService)
    .build();

resumableUploader.start();

while (!resumableUploader.isDone()) {
    Thread.sleep(progressInterval);
}

if (ResumableHttpFileUploader.UploadState.COMPLETE.equals(resumableUploader.getUploadState())) {
    ResponseMessage response = resumableUploader.getResponse();
    VideoEntry newVideo = new VideoEntry();
    newVideo.parseAtom(new ExtensionProfile(), response.getInputStream());
    extMsgId = newVideo.getHtmlLink().getHref();
}

executorService.shutdown();

.....