微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

客户端请求由调用第三方 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 举报,一经查实,本站将立刻删除。