本文介紹了如何發送異步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
}
呼叫流:-
-
客戶端將調用ConnectAPI,后者將調用第三方API。
處理上述調用后的第三方接口將回調postQuestionnaire
ConnectAPI只有在收到postQuestionnaire的回調時才會發送響應。
推薦答案
理想情況下,這應該是您的流
-
客戶端將調用ConnectAPI,后者將調用第三方API。
處理上述調用后的第三方接口將回調
郵報問答。->;這應該使用
Callable
使用Executor服務的任務分離線程來完成。然后,您將從Executor服務返回Future<ResponseFromPOstQuestionnaire>
調用submit
ConnectAPI僅在以下情況下發送響應
收到了postQuestionnaire的回調。-&>一旦您有了
Future
對象,您可以通過調用.get()
(這是阻塞調用)來進行等待,這樣它就會等待響應到來,然后您可以將相同的響應或修改后的響應返回給客戶端。
此處介紹了如何將可調用任務與Executor服務一起使用示例-
這篇關于如何發送異步Http響應?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,