Esempio n. 1
0
  public DeferredResult newDeferredResult(final Long userId) {
    final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
    deferredResult.onCompletion(
        new Runnable() {
          @Override
          public void run() {
            Queue<DeferredResult<Object>> queue = userIdToDeferredResultMap.get(userId);
            if (queue != null) {
              queue.remove(deferredResult);
              deferredResult.setResult("");
            }
          }
        });
    deferredResult.onTimeout(
        new Runnable() {
          @Override
          public void run() {
            deferredResult.setErrorResult("");
          }
        });
    Queue<DeferredResult<Object>> queue = userIdToDeferredResultMap.get(userId);
    if (queue == null) {
      queue = new LinkedBlockingDeque<DeferredResult<Object>>();
      userIdToDeferredResultMap.put(userId, queue);
    }
    queue.add(deferredResult);

    return deferredResult;
  }
Esempio n. 2
0
  public void offline(final Long userId) {

    Queue<DeferredResult<Object>> queue = userIdToDeferredResultMap.remove(userId);
    if (queue != null) {
      for (DeferredResult<Object> result : queue) {
        try {
          result.setResult("");
        } catch (Exception e) {
          // ignore
        }
      }
    }
  }
Esempio n. 3
0
  @RequestMapping(Routes.USER_AUTH)
  public DeferredResult<GitkitUser> authenticateGitkitToken(@RequestBody final String token) {
    DeferredResult<GitkitUser> dr = new DeferredResult<>();
    GitkitClientService gcs = new GitkitClientService();

    try {
      GitkitUser gu = gcs.getClient().validateToken(token);
      dr.setResult(gu);
    } catch (GitkitClientException gkce) {
      dr.setErrorResult("No good.");
    }

    return dr;
  }
Esempio n. 4
0
 public void push(final Long userId, final Object data) {
   Queue<DeferredResult<Object>> queue = userIdToDeferredResultMap.get(userId);
   if (queue == null) {
     return;
   }
   for (DeferredResult<Object> deferredResult : queue) {
     if (!deferredResult.isSetOrExpired()) {
       try {
         deferredResult.setResult(data);
       } catch (Exception e) {
         queue.remove(deferredResult);
       }
     }
   }
 }
Esempio n. 5
0
  private void commandPong(DeferredResult<Result> defResult, String usernameFromToken) {
    UsersService usersServ = context.getBean(UsersService.class);

    usersServ.updateCallsForUser(usernameFromToken);
    Users user = usersServ.getUserByName(usernameFromToken).get(0);
    defResult.setResult(new Result("pong", user.getNumberOfCalls()));
  }
Esempio n. 6
0
  /** 定期清空队列 防止中间推送消息时中断造成消息丢失 */
  @Scheduled(fixedRate = 5L * 60 * 1000)
  public void sync() {
    Map<Long, Queue<DeferredResult<Object>>> oldMap = userIdToDeferredResultMap;
    userIdToDeferredResultMap = new ConcurrentHashMap<Long, Queue<DeferredResult<Object>>>();
    for (Queue<DeferredResult<Object>> queue : oldMap.values()) {
      if (queue == null) {
        continue;
      }

      for (DeferredResult<Object> deferredResult : queue) {
        try {
          deferredResult.setResult("");
        } catch (Exception e) {
          queue.remove(deferredResult);
        }
      }
    }
  }
  @Override
  public void run() {
    long requests = concurrentRequests.getAndDecrement();

    if (deferredResult.isSetOrExpired()) {
      logger.warn(
          "{}: Processing of non-blocking request #{} already expired", requests, requestId);
    } else {
      logger.info("Retrieving result");
      NetworkServiceInstantiateReply reply = ServiceSelectionMsgTable.getInstance().get(requestId);
      if (reply != null) {
        logger.info("Retrieving reply from orchestrator with ns id {}", requestId);
      }

      boolean deferredStatus = deferredResult.setResult(reply);
      logger.debug(
          "{}: Processing of non-blocking request #{} done, deferredStatus = {}",
          requests,
          requestId,
          deferredStatus);
    }
  }