เมื่อเร็ว ๆ นี้ฉันพบข้อกำหนดซึ่งก็คือเมื่อเซิร์ฟเวอร์ได้รับการร้องขอมันไม่จำเป็นต้องมีการดำเนินการงานให้เสร็จสมบูรณ์ก่อนที่จะส่งคืนผลลัพธ์ มันสามารถส่งคืนผลลัพธ์ได้ทันทีและปล่อยให้งานดำเนินการแบบอะซิงโครนัส สิ่งแรกที่ต้องพิจารณาคือการเริ่มเธรดใหม่เพื่อดำเนินการหรือส่งงานไปยังพูลเธรดเพื่อดำเนินการ ทั้งสองวิธีก็โอเค แต่ฤดูใบไม้ผลินั้นทรงพลังมากต้องมีวิธีที่ง่ายกว่า ฉันเพิ่ง googled มันและมันมีจริงๆ เพียงใช้สองคำอธิบายประกอบ @enableasync และ @async ให้เป็นโอเค
เพิ่ม @async คำอธิบายประกอบในวิธีการ
แพ็คเกจ me.deweixu.aysncdemo.service; อินเตอร์เฟสสาธารณะ asyncService {void asyncMethod (String arg);} แพ็คเกจ me.deweixu.aysncdemo.service.ipml; นำเข้า me.deweixu.aysncdemo.service.asyncservice; นำเข้า org.springframework.scheduling.annotation.async; asyncService {@async @Override โมฆะสาธารณะ asyncMethod (สตริง arg) {system.out.println ("arg:" + arg); System.out.println ("=====" + thread.currentthread (). getName () + "==================);}} @enableasync
เพิ่มคำอธิบายประกอบ @enableasync ในคลาสเริ่มต้นหรือคลาสการกำหนดค่า
แพ็คเกจ me.deweixu.aysncdemo; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.scheduling.annotation. AYSNCDEMOAPPLICATION {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (AysncDemoapplication.class, args); - ทดสอบ
แพ็คเกจ me.deweixu.aysncdemo; นำเข้า me.deweixu.aysncdemo.service.asyncservice; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.beans.beans. org.springframework.boot.test.context.springboottest; นำเข้า org.springframework.test.context.junit4.springrunner; @runwith (Springrunner.class) @springboottestpublic @Test โมฆะสาธารณะ testAsync () {system.out.println ("=====" + thread.currentthread (). getName () + "======================); asyncService.asyncMethod-
2018-03-25 21: 30: 31.391 ข้อมูล 28742 --- [Main] .saannotationasyncexecutionInterceptor: ไม่พบการประมวลผลถั่วสำหรับการประมวลผล async: ไม่มีถั่วชนิด taskexecutor และไม่มีถั่วชื่อ 'taskexecutor'
Arg: Async
===== SimpleAsyNcTaskexecutor-1 ==============
จากผลลัพธ์ข้างต้น asyncService.asyncMethod ("async") ถูกดำเนินการอย่างแน่นอนแบบอะซิงโครนัสและใช้เธรดใหม่
ระบุผู้ดำเนินการ
จากบันทึกการดำเนินการข้างต้นคุณสามารถเดาได้ว่า Spring ใช้ SimpleAsyNcTaskexecutor เพื่อดำเนินการงานแบบอะซิงโครนัสโดยค่าเริ่มต้น คุณสามารถค้นหาคลาสนี้ @async ยังสามารถระบุผู้ดำเนินการที่กำหนดเองได้
เพิ่มผู้ดำเนินการที่กำหนดเองในคลาสเริ่มต้น
แพ็คเกจ me.deweixu.aysncdemo; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.context.annotatation.bean; org.springframework.scheduling.annotation.enableasync; นำเข้า org.springframework.scheduling.concurrent.threadpooltaskexecutor; นำเข้า Java.util.concurrent.executor; args) {springapplication.run (AysncDemoapplication.class, args); } @Bean (name = "threadpooltaskexecutor") ผู้ดำเนินการสาธารณะ Threadpooltaskexecutor () {ส่งคืน Threadpooltaskexecutor ใหม่ (); -ระบุผู้ดำเนินการ
แพ็คเกจ me.deweixu.aysncdemo.service.ipml; นำเข้า me.deweixu.aysncdemo.service.asyncservice; นำเข้า org.springframework.scheduling.annotation.async; asyncService {@async ("threadpooltaskexecutor") @Override โมฆะสาธารณะ asyncMethod (String arg) {system.out.println ("arg:" + arg); System.out.println ("=====" + thread.currentthread (). getName () + "======================);}}ด้วยวิธีนี้ให้ใช้ threadpooltaskexecutor เมื่อดำเนินงานแบบอะซิงโครนัส
ตั้งค่าผู้ดำเนินการเริ่มต้น
ดังที่ได้กล่าวไว้ข้างต้นหาก @async ไม่ได้ระบุผู้ดำเนินการจะใช้ SimpleAsyNcTaskexecutor โดยค่าเริ่มต้น อันที่จริงแล้วผู้ดำเนินการเริ่มต้นสามารถกำหนดค่าได้โดยใช้อินเตอร์เฟส ASYNCCONFIGURER
@ConfigurationPublic คลาส SpringAsyncConfig ใช้ ASYNCCONFIGURER {@Override Public Executor GetAsyncexecutor () {ส่งคืน ThreadPoolTaskexecutor ใหม่ (); - การจับข้อยกเว้น
ข้อยกเว้นอาจเกิดขึ้นในวิธีการดำเนินการแบบอะซิงโครนัส เราสามารถใช้ลองจับภายในงานเพื่อจัดการข้อยกเว้น เมื่องานโยนข้อยกเว้นสปริงก็มีวิธีการจับ
ใช้อินเตอร์เฟส asyncuncaughtexceptionhandler
คลาสสาธารณะ customasyncexceptionhandler ดำเนินการ asyncuncaughtexceptionhandler {@Override โมฆะสาธารณะที่จับจ่าย thetxception (throwable throwable, วิธีการ, วัตถุ ... obj) {system.out.println ("ข้อความยกเว้น -" throwable.getMessage ()); System.out.println ("ชื่อวิธี -" + method.getName ()); สำหรับ (วัตถุพารามิเตอร์: obj) {system.out.println ("ค่าพารามิเตอร์ -" + param); -ใช้อินเทอร์เฟซ ASYNCCONFIGURER REWRITE GETASYNCUNCUCHETEXceptionHandler
@ConfigurationPublic คลาส SpringAsyncConfig ใช้ ASYNCCONFIGURER {@Override Public Executor GetAsyncexecutor () {ส่งคืน ThreadPoolTaskexecutor ใหม่ (); } @Override สาธารณะ asyncuncaughtexceptionhandler getasyncuncuncaughtexceptionhandler () {ส่งคืน customasyncexceptionhandler ใหม่ (); -เขียนวิธี asyncMethod ใหม่เพื่อให้มันโยนข้อยกเว้น
@async @Override โมฆะสาธารณะ asyncMethod (String arg) {system.out.println ("arg:" + arg); System.out.println ("=====" + thread.currentThread (). getName () + "===================); โยน nullpointerexception ใหม่ ();};ผลการทำงาน:
-
Arg: Async
===== threadpooltaskexecutor-1 =============
ข้อความยกเว้น - async nullpointerexception
ชื่อวิธี - asyncMethod
ค่าพารามิเตอร์ - async
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น