Java Code Examples for com.google.common.util.concurrent.Futures#successfulAsList()

The following examples show how to use com.google.common.util.concurrent.Futures#successfulAsList() . 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: ScrapeLocationsGraph.java    From curiostack with MIT License 7 votes vote down vote up
@Produces
@FetchedPostPage
static ListenableFuture<List<@Nullable AggregatedHttpResponse>> fetchPosts(
    @HashtagPage List<@Nullable AggregatedHttpResponse> hashtagPages,
    SharedDataExtractor sharedDataExtractor,
    WebClient instagramClient,
    ServiceRequestContext ctx) {
  return Futures.successfulAsList(
      hashtagPages.stream()
          .filter(Objects::nonNull)
          .map(page -> sharedDataExtractor.extractSharedData(page, TagPage.class))
          .flatMap(
              page ->
                  page
                      .getEntryData()
                      .getTagPage()
                      .get(0)
                      .getGraphql()
                      .getHashtag()
                      .getPosts()
                      .getEdges()
                      .stream())
          .map(
              post ->
                  toListenableFuture(
                      instagramClient
                          .get("/p/" + post.getNode().getShortcode() + '/')
                          .aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc())))
          .collect(toImmutableList()));
}
 
Example 2
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
private void dealResult(List<ListenableFuture<Response>> futures, final List<String> urls, final String type) {
    final ListenableFuture<List<Response>> future = Futures.successfulAsList(futures);
    future.addListener(new Runnable() {
        @Override
        public void run() {
            List<Response> list = getUnchecked(future);
            if (list == null) {
                logger.error("{} notify server error", type);
                Monitor.notifyServerFailCounter.inc(urls.size());
                return;
            }
            for (int i = 0; i < list.size(); ++i) {
                Response response = list.get(i);
                if (response == null || response.getStatusCode() != HttpStatus.SC_OK) {
                    int code = response == null ? 0 : response.getStatusCode();
                    logger.error("{} notify server failed, code {}, {}", type, code, urls.get(i));
                    Monitor.notifyServerFailCounter.inc();
                }
            }
        }
    }, Constants.CURRENT_EXECUTOR);
}
 
Example 3
Source File: ScrapeLocationsGraph.java    From curiostack with MIT License 6 votes vote down vote up
@Produces
@UserPage
static ListenableFuture<List<@Nullable AggregatedHttpResponse>> fetchUserPages(
    ScrapeLocationsRequest request, WebClient instagramClient, ServiceRequestContext ctx) {
  return Futures.successfulAsList(
      request.getUsernameList().stream()
          .map(
              username ->
                  toListenableFuture(
                      instagramClient
                          .get('/' + username + '/')
                          .aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc())))
          .collect(toImmutableList()));
}
 
Example 4
Source File: ListLandmarksGraph.java    From curiostack with MIT License 6 votes vote down vote up
@Produces
static ListenableFuture<List<List<Landmark>>> fetchDbLandmarks(
    S2CellUnion coveredCells, DSLContext db, @ForDatabase ListeningExecutorService dbExecutor) {
  return Futures.successfulAsList(
      Streams.stream(coveredCells)
          .map(
              cell ->
                  dbExecutor.submit(
                      () ->
                          db.selectFrom(LANDMARK)
                              .where(
                                  LANDMARK
                                      .S2_CELL
                                      .ge(ULong.valueOf(cell.rangeMin().id()))
                                      .and(
                                          LANDMARK.S2_CELL.le(
                                              ULong.valueOf(cell.rangeMax().id()))))
                              .fetchInto(Landmark.class)))
          .collect(toImmutableList()));
}
 
Example 5
Source File: DeviceApiController.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/device/token/{deviceToken}/attributes/shadow", method = RequestMethod.GET, produces = "application/json")
public DeferredResult<ResponseEntity> getDeviceAttributesShadow(@PathVariable("deviceToken") String deviceToken) {
  DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
  HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
  if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
    DeviceId _deviceId = ctx.getDevice().getId();
    try {
      List<ListenableFuture<List<AttributeKvEntry>>> futures = new ArrayList<>();
      Arrays.asList(DataConstants.ALL_SCOPES)
          .forEach(attributeType -> futures.add(attributesService.findAll(_deviceId, attributeType)));
      ListenableFuture<List<List<AttributeKvEntry>>> successfulAsList = Futures.successfulAsList(futures);
      List<AttributeKvEntry> result = new ArrayList<>();
      successfulAsList.get().forEach(r -> result.addAll(r));
      List<ThingsKVData> collect = result.stream().map(attribute -> new ThingsKVData(attribute.getKey(), attribute.getValue())).collect(Collectors.toList());
      responseWriter.setResult(new ResponseEntity<>(collect, HttpStatus.OK));

    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
      responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
    }
  } else {
    responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
  }

  return responseWriter;
}
 
