当执行程序服务需要对象时进行异步响应写入?

问题描述

客户端请求由调用第三方 API 的 doGET 处理。然后,此第三方 API 不会将响应发送回 doGET,而是调用我的其他端点以由 doPOST 处理。我现在需要将来自 doPOST 的响应写入 doGET。

所需的工作流程如下-

  1. 第三方API会回调doPOST
  2. doPOST 应该使用执行器服务使用的 Callable Task
  3. 然后我将从执行程序服务返回 Future 提交调用,如上所示
  4. doGET API 将在此未来执行阻塞 .get() 操作
  5. 一旦收到来自 doPOST回复doGET 会将相同的回复返回给客户端

为了实现这一点,我正在尝试如下设置执行器和未来机制 -

public HttpServletResponse doGET(Request jettyReq,HttpServletRequest request,HttpServletResponse response) throws ExecutionException {
        ExecutorService executor = Executors.newFixedThreadPool(10);

        // Placeholder for the value which will arrive in the future
        Future<WriteDoGetResponse> future = executor.submit(new WriteDoGetResponse());

        // perform some unrelated actions here

        // check future status that gives me an actual response object
        HttpServletResponse modifiedResponse = future.get();

        // if hashmap has future for request-id
        executor.shutdown();
        return modifiedResponse;
    }

public WriteDoGetResponse doPOST(Request jettyReq,HttpServletResponse response) {

}

我现在在另一个班级中有可调用的任务,但这不是我想要解决的问题。

static class WritedoGetResponse implements Callable<HttpServletResponse> {
    @Override
    public HttpServletResponse call() throws Exception {
        return null;
    }

但是我需要关于如何使我的 doPOST api 可调用的帮助?我需要一个机制来解决这个问题。因为 executor.submit() 需要一个实例,我不能使 doPOST 实现可调用。有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)