<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、概述 耗時的任務,不宜同步執行;我們可以用多線程來實現,但Spring提供了更優雅的機制; ## 二、@Async實現 ### **基本用法** 1、在啟動類中加上@EnableAsync注解來開啟異步任務,或者新建一個專用的異步線程池配置類; 2、新建一個異步任務類; 3、在業務Service(其實任何地方都可以,但建議在這里調用)中,注入該任務類,并執行; 實例: ``` @Configuration @EnableAsync public class RayAsyncExecutorConfig { private int corePoolSize = 8; private int maxPoolSize = 16; private int queueCapacity = 200; private String threadNamePrefix = "AsyncExecutor-"; @Bean("rayExecutor") public Executor rayExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix(threadNamePrefix); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; } } ``` ``` @Component public class RoleNameRefreshTask { @Async("rayExecutor") public Future<String> test() throws InterruptedException { return new AsyncResult<>(DateUtil.getDateStr(100)); } } ``` 調用代碼: ![](https://img.kancloud.cn/1f/8b/1f8b6ef51a8b647b5c5a4ea6c79b7812_937x486.png) ### **事務控制** 如果需要在異步任務中啟用事務,那么,需要單獨新建業務操作類,在該類的公開方法中加入@Transactional注解,否則,無法控制事務;也就是主方法用@Async標注,然后里面調用另外類的事務方法,事務方法用@Transactional標注,即可; 如果方法中同時加這兩個注解,則事務不會生效; ``` @Async public void importAction(RayModel model, SysAsynOrder sysAsynOrder) throws Exception { Workbook wb = Base64Util.getWorkbookByBase64(Base64Util.trimHeadFromFront(sysAsynOrder.getImportedFile())); Sheet sheet = wb.getSheetAt(0); candidateCardProcessor.doLogic(sheet, sysAsynOrder); } ``` ``` @Component public class CandidateCardExecutor extends BaseAsynActionTransactionalExecutor { @Autowired private CardInfoCandidateDao cardInfoCandidateDao; @Transactional public void doLogic(Sheet sheet, SysAsynOrder sysAsynOrder) throws Exception { for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); CardInfoCandidate candidate = new CardInfoCandidate(); if (!StringUtil.isEmpty(ExcelUtil.getCellValue(row.getCell(0)).toString())) { candidate.setUserCode(ExcelUtil.getCellStringValue(row.getCell(0), true)); candidate.setIccidCode(ExcelUtil.getCellStringValue(row.getCell(1), true)); candidate.setAccessCode(ExcelUtil.getCellStringValue(row.getCell(2), true)); candidate.setCardSource(ExcelUtil.getCellStringValue(row.getCell(3), true)); candidate.setControlType(ExcelUtil.getCellStringValue(row.getCell(4), true)); candidate.setFunctionType(ExcelUtil.getCellStringValue(row.getCell(5), true)); candidate.setGroupTag(ExcelUtil.getCellStringValue(row.getCell(6), true)); candidate.setCardBoardPrice(StringUtil.isEmpty(ExcelUtil.getCellStringValue(row.getCell(7), true)) ? 0 : NumberUtil.getLongValueByNumberObject(ExcelUtil.getCellStringValue(row.getCell(7), true))); candidate.setSilentDays(StringUtil.isEmpty(ExcelUtil.getCellStringValue(row.getCell(8), true)) ? 0 : NumberUtil.getLongValueByNumberObject(ExcelUtil.getCellStringValue(row.getCell(8), true))); candidate.setTestFlowQuota(StringUtil.isEmpty(ExcelUtil.getCellStringValue(row.getCell(9), true)) ? 0 : NumberUtil.getDoubleValueByObject(ExcelUtil.getCellStringValue(row.getCell(9), true))); cardInfoCandidateDao.save(candidate); } } asynOrderExecutor.completeImportOrder(sysAsynOrder, "完成", AsynOrderStatus.EXECUTED_SUCCESS); } } ``` ## 三、注意 與@Transactional事務類似,異步也是基于動態代理技術實現; >[danger] > 1、異步方法不能使用static修飾; > 2、注解的方法必須是`public`方法; > 3、異步類必須使用@Component注解; > 4、調用異步方法的類與異步方法本身的類不能在同一個類中; > 5、返回值只能為void或者Future;
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看