日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot中使用@Async失效

發(fā)布時間:2024/10/5 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot中使用@Async失效 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

場景:在service中用戶新增的方法中調(diào)用接口上傳數(shù)據(jù)到其他庫的接口,在新增方法上使用了@Transactional,在上傳數(shù)據(jù)方法上使用了@Async,然后發(fā)現(xiàn)@Async注解不生效。

解決方案:不要直接在service用戶新增中調(diào)用上傳接口,改為在controller中即可。

代碼如下:

service層

@Transactional public User userEdit(String name,String mobile,String pwd,String ip,String type,Integer companyId,Integer roleId){User user = new User();user.setLoginKey(mobile);user.setLoginPwd(pwd);if(StringUtils.isNotBlank(name)){user.setName(name);}else {user.setName(mobile);}user.setUserState(Enums.UserState.Normal);user.setUnlockTime(null);user.setErrorLoginCount(0);user.setLastLoginIp(ip);user.setLastLoginTime(new Date());if ("Driver".equalsIgnoreCase(type)){user.setType(Enums.UserType.Driver);dao.save(user);Driver driver = new Driver();driver.setUserId(user.getId());driver.setCompanyId(companyId);driverDao.save(driver);}else if ("Passenger".equalsIgnoreCase(type)){user.setType(Enums.UserType.Passenger);dao.save(user);Passenger passenger = new Passenger();passenger.setUserId(user.getId());passenger.setPhone(mobile);passengerDao.save(passenger);}else if ("Admin".equalsIgnoreCase(type)){user.setType(Enums.UserType.Admin);user.setRoles(roleId);dao.save(user);}return user; }/*** 上報(bào)信息*/ @Async public void mocpassengerUpload(String mobile){boolean update = true;MocPassengerInfo mocPassengerInfo = passengerInfoDao.getRepo().findByPhone(mobile);if (mocPassengerInfo == null){mocPassengerInfo = new MocPassengerInfo();update = false;}mocPassengerInfo.setPhone(mobile);mocPassengerInfo.setRegister(new Date());mocPassengerInfo.setStatus(mocService.upload("*",mocPassengerInfo, update ? MocInfo.Action.Update : MocInfo.Action.Add));passengerInfoDao.save(mocPassengerInfo);}

controller層

@PostMapping("/api/user/register") public Result register(@Pattern(regexp = Const.REGEXP_MOBILE, message = "手機(jī)號[mobile]格式不正確") String mobile,@NotBlank(message = "密碼[passwd]不能為空值") String passwd,@NotBlank(message = "用戶類型[type]不能為空值") String type,String name,Integer companyId,Integer roleId,HttpServletRequest request){Map<String, Object> data = new HashMap<>();try {User users = userService.findUser(mobile,type);if (users!=null){return Result.fail(206, "用戶已存在!");}if (CommonKit.passwdMD5(passwd).equalsIgnoreCase(CommonKit.passwd(mobile.substring(5)))) {return Result.fail(206, "密碼過于簡單,請更換密碼");}if(companyId==null){companyId = Integer.valueOf(company);}User user = userService.userEdit(name,mobile, CommonKit.passwdMD5(passwd), IpKit.getIpAddr(request), type, companyId,roleId);data.put("uid", user.getId());data.put("token", tokenService.generate(user.getId(), 5*24*60));data.put("name", user.getName());data.put("mobile", user.getLoginKey());data.put("avatar", user.getPicUrl()!=null?rootUrl + user.getPicUrl():"");if(type.equals("Driver")){data.put("cname", companyInfoService.getDao().findSnapshoot(companyId).getName());}if(type.equals("Passenger")){userService.mocpassengerUpload(mobile);}}catch (Exception e){e.printStackTrace();return Result.error(901,"注冊失敗!");}return Result.ok(data); }

使用@Async需要配置,配置如下:

package net.web.config;import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;@Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer {@Overridepublic Executor getAsyncExecutor() {//線程池設(shè)置ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();taskExecutor.setCorePoolSize( 10 );//核心線程taskExecutor.setMaxPoolSize( 100 );//最大線程taskExecutor.setQueueCapacity( 40 );//隊(duì)列大小taskExecutor.setThreadNamePrefix( "async-service-" );taskExecutor.initialize();return taskExecutor;}@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {//直接返回 祖籍 AsyncUncaughtExceptionHandler 對象return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();} }

另外還需要在啟動類上添加注解:@EnableAsync

?

總結(jié)

以上是生活随笔為你收集整理的spring boot中使用@Async失效的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。