最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot @Async 異步任務(wù)執(zhí)行方法

 更新時間:2018年05月10日 10:54:12   作者:不要亂摸  
本篇文章主要介紹了Spring Boot @Async 異步任務(wù)執(zhí)行方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1、任務(wù)執(zhí)行和調(diào)度

Spring用TaskExecutor和TaskScheduler接口提供了異步執(zhí)行和調(diào)度任務(wù)的抽象。

Spring的TaskExecutor和java.util.concurrent.Executor接口時一樣的,這個接口只有一個方法execute(Runnable task)。

1.1、TaskExecutor類型

Spring已經(jīng)內(nèi)置了許多TaskExecutor的實現(xiàn),你沒有必要自己去實現(xiàn):

  1. SimpleAsyncTaskExecutor  這種實現(xiàn)不會重用任何線程,每次調(diào)用都會創(chuàng)建一個新的線程。
  2. SyncTaskExecutor  這種實現(xiàn)不會異步的執(zhí)行
  3. ConcurrentTaskExecutor  這種實現(xiàn)是java.util.concurrent.Executor的一個adapter。
  4. SimpleThreadPoolTaskExecutor  這種實現(xiàn)實際上是Quartz的SimpleThreadPool的一個子類,它監(jiān)聽Spring的聲明周期回調(diào)。
  5. ThreadPoolTaskExecutor  這是最常用最通用的一種實現(xiàn)。它包含了java.util.concurrent.ThreadPoolExecutor的屬性,并且用TaskExecutor進行包裝。

1.2、注解支持調(diào)度和異步執(zhí)行

To enable support for @Scheduled and @Async annotations add @EnableScheduling and @EnableAsync to one of your

@Configuration classes:

@Configuration
@EnableAsync
@EnableScheduling
public class AppConfig {
}

特別注意

The default advice mode for processing @Async annotations is "proxy" which allows for interception of calls through the proxy only; local calls within the same class cannot get intercepted that way. For a more advanced mode of interception, consider switching to "aspectj" mode in combination with compile-time or load-time weaving.

默認是用代理去處理@Async的,因此,相同類中的方法調(diào)用帶@Async的方法是無法異步的,這種情況仍然是同步。

舉個例子:下面這種,在外部直接調(diào)用sayHi()是可以異步執(zhí)行的,而調(diào)用sayHello()時sayHi()仍然是同步執(zhí)行

public class A {
   public void sayHello() {
    sayHi();
  }

  @Async
  public void sayHi() {

  }   
}

1.3、@Async注解

在方法上加@Async注解表示這是一個異步調(diào)用。換句話說,方法的調(diào)用者會立即得到返回,并且實際的方法執(zhí)行是想Spring的TaskExecutor提交了一個任務(wù)。

In other words, the caller will return immediately upon invocation and the actual execution of the method will occur in a task that has been submitted to a Spring TaskExecutor.

@Async
void doSomething() {
  // this will be executed asynchronously
}

@Async
void doSomething(String s) {
  // this will be executed asynchronously
}

@Async
Future<String> returnSomething(int i) {
  // this will be executed asynchronously
}

注意:

@Async methods may not only declare a regular java.util.concurrent.Future return type but also Spring's org.springframework.util.concurrent.ListenableFuture or, as of Spring 4.2, JDK 8's java.util.concurrent.CompletableFuture: for richer interaction with the asynchronous task and for immediate composition with further processing steps.

1.4、@Async限定Executor

默認情況下,當在方法上加@Async注解時,將會使用一個支持注解驅(qū)動的Executor。然而,@Async注解的value值可以指定一個別的Executor

@Async("otherExecutor")
void doSomething(String s) {
  // this will be executed asynchronously by "otherExecutor"
}

這里,otherExecutor是Spring容器中任意Executor bean的名字。

1.5、@Async異常管理

當一個@Async方法有一個Future類型的返回值時,就很容易管理在調(diào)Future的get()方法獲取任務(wù)的執(zhí)行結(jié)果時拋出的異常。如果返回類型是void,那么異常是不會被捕獲到的。

public class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {

  @Override
  public void handleUncaughtException(Throwable ex, Method method, Object... params) {
    // handle exception
  }
}

2、線程池配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@EnableAsync
public class TaskExecutorConfig {
  private Integer corePoolSize = 30;
  private Integer maxPoolSize = 50;
  private Integer keepAliveSeconds = 300;
//  private Integer queueCapacity = 2000;
  @Bean("myThreadPoolTaskExecutor")
  public ThreadPoolTaskExecutor myThreadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(corePoolSize);
    executor.setMaxPoolSize(maxPoolSize);
    executor.setKeepAliveSeconds(keepAliveSeconds);
//    executor.setQueueCapacity(queueCapacity);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;
  }
}

調(diào)用