Example 6
Source File: ListeningClientsServiceImpl.java    From qconfig with MIT License 5 votes vote down vote up
private <T> ListenableFuture<Set<T>> parseData(final List<String> serverUrls, final List<ListenableFuture<Response>> futures, final Function<String, Set<T>> parseFunction) {
    final SettableFuture<Set<T>> result = SettableFuture.create();
    ListenableFuture<List<Response>> responseFuture = Futures.successfulAsList(futures);
    Futures.addCallback(responseFuture, new FutureCallback<List<Response>>() {
        @Override
        public void onSuccess(List<Response> responses) {
            Set<T> allClients = Sets.newHashSet();
            for (int i = 0; i < responses.size(); ++i) {
                Response response = responses.get(i);
                if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
                    String responseStr = null;
                    try {
                        responseStr = response.getResponseBody("utf8");
                    } catch (IOException e) {
                        logger.warn("get listening client response from server error", e);
                    }
                    allClients.addAll(parseFunction.apply(responseStr));
                } else {
                    logger.warn("get listening clients error with {}", serverUrls.get(i));
                }
            }
            result.set(allClients);
        }

        @Override
        public void onFailure(Throwable t) {
            result.setException(t);
        }
    }, executor);

    return result;
}
 
Example 7
Source File: PublishingReleaseStatus.java    From qconfig with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<?> work() {
    int publishingBatchNum = statusInfo.getFinishedBatchNum() + 1;
    final List<Host> machines = statusInfo.getBatches().get(publishingBatchNum);
    preparePush(machines);
    ListenableFuture<?> f1 = executor.submit(new Runnable() {
        @Override
        public void run() {
            push(machines);
        }
    });

    ListenableScheduledFuture<?> f2 = executor.schedule(new Runnable() {
        @Override
        public void run() {
            push(machines);
        }
    }, DELAY_TIME_MS, TimeUnit.MILLISECONDS);

    ListenableFuture<List<Object>> workFuture = Futures.successfulAsList(f1, f2);
    workFuture.addListener(new Runnable() {
        @Override
        public void run() {
            accept(Command.next);
        }
    }, Constants.CURRENT_EXECUTOR);
    return workFuture;
}
 
Example 8
Source File: ScrapeLocationsGraph.java    From curiostack with MIT License 5 votes vote down vote up
@Produces
@HashtagPage
static ListenableFuture<List<@Nullable AggregatedHttpResponse>> fetchHashtags(
    ScrapeLocationsRequest request, WebClient instagramClient, ServiceRequestContext ctx) {
  return Futures.successfulAsList(
      request.getHashtagList().stream()
          .map(
              hashtag ->
                  toListenableFuture(
                      instagramClient
                          .get("/explore/tags/" + hashtag + '/')
                          .aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc())))
          .collect(toImmutableList()));
}
 
Example 9
Source File: ScrapeLocationsGraph.java    From curiostack with MIT License 5 votes vote down vote up
@Produces
@LocationPage
static ListenableFuture<List<@Nullable AggregatedHttpResponse>> fetchLocations(
    @FetchedPostPage List<@Nullable AggregatedHttpResponse> postPages,
    @UserPage List<@Nullable AggregatedHttpResponse> userPages,
    WebClient instagramClient,
    SharedDataExtractor sharedDataExtractor,
    ServiceRequestContext ctx) {
  return Futures.successfulAsList(
      Stream.concat(
              userPages.stream()
                  .filter(Objects::nonNull)
                  .map(page -> sharedDataExtractor.extractSharedData(page, ProfilePage.class))
                  .flatMap(ScrapeLocationsGraph::getLocationPageIds),
              postPages.stream()
                  .filter(Objects::nonNull)
                  .map(page -> sharedDataExtractor.extractSharedData(page, PostPage.class))
                  .map(ScrapeLocationsGraph::getLocationPageId)
                  .filter(s -> !s.isEmpty()))
          .distinct()
          .map(
              locationId ->
                  toListenableFuture(
                      instagramClient
                          .get("/explore/locations/" + locationId + '/')
                          .aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc())))
          .collect(toImmutableList()));
}
 
