定义超时异常

@HystrixCommand
  @RequestMapping(value = "/findByNumber", method = RequestMethod.GET)
  public ObjectRestResponse findByNumber(@RequestParam(required = true) String number) {
    final ExecutorService exec = Executors.newFixedThreadPool(1);
    ObjectRestResponse result = new ObjectRestResponse<>();
    Callable> call = new Callable>() {
      ObjectRestResponse result = new ObjectRestResponse<>();

      @Override
      public ObjectRestResponse call() throws Exception {
        result = basePersonFeignBiz.findByNumber(number);
        return result;
      }
    };
    // 超时抛出
    try {
      Future> future = exec.submit(call);
      result = future.get(1000 * 3, TimeUnit.MILLISECONDS); // 任务处理超时时间设为
      // 1 秒
      System.out.println("任务成功返回:" + result);
    } catch (Exception e) {
      throw new RuntimeException("invoke timeout", e);
    }
    // 关闭线程池
    exec.shutdown();
    return result;
  }

你可能感兴趣的:(定义超时异常)