Reputation: 355
I have created a Rest API that takes a request and gives response to the client. But My API is dependent on a third party service, that instead of giving a response, give a callback to my other endpoint. In order to send the service I need to wait for the callback to be received. How can I achieve it?
My Rest API that needs to send the response.
@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();
}
Instead of sending a simple response object I want to wait for the callback to hit at the following 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
}
Call flow : -
Upvotes: 1
Views: 679
Reputation: 858
Ideally this should be your flow
Callable
Task using executor service. Then you will have Future<ResponseFromPOstQuestionnaire>
returned from executor service submit
callFuture
object you can do wait on by calling .get()
(this is blocking call) so it will wait for response to come then you can return the same response or modified respone back to client.Example on how to use callable task with executor service is explain here -> https://www.journaldev.com/1090/java-callable-future-example
Upvotes: 1