Example 10
Source File: UiRequestHandler.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof RequestData)) {
        super.channelRead(ctx, msg);
        return;
    }

    @SuppressWarnings("unchecked")
    RequestData<String> inputData = (RequestData<String>) msg;

    UiConnection uiConnection = uiConnectionStore.register(ctx.channel());

    if (inputData.getType() == CommandCode.REQ_TYPE_CANCEL.getCode()) {
        cancelRequest(uiConnection);
        return;
    }

    Optional<CommunicateCommand> command = commandStore.getCommunicateCommand(inputData.getType());
    if (!command.isPresent()) {
        ctx.channel().writeAndFlush(UiResponses.createNoCommandResponse(inputData));
        return;
    }

    CommunicateCommand communicateCommand = command.get();
    if (!communicateCommand.isSupportMulti() && inputData.getAgentServerInfos().size() > 1) {
        ctx.channel().writeAndFlush(UiResponses.createNotSupportMultiResponse(inputData));
        return;
    }

    CommunicateCommandProcessor<?> processor = communicateCommand.getProcessor();
    Optional<? extends RequestData<?>> requestDataOptional = preProcessor(processor, inputData, ctx);
    if (!requestDataOptional.isPresent()) {
        return;
    }

    RequestData<?> requestData = requestDataOptional.get();
    List<AgentConnection> agentConnections = Lists.newArrayListWithCapacity(requestData.getAgentServerInfos().size());
    List<String> lessVersionAgents = Lists.newArrayList();
    List<String> noConnectionAgents = Lists.newArrayList();
    for (AgentServerInfo agentServerInfo : requestData.getAgentServerInfos()) {
        Optional<AgentConnection> agentConnection = agentConnectionStore.getConnection(agentServerInfo.getAgentId());
        if (agentConnection.isPresent()) {
            if (agentConnection.get().getVersion() >= communicateCommand.getMinAgentVersion()) {
                agentConnections.add(agentConnection.get());
            } else {
                lessVersionAgents.add(agentServerInfo.getAgentId());
            }
        } else {
            noConnectionAgents.add(agentServerInfo.getAgentId());
        }
    }

    noConnectionAgents.stream()
            .map(noConnectionAgent -> UiResponses.createNoConnectionResponse(noConnectionAgent, requestData))
            .forEach(uiConnection::write);
    lessVersionAgents.stream().
            map(lessVersionAgent -> UiResponses.createLessVersionResponse(lessVersionAgent, requestData))
            .forEach(uiConnection::write);

    if (agentConnections.isEmpty()) {
        uiConnection.write(UiResponses.createFinishResponse(requestData));
        return;
    }

    List<Session> sessions = agentConnections.stream()
            .map((agentConnection -> sendMessage(command.get(), requestData, processor, agentConnection, uiConnection)))
            .collect(Collectors.toList());

    ListenableFuture<List<Session.State>> sessionsFuture = Futures.successfulAsList(sessions.stream().map(Session::getEndState).collect(Collectors.toList()));
    sessionsFuture.addListener(() -> uiConnection.write(UiResponses.createFinishResponse(requestData)), MoreExecutors.directExecutor());
}
 
