如何发送异步Http响应?

How to send asynchronous Http Response?(如何发送异步Http响应?)

本文介绍了如何发送异步Http响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个rest API,它接受请求并向客户端提供响应。但我的API依赖于第三方服务,它不是给出响应,而是回调我的另一个端点。为了发送服务,我需要等待收到回调。我如何才能做到这一点?

我需要发送响应的REST API。

@POST
//  @Produces(MediaType.TEXT_XML)
//  @Consumes(MediaType.TEXT_XML)
    public ConnectResponse connectAPI(String connectString, @Context HttpHeaders headers) {
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        for (Entry<String, List<String>> entry : headers.getRequestHeaders().entrySet()) {
            logger.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            for (String eachEntry : entry.getValue()) {
                logger.info("eachEntry " + eachEntry);
            }
        }
        logger.info("USSD received " + connectString);
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        ConnectRequest requestObj = new ConnectRequest();
        try {
            if (connectString != null && connectString.startsWith("<")) {
                requestObj = marshallConnectRequest(connectString);
            } else {
                requestObj = convertKeyValueToObject(connectString);
            }

            logger.info("Request is " + requestObj);
        } catch (JAXBException e) {
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error(e.getMessage(), e);
        }
        ConnectResponse connectResponse = new ConnectResponse();
        connectResponse.setSession(requestObj.getSessionid());
        connectResponse.setText("Hello");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        return connectResponse;
    }

    public ConnectRequest marshallConnectRequest(String connectString) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(ConnectRequest.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (ConnectRequest) jaxbUnmarshaller.unmarshal(new StringReader(connectString));
    }

    public ConnectRequest convertKeyValueToObject(String connectString) {
        return new ConnectRequest();
    }

我希望等待回调命中以下API,而不是发送简单的响应对象。

@Path("/rest")
public class RESTWebservice {
    /*
     * @Context private MessageContext messageContext;
     */
    final Logger logger = Logger.getLogger(RESTWebservice.class);

    @POST
    @Path("/sendResponse")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public ResponseJSON postQuestionnaire(RequestJSON requestJson) {
    // performing operations
}

呼叫流:-

  1. 客户端将调用ConnectAPI,后者将调用第三方API。
  2. 处理上述调用后的第三方接口将回调postQuestionnaire
  3. ConnectAPI只有在收到postQuestionnaire的回调时才会发送响应。

推荐答案

理想情况下,这应该是您的流

  1. 客户端将调用ConnectAPI,后者将调用第三方API。
  2. 处理上述调用后的第三方接口将回调 邮报问答。->;这应该使用Callable使用Executor服务的任务分离线程来完成。然后,您将从Executor服务返回Future<ResponseFromPOstQuestionnaire>调用submit
  3. ConnectAPI仅在以下情况下发送响应 收到了postQuestionnaire的回调。-&>一旦您有了Future对象,您可以通过调用.get()(这是阻塞调用)来进行等待,这样它就会等待响应到来,然后您可以将相同的响应或修改后的响应返回给客户端。
此处介绍了如何将可调用任务与Executor服务一起使用示例-

这篇关于如何发送异步Http响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何发送异步Http响应?

基础教程推荐