Question

When I am configuring the ResumableGDataFileUploader for video upload, even after my program is done uploading, it doesn't end. I have been trying to figure out ways to kill it, but no luck so far. How do I do it?

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;
}
Was it helpful?

Solution

The problem was that, shutdown() was not invoked on the ExecutorService instance created. So, providing an executor of our own to the Builder and shutting it down after the job was done, did the trick.

.....

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();

.....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top