进来接手一个任务,需要做异步多线程数据源调用,数据源的配置和使用请阅读相关文章:
https://www.cnblogs.com/haoliyou/p/9604452.html
配置好数据源后,实质上其实就是需要做一个异步的调用查询
@Resource private LoginTask loginTask; @Override public JSONObject getLoginResult(String username, String password) { JSONObject jsonObject = new JSONObject(); jsonObject.put("ret", "-1"); jsonObject.put("msg", "用户名或密码错误"); Future<JSONObject> cmFutureTask = loginTask.handleCmLogin(jsonObject, username, password); Future<JSONObject> csFutureTask = loginTask.handleCsLogin(jsonObject, username, password); JSONObject taskJson = new JSONObject(); int count = 0; while (count < LoginConstants.TASK_REQUEST_LIMIT) { try { if (cmFutureTask.isDone()) { taskJson = cmFutureTask.get(LoginConstants.TASK_RESULT_LIMIT, TimeUnit.SECONDS); if (StringUtils.equals(taskJson.getString("ret"), "0")) { csFutureTask.cancel(true); jsonObject = taskJson; break; } } if (cbsFutureTask.isDone()) { taskJson = csFutureTask.get(LoginConstants.TASK_RESULT_LIMIT, TimeUnit.SECONDS); if (StringUtils.equals(taskJson.getString("ret"), "0")) { cmFutureTask.cancel(true); jsonObject = taskJson; break; } } if (cmFutureTask.isDone() && csFutureTask.isDone()) { if (checkAdUser(username, password)) { jsonObject.put("ret", "0"); jsonObject.put("msg", "登录成功!"); break; } } count++; Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } return jsonObject; }
@Component public class LoginTask { private static final Logger log = LoggerFactory.getLogger(LoginTask.class); @Resource private UserInfoMapper userInfoMapper; @Resource private UserMapper userMapper; /** * 处理发起的请求 * * @param request * @param requestHeaders * @param result * @param jsonObject * @param loginInfo * @return */ @Async("taskExecutor") public Future<JSONObject> handleCmLogin(JSONObject jsonObject, String username, String password) { UserInfo userInfo = userInfoMapper.selectByUserId(username); if (userInfo == null) { jsonObject.put("ret", "20003"); jsonObject.put("msg", "抱歉,目前仅支持xxxxx账号登录"); return new AsyncResult<JSONObject>(jsonObject); } else if (!password.equals(userInfo.getPassword())) { log.info(userInfo.getUserName()); jsonObject.put("ret", "20002"); jsonObject.put("msg", "登录失败,请确认用户名和密码是否正确"); return new AsyncResult<JSONObject>(jsonObject);
} jsonObject.put("data", userInfo); jsonObject.put("ret", "0"); jsonObject.put("msg", "success"); log.info(userInfo.toString()); return new AsyncResult<JSONObject>(jsonObject); } /** * 处理的请求 * * @param result * @param requestHeaders * @param jsonObject * @param loginInfo * @return */ @Async("taskExecutor") public Future<JSONObject> handleCsLogin(JSONObject jsonObject, String username, String password) { User user = userMapper.selectByUserId(username, password); if (user == null) { jsonObject.put("ret", "20002"); jsonObject.put("msg", "登录失败,请确认用户名和密码是否正确"); return new AsyncResult<JSONObject>(jsonObject); } log.info(user.toString()); jsonObject.put("data", user); jsonObject.put("ret", "0"); jsonObject.put("msg", "success"); return new AsyncResult<JSONObject>(jsonObject); }
}