Java Code Examples for com.google.api.core.ApiFuture#addListener()

The following examples show how to use com.google.api.core.ApiFuture#addListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlbumDemo.java    From java-photoslibrary with Apache License 2.0 5 votes vote down vote up
private static BiConsumer<AbstractCustomView, List<String>> getOnFileSelectedFn(
    PhotosLibraryClient client, Album album) {
  return (abstractCustomView, mediaItemPaths) -> {
    PhotoListView photoListView = (PhotoListView) abstractCustomView;
    for (String mediaItemPath : mediaItemPaths) {
      try {
        UploadMediaItemRequest.Builder uploadRequestBuilder = UploadMediaItemRequest.newBuilder();
        uploadRequestBuilder
            .setMimeType(URLConnection.guessContentTypeFromName(mediaItemPath))
            .setDataFile(new RandomAccessFile(mediaItemPath, FILE_ACCESS_MODE));
        ApiFuture<UploadMediaItemResponse> uploadResponseFuture =
            client.uploadMediaItemCallable().futureCall(uploadRequestBuilder.build());

        // Show loading dialog while uploading
        LoadingView.getLoadingView().showView();

        uploadResponseFuture.addListener(
            getOnUploadFinished(
                client, photoListView, uploadResponseFuture, album, mediaItemPath),
            MoreExecutors.directExecutor());
      } catch (FileNotFoundException e) {
        LoadingView.getLoadingView().hideView();
        JOptionPane.showMessageDialog(photoListView, e.getMessage());
      }
    }
  };
}
 
Example 2
Source File: CloudPubSubSourceTask.java    From pubsub with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to ack all ids in {@link #deliveredAckIds}.
 */
private void ackMessages() {
  if (deliveredAckIds.size() != 0) {
    AcknowledgeRequest.Builder requestBuilder = AcknowledgeRequest.newBuilder()
        .setSubscription(cpsSubscription);
    final Set<String> ackIdsBatch = new HashSet<>();
    synchronized (deliveredAckIds) {
      requestBuilder.addAllAckIds(deliveredAckIds);
      ackIdsInFlight.addAll(deliveredAckIds);
      ackIdsBatch.addAll(deliveredAckIds);
      deliveredAckIds.clear();
    }
    final ApiFuture<Empty> response = subscriber.ackMessages(requestBuilder.build());
    response.addListener(new Runnable() {
      @Override
      public void run() {
        try {
          response.get();
          log.trace("Successfully acked a set of messages. {}", ackIdsBatch.size());
        } catch (Exception e) {
          deliveredAckIds.addAll(ackIdsBatch);
          log.error("An exception occurred acking messages: " + e);
        } finally {
          ackIdsInFlight.removeAll(ackIdsBatch);
        }
      }
    }, ackExecutor);
  }
}
 
Example 3
Source File: ApiCommonConvertedFutureTestHelper.java    From future-converter with Apache License 2.0 4 votes vote down vote up
@Override
public void addCallbackTo(ApiFuture<String> convertedFuture) {
    ApiFutures.addCallback(convertedFuture, callback);
    convertedFuture.addListener(this::callbackCalled, MoreExecutors.directExecutor());
}