一般写在Controller里面
@ResponseBody
@RequestMapping("/logOutCaigou")
public boolean logOutCaigou(ModelAndView mav, @Param("memberId")Long memberId, @Param("openId")String openId) {
saveLoginLog(openId, memberId, LoginLog.LOGOUT); //存储登出日志
return true;
}
//开启异步记日志
启动异步线程。、,后台执行任务。任务开启后,就return直接返回
不需要等待任务执行完成
private void saveLoginLog(final String openId, final long memberId, int type) {
ExecutorService exec = Executors.newSingleThreadExecutor(); //开启异步单例线程
exec.execute(new Runnable() {
public void run() {
iLoginLogService.saveLoginLog(openId, memberId, LoginLog.LOGIN); //存储登录日志
}
});
return;
}
Service里面
@Override
@TradingDataSource(name = DataSources.WRITE_WECHAT_DATASOURCE)
public boolean saveLoginLog(String openId, long memberId, int type) throws ServiceException {
LoginLog loginLog = new LoginLog();
loginLog.setId(sequenceUtil.getSeqNextVal("seq_login_log"));
loginLog.setOpenId(openId);
loginLog.setMemberId(memberId);
loginLog.setState(type);
String key = CaigouConstant.CACHEKEY_ADDRESS_PREFIX + openId;
cacheManagerDao.set(key, "北京北京"); //保存详细地址
loginLog.setDeviceMessage(cacheManagerDao.get(key).toString());
iLoginLogDao.saveLoginLog(loginLog);
return true;
}