当前位置: 首页>>代码示例>>Java>>正文


Java Futures.successfulAsList方法代码示例

本文整理汇总了Java中com.google.common.util.concurrent.Futures.successfulAsList方法的典型用法代码示例。如果您正苦于以下问题:Java Futures.successfulAsList方法的具体用法?Java Futures.successfulAsList怎么用?Java Futures.successfulAsList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.util.concurrent.Futures的用法示例。


在下文中一共展示了Futures.successfulAsList方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getDeviceAttributesShadow

import com.google.common.util.concurrent.Futures; //导入方法依赖的package包/类
@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;
}
 
开发者ID:osswangxining,项目名称:iothub,代码行数:27,代码来源:DeviceApiController.java

示例2: unitePessimistically

import com.google.common.util.concurrent.Futures; //导入方法依赖的package包/类
/**
 * Requests the hub to return a single united, pessimistic {@link RedFuture} instance.
 * Pessimistic means that the returned future expects all the hub's tracked futures to complete at some point,
 * namely, it will be successfully resolved once all the hub's tracked futures are completed.
 * It will not be failed in any case.
 *
 * @return the united future.
 */
public RedFuture unitePessimistically() {
    RedFuture validated = validate();
    if (validated != null) {
        return validated;
    }
    ListenableFuture<List<Object>> collection = Futures.successfulAsList(listenableFutures);
    OpenRedFuture future = RedFuture.future();
    future.follow(collection);
    return future;
}
 
开发者ID:avivcarmis,项目名称:java-red,代码行数:19,代码来源:RedFutureHub.java

示例3: getPivotHeaderByNumber

import com.google.common.util.concurrent.Futures; //导入方法依赖的package包/类
/**
 * 1. Get pivotBlockNumber blocks from all peers
 * 2. Ensure that pivot block available from 50% + 1 peer
 * 3. Otherwise proposes new pivotBlockNumber (stepped back)
 * @param pivotBlockNumber      Pivot block number
 * @return     null - if no peers available
 *             null, newPivotBlockNumber - if it's better to try other pivot block number
 *             BlockHeader, null - if pivot successfully fetched and verified by majority of peers
 */
private Pair<BlockHeader, Long> getPivotHeaderByNumber(long pivotBlockNumber) throws Exception {
    List<Channel> allIdle = pool.getAllIdle();
    if (!allIdle.isEmpty()) {
        try {
            List<ListenableFuture<List<BlockHeader>>> result = new ArrayList<>();

            for (Channel channel : allIdle) {
                ListenableFuture<List<BlockHeader>> future =
                        channel.getEthHandler().sendGetBlockHeaders(pivotBlockNumber, 1, false);
                result.add(future);
            }
            ListenableFuture<List<List<BlockHeader>>> successfulRequests = Futures.successfulAsList(result);
            List<List<BlockHeader>> results = successfulRequests.get(3, TimeUnit.SECONDS);

            Map<BlockHeader, Integer> pivotMap = new HashMap<>();
            for (List<BlockHeader> blockHeaders : results) {
                if (!blockHeaders.isEmpty()) {
                    BlockHeader currentHeader = blockHeaders.get(0);
                    if (pivotMap.containsKey(currentHeader)) {
                        pivotMap.put(currentHeader, pivotMap.get(currentHeader) + 1);
                    } else {
                        pivotMap.put(currentHeader, 1);
                    }
                }
            }

            int peerCount = allIdle.size();
            for (Map.Entry<BlockHeader, Integer> pivotEntry : pivotMap.entrySet()) {
                // Require 50% + 1 peer to trust pivot
                if (pivotEntry.getValue() * 2 > peerCount) {
                    logger.info("Pivot header fetched: " + pivotEntry.getKey().getShortDescr());
                    return Pair.of(pivotEntry.getKey(), null);
                }
            }

            Long newPivotBlockNumber = Math.max(0, pivotBlockNumber - 1000);
            logger.info("Current pivot candidate not verified by majority of peers, " +
                    "stepping back to block #{}", newPivotBlockNumber);
            return Pair.of(null, newPivotBlockNumber);
        } catch (TimeoutException e) {
            logger.debug("Timeout waiting for answer", e);
        }
    }

    return null;
}
 
开发者ID:talentchain,项目名称:talchain,代码行数:56,代码来源:FastSyncManager.java

示例4: getDeviceAttributes

import com.google.common.util.concurrent.Futures; //导入方法依赖的package包/类
@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;
}
 
开发者ID:osswangxining,项目名称:iothub,代码行数:55,代码来源:DeviceApiController.java


注:本文中的com.google.common.util.concurrent.Futures.successfulAsList方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。