异步回调
2019-01-02 本文已影响0人
杜子龙
调用方在调用本接口时,会在接口地址后跟回调地址,然后提供方立刻返回一个结果,并启动一个线程执行业务逻辑,执行完后请求回调地址,实现异步回调,这里只给出提供方demo:
@ApiOperation(value = "自动同步权限")
@RequestMapping(value = "/auto_synchronize_permission", method = RequestMethod.POST)
@ResponseBody
public TestResult autoSynchronize(HttpServletRequest httpServletRequest){
String callback = httpServletRequest.getParameter("callback");//回调url
new Thread(){
public void run(){
TestResult testResult = testPermissionCategoryService.autoSynchronize();//具体实现
JSONObject request =new JSONObject();
request.put("result", testResult);
try {
String res = HttpClientUtil.doPostJson(callback, request.toJSONString(), null);
log.info(res);
} catch (Exception e) {
log.info(e.getMessage(), e);
}
}
}.start();
return new TestResult(BaseMessage.SUCCESS);
}