1。タスクの実行とスケジューリング
Springは、TaskexecutorおよびTaskschedulerインターフェイスを使用して、非同期実行およびスケジューリングタスクの抽象化を提供します。
SpringのTaskexecutorは、java.util.concurrent.executorインターフェイスと同じです。このインターフェイスには、1つのメソッド実行(実行可能なタスク)のみがあります。
1.1。 taskexecutorタイプ
Springには多くのTaskexecutorの実装が組み込まれており、自分で実装する必要はありません。
1.2。注釈は、スケジューリングと非同期実行をサポートします
@scheduledおよび@asyncアノテーションのサポートを有効にするには、@enableSchedulingと@enableasyncを追加します
@configurationクラス:@configuration@enableasync@enableSchedulingpublic class appconfig {}特に注意してください
@ASYNCアノテーションを処理するためのデフォルトのアドバイスモードは「プロキシ」であり、プロキシのみを介した通話の干渉を可能にします。同じクラス内のローカルコールは、そのように傍受することはできません。より高度な干渉モードの場合は、コンパイル時間または負荷時間溶接と組み合わせて「aspectj」モードに切り替えることを検討してください。
デフォルトでは、@Asyncはプロキシで処理されます。したがって、同じクラスのメソッドは、@Asyncを使用して非同期的にメソッドと呼ばれることはできませんが、この状況は依然として同期しています。
たとえば、次の場合、Sayshi()は直接外部的に()を非同期に実行できます。
パブリッククラスA {public void sayhello(){sayhi(); } @async public void sayshi(){}}1.3。 @Asyncアノテーション
このメソッドに@Asyncアノテーションを追加すると、これが非同期呼び出しであることがわかります。言い換えれば、メソッドの発信者はすぐに返品を取得し、実際のメソッド実行は、春のTaskexecutorでタスクを提出することです。
言い換えれば、発信者は呼び出し時にすぐに戻り、メソッドの実際の実行は、春のtaskexecutorに提出されたタスクで発生します。
@asyncvoid dosomething(){//これは非同期に実行されます} @asyncvoid dosomething(string s){//これは非同期に実行されます} @asyncfuture <string> returnsomething(int i){//これは非同期に実行されます}知らせ:
@Asyncメソッドは、通常のjava.util.concurrent.futureのリターンタイプを宣言するだけでなく、springのorg.springframework.util.concurrent.listenablefutureも宣言することができます。
1.4。 @Async Limited Exector
デフォルトでは、@Asyncアノテーションがメソッドに追加されると、注釈ドライバーをサポートするエグゼクティアが使用されます。ただし、@Asyncアノテーションの値値は、追加の執行者を指定できます
@async( "otherexecutor")void dosomething(string s){//これは「otherexecutor」によって非同期に実行されます}ここでは、oterexecutorは、スプリングコンテナのエグゼキュータービーンの名前です。
1.5。 @ASYNC例外管理
@Asyncメソッドの将来のタイプの返品値がある場合、タスクの実行結果を取得するために、未来のget()メソッドを入力するときにスローされる例外を簡単に管理できます。返品タイプが無効である場合、例外はキャッチされません。
パブリッククラスmyasyncuncuntexexceptionhandlerは、asyncuncaughtexceptionhandler {@override public void handle handleuncaughtexception(スロー可能なEx、メソッドメソッド、オブジェクト...パラメーション){//ハンドル例外}}}}}を実装しています。 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@EnableAsyncpublicクラス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 StreadPooltaskexecutor(); executor.setCorepoolsize(corepoolsize); executor.SetMaxPoolsize(MaxPoolsize); executor.setKeepaliveseConds(KeepaliveseConds); // executor.setQueUeCapacity(QueueCapacity); executor.setWaitfortAskStocompleteOnshutdown(true); executor.Initialize();執行者を返す; }}電話
@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(sprestStatusenum.success.getType()); } else {string理由=(null == rst)? 「応答例外」:rst.getMSG(); entity.setfailurer eaveron(理由); entity.setStatus(sprestStatusenum.failure.getType()); }} catch(Exception ex){log.Error(ex.getMessage()、ex); entity.setfailurer eaveron(ex.getMessage()); entity.setStatus(sprestStatusenum.failure.getType()); } couponpresentlogdao.update(entity); }結果
[情報] 2018-05-09 16:27:39.887 [mythreadpooltaskexecutor-1] [com.ourhours.coupon.rpc.dubbo.receivelogfilter] - メソッドを受け取る方法:送信;引数:[10046031、 "4D7CC32F8F7E4B00BCA56F6BF4B3B658"、 "1"、10001]
[情報] 2018-05-09 16:27:39.889 [mythreadpooltaskexecutor-2] [com.ourhours.coupon.rpc.dubbo.receivelogfilter] - メソッドを受信した方法:送信;引数:[10046031、 "4D7CC32F8F7E4B00BCA56F6BF4B3B658"、 "1"、10001]
参照:
Spring Frameworkリファレンスドキュメント4.3.17.Release
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。