@Async("myThreadPoolTaskExecutor")
  @Override
  public void present(CouponPresentLogEntity entity) {
    try {
      CouponBaseResponse rst = couponSendRpcService.send(entity.getUserId(), entity.getCouponBatchKey(), "1", entity.getVendorId());
      if (null != rst && rst.isSuccess()) {
        entity.setStatus(PresentStatusEnum.SUCCESS.getType());
      }else {
        String reason = (null == rst) ? "響應異常" : rst.getMsg();
        entity.setFailureReason(reason);
        entity.setStatus(PresentStatusEnum.FAILURE.getType());
      }
    }catch (Exception ex) {
      log.error(ex.getMessage(), ex);
      entity.setFailureReason(ex.getMessage());
      entity.setStatus(PresentStatusEnum.FAILURE.getType());
    }
    couponPresentLogDao.update(entity);
  }

結(jié)果

[INFO ] 2018-05-09 16:27:39.887 [myThreadPoolTaskExecutor-1] [com.ourhours.coupon.rpc.dubbo.ReceiveLogFilter] - receive method-name:send; arguments:[10046031,"4d7cc32f8f7e4b00bca56f6bf4b3b658","1",10001]
[INFO ] 2018-05-09 16:27:39.889 [myThreadPoolTaskExecutor-2] [com.ourhours.coupon.rpc.dubbo.ReceiveLogFilter] - receive method-name:send; arguments:[10046031,"4d7cc32f8f7e4b00bca56f6bf4b3b658","1",10001]

參考:

Spring Framework Reference Documentation 4.3.17.RELEASE

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據(jù)持久化的操作

    SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據(jù)持久化的操作

    這篇文章主要介紹了SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據(jù)持久化,本節(jié)內(nèi)容我們介紹了數(shù)據(jù)持久化的相關(guān)操作,并且是基礎(chǔ)傳統(tǒng)的關(guān)系型數(shù)據(jù)庫——mysql,需要的朋友可以參考下
    2022-10-10
  • Java如何設(shè)置PDF文檔背景色詳解

    Java如何設(shè)置PDF文檔背景色詳解

    這篇文章主要介紹了Java如何設(shè)置PDF文檔背景色詳解,一般生成的PDF文檔默認的文檔底色為白色,我們可以通過一定方法來更改文檔的背景色,以達到文檔美化以及保護雙眼的作用。 以下內(nèi)容提供了Java編程來設(shè)置PDF背景色的方法,需要的朋友可以參考下
    2019-07-07
  • java解決單緩沖生產(chǎn)者消費者問題示例

    java解決單緩沖生產(chǎn)者消費者問題示例

    這篇文章主要介紹了java解單緩沖生產(chǎn)者消費者問題示例,需要的朋友可以參考下
    2014-04-04
  • Javadoc標簽和Javadoc注釋規(guī)范說明

    Javadoc標簽和Javadoc注釋規(guī)范說明

    這篇文章主要介紹了Javadoc標簽和Javadoc注釋規(guī)范說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java Http接口加簽、驗簽操作方法

    Java Http接口加簽、驗簽操作方法

    下面小編就為大家?guī)硪黄狫ava Http接口加簽、驗簽操作方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • 深入理解java中i++和++i的區(qū)別

    深入理解java中i++和++i的區(qū)別

    下面小編就為大家?guī)硪黄钊肜斫鈐ava中i++和++i的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • 最通俗的白話講解JDK源碼中的ThreadLocal

    最通俗的白話講解JDK源碼中的ThreadLocal

    ThreadLocal是JDK包提供的,它提供線程本地變量,如果創(chuàng)建一樂ThreadLocal變量,那么訪問這個變量的每個線程都會有這個變量的一個副本,在實際多線程操作的時候,操作的是自己本地內(nèi)存中的變量,從而規(guī)避了線程安全問題,感興趣的朋友快來看看吧
    2022-01-01
  • java中創(chuàng)建兩表之間的觸發(fā)器詳解

    java中創(chuàng)建兩表之間的觸發(fā)器詳解

    這篇文章主要介紹了java中創(chuàng)建兩表之間的觸發(fā)器詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-06-06
  • Javaweb實現(xiàn)上傳下載文件的多種方法

    Javaweb實現(xiàn)上傳下載文件的多種方法

    本篇文章主要介紹了Javaweb實現(xiàn)上傳下載文件,有多種實現(xiàn)方式,需要的朋友可以參考下。
    2016-10-10
  • java在hashmap初始化時賦初值過程解析

    java在hashmap初始化時賦初值過程解析

    這篇文章主要介紹了java在hashmap初始化時賦初值過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10

最新評論

雷山县| 习水县| 高台县| 上蔡县| 渭源县| 二连浩特市| 广东省| 股票| 滦平县| 灵丘县| 蚌埠市| 盐边县| 孝义市| 朝阳区| 札达县| 伊金霍洛旗| 周宁县| 金寨县| 大同市| 四川省| 广宁县| 南靖县| 辽源市| 和田县| 西乌| 天等县| 芜湖县| 吴忠市| 招远市| 德惠市| 资中县| 沾化县| 永春县| 溧水县| 井研县| 四子王旗| 曲松县| 桃源县| 阿坝| 荥经县| 新丰县|