Example 11
Source File: DeviceApiController.java    From iotplatform with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/{deviceToken}/attributes", method = RequestMethod.GET, produces = "application/json")
public DeferredResult<ResponseEntity> getDeviceAttributes(@PathVariable("deviceToken") String deviceToken,
    @RequestParam(value = "clientKeys", required = false, defaultValue = "") String clientKeys,
    @RequestParam(value = "sharedKeys", required = false, defaultValue = "") String sharedKeys,
    @RequestParam(value = "serverKeys", required = false, defaultValue = "") String serverKeys) {
  DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
  HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
  if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
    DeviceId _deviceId = ctx.getDevice().getId();
    try {
      List<ListenableFuture<List<AttributeKvEntry>>> futures = new ArrayList<>();
      if (StringUtils.isEmpty(clientKeys) && StringUtils.isEmpty(sharedKeys) && StringUtils.isEmpty(serverKeys)) {
        Arrays.asList(DataConstants.ALL_SCOPES)
            .forEach(attributeType -> futures.add(attributesService.findAll(_deviceId, attributeType)));
      } else {
        Set<String> clientKeySet = !StringUtils.isEmpty(clientKeys)
            ? new HashSet<>(Arrays.asList(clientKeys.split(","))) : new HashSet<>();
        Set<String> sharedKeySet = !StringUtils.isEmpty(sharedKeys)
            ? new HashSet<>(Arrays.asList(sharedKeys.split(","))) : new HashSet<>();
        Set<String> serverKeySet = !StringUtils.isEmpty(serverKeys)
            ? new HashSet<>(Arrays.asList(serverKeys.split(","))) : new HashSet<>();
        clientKeySet.addAll(sharedKeySet);
        clientKeySet.addAll(serverKeySet);
        Arrays.asList(DataConstants.ALL_SCOPES)
            .forEach(attributeType -> futures.add(attributesService.find(_deviceId, attributeType, clientKeySet)));
      }
      ListenableFuture<List<List<AttributeKvEntry>>> successfulAsList = Futures.successfulAsList(futures);
      List<AttributeKvEntry> result = new ArrayList<>();
      successfulAsList.get().forEach(r -> result.addAll(r));
      List<ThingsKVData> collect = result.stream().map(attribute -> new ThingsKVData(attribute.getKey(), attribute.getValue())).collect(Collectors.toList());
      responseWriter.setResult(new ResponseEntity<>(collect, HttpStatus.OK));

    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
      responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
    }

    // if (StringUtils.isEmpty(clientKeys) && StringUtils.isEmpty(sharedKeys))
    // {
    // request = new BasicGetAttributesRequest(0);
    // } else {
    // Set<String> clientKeySet = !StringUtils.isEmpty(clientKeys)
    // ? new HashSet<>(Arrays.asList(clientKeys.split(","))) : null;
    // Set<String> sharedKeySet = !StringUtils.isEmpty(sharedKeys)
    // ? new HashSet<>(Arrays.asList(sharedKeys.split(","))) : null;
    // request = new BasicGetAttributesRequest(0, clientKeySet, sharedKeySet);
    // }
    // process(ctx, request);
  } else {
    responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
  }

  return responseWriter;
}
 
Example 12
Source File: WaitForCompletionTaskRunner.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public <R> ListenableFuture<List<R>> submitTasks(List<ListenableFuture<R>> futures) {
  return Futures.successfulAsList(futures);
}
 
Example 13
Source File: WaitForCompletionTaskRunner.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public <R> ListenableFuture<List<R>> submitTasks(List<ListenableFuture<R>> futures) {
  return Futures.successfulAsList(futures);
}
 
Example 14
Source File: WaitForCompletionTaskRunner.java    From phoenix with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <R> ListenableFuture<List<R>> submitTasks(List<ListenableFuture<R>> futures) {
  return Futures.successfulAsList(futures);
}
 
Example 15
Source File: ResourcePool.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void close() {
  Preconditions.checkState(!closing.get());
  closing.set(true);

  // Unblock all waiting requests.
  for (SettableFuture<Unit> request : resourceRequests) {
    request.set(null);
  }
  resourceRequests.clear();

  // Any processing that is currently taking place will be allowed to complete (as it won't notice
  // `closing` is true.
  // Any scheduled (but not executing) resource requests should notice `closing` is true and
  // mark themselves as cancelled.
  // Therefore `closeFuture` should allow us to wait for any resources that are in use.
  ListenableFuture<List<Object>> closeFuture = Futures.successfulAsList(pendingWork);

  // As silly as it seems this is the only reliable way to make sure we run the shutdown code.
  // Reusing an external executor means we run the risk of it being shut down before the cleanup
  // future is ready to run (which causes it to never run).
  // Using a direct executor means we run the chance of executing shutdown synchronously (which
  // we try to avoid).
  ExecutorService executorService = MostExecutors.newSingleThreadExecutor("resource shutdown");

  // It is possible that more requests for work are scheduled at this point, however they should
  // all early-out due to `closing` being set to true, so we don't really care about those.
  shutdownFuture =
      Futures.transformAsync(
          closeFuture,
          input -> {
            synchronized (ResourcePool.this) {
              if (parkedResources.size() != createdResources.size()) {
                LOG.error("Whoops! Some resource are still in use during shutdown.");
              }
              // Now that pending work is done we can close all resources.
              for (R resource : createdResources) {
                resource.close();
              }
              if (!resourceRequests.isEmpty()) {
                LOG.error(
                    "Error shutting down ResourcePool: "
                        + "there should be no enqueued resource requests.");
              }
            }
            executorService.shutdown();
            return Futures.immediateFuture(null);
          },
          executorService);
}