国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot @Async 異步任務執行方法

Spring Boot @Async 異步任務執行方法

2021-04-27 14:22不要亂摸 Java教程

本篇文章主要介紹了Spring Boot @Async 異步任務執行方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1、任務執行和調度

spring用taskexecutor和taskscheduler接口提供了異步執行和調度任務的抽象。

spring的taskexecutor和java.util.concurrent.executor接口時一樣的,這個接口只有一個方法execute(runnable task)。

1.1、taskexecutor類型

spring已經內置了許多taskexecutor的實現,你沒有必要自己去實現:

  1. simpleasynctaskexecutor  這種實現不會重用任何線程,每次調用都會創建一個新的線程。
  2. synctaskexecutor  這種實現不會異步的執行
  3. concurrenttaskexecutor  這種實現是java.util.concurrent.executor的一個adapter。
  4. simplethreadpooltaskexecutor  這種實現實際上是quartz的simplethreadpool的一個子類,它監聽spring的聲明周期回調。
  5. threadpooltaskexecutor  這是最常用最通用的一種實現。它包含了java.util.concurrent.threadpoolexecutor的屬性,并且用taskexecutor進行包裝。

1.2、注解支持調度和異步執行

to enable support for @scheduled and @async annotations add @enablescheduling and @enableasync to one of your

?
1
2
3
4
5
6
7
@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的,因此,相同類中的方法調用帶@async的方法是無法異步的,這種情況仍然是同步。

舉個例子:下面這種,在外部直接調用sayhi()是可以異步執行的,而調用sayhello()時sayhi()仍然是同步執行

?
1
2
3
4
5
6
7
8
9
10
public class a {
   public void sayhello() {
    sayhi();
  }
 
  @async
  public void sayhi() {
 
  }  
}

1.3、@async注解

在方法上加@async注解表示這是一個異步調用。換句話說,方法的調用者會立即得到返回,并且實際的方法執行是想spring的taskexecutor提交了一個任務。

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.

?
1
2
3
4
@async
void dosomething() {
  // this will be executed asynchronously
}
?
1
2
3
4
@async
void dosomething(string s) {
  // this will be executed asynchronously
}
?
1
2
3
4
@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注解時,將會使用一個支持注解驅動的executor。然而,@async注解的value值可以指定一個別的executor

?
1
2
3
4
@async("otherexecutor")
void dosomething(string s) {
  // this will be executed asynchronously by "otherexecutor"
}

這里,otherexecutor是spring容器中任意executor bean的名字。

1.5、@async異常管理

當一個@async方法有一個future類型的返回值時,就很容易管理在調future的get()方法獲取任務的執行結果時拋出的異常。如果返回類型是void,那么異常是不會被捕獲到的。

?
1
2
3
4
5
6
7
public class myasyncuncaughtexceptionhandler implements asyncuncaughtexceptionhandler {
 
  @override
  public void handleuncaughtexception(throwable ex, method method, object... params) {
    // handle exception
  }
}

2、線程池配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
  }
}

調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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);
  }

結果

[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

Spring Boot @Async 異步任務執行方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/cjsblog/p/9016657.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩欧美在线观看一区二区 | 久久久久香蕉视频 | 色爱综合网 | 成人午夜影院 | 黄视频网站在线观看 | 亚洲综合二区 | 日韩精品免费 | 一区欧美| 午夜视频在线网站 | 日韩在线免费电影 | 国产高潮国产高潮久久久91 | 日本高清无卡码一区二区久久 | 精品国产精品三级精品av网址 | 国产精品久久精品 | 91精品久久 | 在线欧美视频 | 国内成人精品2018免费看 | 国产激情在线视频 | 亚洲免费美女视频 | 一本一道久久久a久久久精品91 | 欧美大片一区二区 | 成人高清网站 | 羞羞网站免费观看 | 亚洲国产成人久久 | 久久精品成人 | 91免费视频在线 | av成人在线电影 | 久久精彩 | 成人小视频在线看 | 久久综合99| 日日久| 国产精品免费一区二区 | 一区二区三区视频免费在线观看 | 黄色av大片在线观看 | 亚洲免费看片 | 国产婷婷在线观看 | 久久久久久久久久久久一区二区 | 亚洲高清视频在线 | 久久综合久久久 | 亚洲在线一区二区三区 | 国产高清一区二区 |