Java Springboot Async使用
Posted シ゛甜虾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Springboot Async使用相关的知识,希望对你有一定的参考价值。
异步函数调用不会阻塞主线程,主线程自动继续往下执行,只需要在函数上打上标记@Async
@Service
public class AsyncSetting
Logger logger = LoggerFactory.getLogger(AsyncSetting.class);
@Async
@Transactional
public void SettingCommandModeStatusSelect(LogService logService, EquipmentService equipmentService, EntityManager entityManager, SettingStatus settingStatus)
logger.info("settingStatus CommandId="+settingStatus.getCommandId()+" 执行查询任务开始!");
try
while(true)
UserHttpClient userHttpClient = new UserHttpClient();
String json = userHttpClient.QueryDeviceCommand(settingStatus.getDeviceId(), startTime,endTime);
JsonToSettingCommand jsonToSettingCommand = new JsonToSettingCommand();
EmnuSettingStatus emnuSettingStatus = jsonToSettingCommand.dataConvert(json,settingStatus);
//成功,超时,过期,取消,失败,发生错误
if((emnuSettingStatus == EmnuSettingStatus.DELIVERED) || (emnuSettingStatus == EmnuSettingStatus.SUCCESSFUL) || (emnuSettingStatus == EmnuSettingStatus.TIMEOUT)|| (emnuSettingStatus == EmnuSettingStatus.EXPIRED)||
(emnuSettingStatus == EmnuSettingStatus.FAILED)|| (emnuSettingStatus == EmnuSettingStatus.CANCELED)|| (emnuSettingStatus == EmnuSettingStatus.ERROR))
try
UserDefinedServer userDefinedServer = new UserDefinedServer();
Long elapsedTime = settingStatus.getElapsedTime() - (settingStatus.getCorrectionTime()/1000);
if(userDefinedServer.updateSettingModeByTableNameBySettingId(entityManager, "tb_setting_mode", settingStatus.getCommandId(), emnuSettingStatus.getCode(),elapsedTime))
logger.info("settingStatus CommandId=" + settingStatus.getCommandId() + " " + emnuSettingStatus.getMsg() + " 写入数据库成功!");
logService.addLog(Str.DebugLevel.Info, "AsyncSetting", "settingStatus CommandId=" + settingStatus.getCommandId() + " " + emnuSettingStatus.getMsg() + " 写入数据库成功!");
else
logger.info("settingStatus CommandId=" + settingStatus.getCommandId() + " " + emnuSettingStatus.getMsg() + " 写入数据库失败!");
logService.addLog(Str.DebugLevel.Info, "AsyncSetting", "settingStatus CommandId=" + settingStatus.getCommandId() + " " + emnuSettingStatus.getMsg() + " 写入数据库失败!");
break;
catch (Exception ex)
logger.error("settingStatus CommandId="+settingStatus.getCommandId()+" "+emnuSettingStatus.getMsg()+" 写入数据库失败!");
logService.addLog(Str.DebugLevel.Error,"AsyncSetting","settingStatus CommandId="+settingStatus.getCommandId()+" "+emnuSettingStatus.getMsg()+" 写入数据库失败!");
break;
else
logger.info("settingStatus CommandId="+settingStatus.getCommandId()+" json="+json);
Thread.sleep(60*1000);//1分钟查询一次命令执行状态
catch (Exception ex)
logger.error("settingStatus CommandId="+settingStatus.getCommandId()+" 执行查询任务失败!"+ex.toString());
logService.addLog(Str.DebugLevel.Error,"AsyncSetting","settingStatus CommandId="+settingStatus.getCommandId()+" 执行查询任务失败!");
logger.info("settingStatus CommandId="+settingStatus.getCommandId()+" 执行查询任务结束!");
前端调用
//注入
@Autowired
private AsyncSetting asyncSetting;
@Transactional
@ResponseBody
@RequestMapping("SendMode")
public ResReturnInfo SendMode(String device_id,String workmode,String reserved0,String reserved1,String now,String retry,HttpSession session)
asyncSetting.SettingCommandModeStatusSelect(logService,equipmentService,entityManager,settingStatus);//无返回参数,有返回参数还没用过
以上是关于Java Springboot Async使用的主要内容,如果未能解决你的问题,请参考以下文章