问题描述
我们正在研究这样的解决方案;
请求:(我们通过API调用接收请求,并通过我们使用的库发送给第三方) 我们的客户->我们的API->第三方
Response :(我们通过正在使用的库中提供的回调方法从第三方异步收到此响应) 第三方->我们的代码->我们的客户
这是下面的代码,想要摆脱Thread.sleep()调用并利用回调提供响应。
----- API Method -------------
@GetMapping
public ResponseEntity<String> getData(@RequestBody String requestId) throws SessionNotFound,InterruptedException {
dataService.get(requestId);
String msg;
long start = System.currentTimeMillis();
do {
// We want to get rid of this sleep() statement and some way to callback here as soon there is message.
Thread.sleep(30);
msg = clientApp.getRespnse(requestId);
} while(msg == null);
return ResponseEntity.ok(msg);
}
------- Service Class and Methods ---------------
@Service
public class DataService {
@Autowired
private ClientApp clientApp;
public void get(String requestId) throws SessionNotFound {
// This method is from the library we use. This only submits the request,response is received on different method.
send(requestId);
}
------- Component Class and Methods ---------------
@Component
public class ClientFixApp {
private Map<String,String> responseMap = new HashMap<>();
// This method is callback from the third party library,whenever there is response this method will get invoked and this message we need to send as response of the API call.
@Override
public void onResponse(String requestId)
throws FieldNotFound,IncorrectDataFormat,IncorrectTagValue,UnsupportedMessageType {
responseMap.put(msgId,jsonMsg);
}
public String getRespnse(String requestId) {
return responseMap.get(requestId);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)