การแนะนำ
ในการประมวลผลธุรกิจบางธุรกิจใช้วิธีการแบบอะซิงโครนัสที่สมเหตุสมผลมากขึ้น ตัวอย่างเช่นในตรรกะทางธุรกิจบางอย่างข้อมูลบางอย่างจะถูกเก็บไว้ในแคช Redis แคชเป็นเพียงฟังก์ชั่นเสริม ความสำเร็จหรือความล้มเหลวจะไม่มีผลกระทบพื้นฐานต่อธุรกิจหลัก กระบวนการนี้สามารถดำเนินการผ่านวิธีการแบบอะซิงโครนัส
ในฤดูใบไม้ผลิวิธีนี้สามารถเรียกได้แบบอะซิงโครนัสโดยการตั้งค่าคำอธิบายประกอบ @async บนวิธีนี้ กล่าวคือวิธีการจะกลับมาทันทีเมื่อการเรียกร้องและการดำเนินการจริงของวิธีนี้จะถูกส่งมอบให้กับ Taskexecutor ของ Spring เพื่อให้เสร็จสมบูรณ์
ตัวอย่างรหัส
โครงการเป็นโครงการฤดูใบไม้ผลิปกติไฟล์การกำหนดค่าสปริง:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: task = "http://www.springframework.org/schema/task http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/task http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd base-package = "com.lzumetal.ssm"/> <!-พูลเธรดสำหรับการดำเนินงานแบบอะซิงโครนัส taskexecutor-> <task: executor id = "myexecutor" พูลขนาด = "5"/>
สองคลาสบริการ:
แพ็คเกจ com.lzumetal.ssm.anotation.service; นำเข้า org.slf4j.logger; นำเข้า org.slf4j.loggerfactory; นำเข้า org.springframework.beans.factory.annotation.autowired; java.util.concurrent.executionexception; นำเข้า java.util.concurrent.future;/** * บริการธุรกิจ */@servicePublic คลาสธุรกิจบริการ {ส่วนตัว logger สุดท้ายคงที่ = loggerFactory.getLogger @autowired Cacheservice Cacheservice; โมฆะสาธารณะ Dobusiness () {log.error ("เริ่มจัดการกับธุรกิจของเรา"); cacheservice.cachedata (); log.error ("Comlete Service Operation"); } / *** รับค่าส่งคืนสำหรับการดำเนินการแบบอะซิงโครนัส* / โมฆะสาธารณะ dobusinesswithasyncreturn () โยนการดำเนินการ ExecutionException, interruptedException {log.error ("เริ่มจัดการกับธุรกิจของเรา"); อนาคต <String> future = cacheservice.cachedatawithReturn (); log.error (future.get ()); //future.get () เมธอดคือการปิดกั้น log.error ("Comlete Service Operation"); - แพ็คเกจ com.lzumetal.ssm.anotation.service; นำเข้า org.slf4j.logger; นำเข้า org.slf4j.loggerfactory; นำเข้า org.springframework.scheduling.annotation.async; org.springframework.stereotype.service; นำเข้า java.util.concurrent.future; นำเข้า java.util.concurrent.timeUnit;/*** บริการแคช*/@cacheservice cacheservice @async (value = "myexecutor") // ระบุ taskexecutor เพื่อดำเนินการงานโมฆะสาธารณะงาน cachedata () {ลอง {timeunit.seconds.sleep (3L); } catch (interruptedException e) {e.printStackTrace (); } log.error ("ความสำเร็จจัดเก็บผลลัพธ์ลงในแคช"); } @async อนาคตสาธารณะ <String> CacheDatAwithReturn () {ลอง {timeUnit.seconds.sleep (3L); } catch (interruptedException e) {e.printStackTrace (); } log.error ("ความสำเร็จจัดเก็บผลลัพธ์ลงในแคช"); // ผลลัพธ์ที่ส่งคืนจะต้องถูกห่อหุ้มโดยคลาส asyncresult ส่งคืนใหม่ asyncresult <> ("ความสำเร็จในการดำเนินการ async"); -คลาสทดสอบ:
แพ็คเกจ com.lzumetal.ssm.anotation.test; นำเข้า com.lzumetal.ssm.anotation.service.businessservice; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.beans.beans. org.springframework.test.context.contextconfiguration; นำเข้า org.springframework.test.context.junit4.springjunit4classrunner; นำเข้า java.util.concurrent.timeunit; @runwith {"classpath: spring-context.xml"}) การบำรุงรักษาคลาสสาธารณะ {@autowired BusinessService BusinessService; @Test Public Void Test () พ่น InterruptedException {BusinessService.dobusiness (); // อย่าปล่อยให้เธรดหลักสิ้นสุดเร็วเกินไปมิฉะนั้นคอนโซลจะไม่เห็นเนื้อหาเอาต์พุตในวิธีการแบบอะซิงโครนัส TimeUnit.seconds.sleep (5L); } @Test โมฆะสาธารณะ TestAsyNcReturn () โยนข้อยกเว้น {BusinessService.DobusinessWithAsyncReturn (); TimeUnit.seconds.sleep (5L); -ผลลัพธ์ของการดำเนินการทดสอบ () วิธี:
22: 20: 33,207 ข้อมูลการสนับสนุนหลัก DEFAULTTESTCONTEBOOTSTRAPPER: 260 - โหลด TESTEXECUTIONLISTENLISTENER ชื่อคลาสจากตำแหน่ง [meta -inf/spring.factories]: [org.springframework.test.context.web.servlettexecec org.springframework.test.context.support.dirtiescontextbeforemodestestexecutionListener, org.springframework.test.context.support.dependinupporttestexecutionlistener, org.springframework org.springframework.test.context.transaction.transactionaltestexecutionListener, org.springframework.test.context.jdbc.sqlscriptstestexecutionListener]
22: 20: 33,226 ข้อมูลการสนับสนุนหลัก DEFAULTTESTCONTEBOOTSTRAPPER: 209 - ไม่สามารถสร้างอินสแตนซ์ testExecutionListener [org.springframework.test.context.web.servlettestexecutionListener] ระบุคลาสฟังที่กำหนดเองหรือทำให้คลาสฟังเริ่มต้น (และการพึ่งพาที่ต้องการ) มีให้ คลาสที่ละเมิด: [Javax/servlet/servletContext]
22: 20: 33,227 ข้อมูลการสนับสนุนหลัก DEFAULTTESTCONTEBOOTSTRAPPER: 187 - การใช้ TESTEXECUTIONLISTENERS: [org.springFramework.test.Context.support.dirtiesContextBeforeModestEstexecutionListener@100fc185 org.springframework.test.context.support.dependencedintestexecutionlistener@643b1d11, org.springframework.test.context.support.dirtiescontextestestexecutionlistener@2ef5e5e3 org.springframework.test.context.transaction.transactionaltestexecutionlistener@36d4b5c, org.springframework.test.context.jdbc.sqlscriptstestexecutionlistener@6d00a15d] 22: 20: 33 การโหลดคำจำกัดความของถั่ว xml จากทรัพยากรพา ธ คลาส [Spring-context.xml]
22: 20: 33,585 ข้อมูลการสนับสนุนหลัก GenericApplicationContext: 583 - รีเฟรช org.springframework.context.support.genericapplicationcontext@4f7d0008: วันที่เริ่มต้น [พุธ 30 22:20:33 CST 2018]; รากของลำดับชั้นบริบท
22: 20: 33,763 ข้อมูลหลักพร้อมกัน ThreadPoolTaskexecutor: 165 - การเริ่มต้น ExecutorService
22: 20: 33,766 ข้อมูลการสนับสนุนหลัก ProcessorRegistrationDelegate $ beanpostprocessorChecker: 325 - ถั่ว 'myexecutor' ประเภท [org.springframework.scheduling.config.taskexecutorbeanbean auto-proxying)
22: 20: 33,767 ข้อมูลการสนับสนุนหลัก PostProcessorRegistrationDelegate $ beanpostprocessorChecker: 325 - ถั่ว 'myExecutor' ประเภท [org.springframework.scheduling auto-proxying)
22: 20: 34,107 ข้อผิดพลาดบริการหลักธุรกิจบริการ: 24 - เริ่มจัดการกับธุรกิจของเรา
22: 20: 34,113 ข้อผิดพลาดบริการหลักบริการธุรกิจ: 26 - การดำเนินการบริการที่สมบูรณ์
22: 20: 37,166 ข้อผิดพลาด MyExecutor -1 บริการ. Cacheservice: 28 - Success Store ผลลัพธ์ในแคช
22: 20: 39,117 INFO เธรด -0 Support.GenericApplicationContext: 984 - การปิด org.springframework.context.support.genericapplicationcontext@4f7d0008: วันที่เริ่มต้น [พุธ 30 22:20:33 CST 2018]; รากของลำดับชั้นบริบท
22: 20: 39,118 INFO เธรด -0 พร้อมกัน ThreadPoolTaskexecutor: 203 - การปิด ExecutorService
ผลลัพธ์ของการดำเนินการ testasyncReturn () วิธี:
21: 38: 16,908 ข้อมูลการสนับสนุนหลัก DEFAULTTESTCONTEBOOTSTRAPPER: 260 - โหลด TESTEXECUTIONLISTENER เริ่มต้นชื่อคลาสจากตำแหน่ง [META -INF/Spring.Factories]: [org.springframework.test.context.web.servlettestexecutionlistener org.springframework.test.context.support.dirtiescontextbeforemodestestexecutionListener, org.springframework.test.context.support.dependinupporttestexecutionlistener, org.springframework org.springframework.test.context.transaction.transactionaltestexecutionListener, org.springframework.test.context.jdbc.sqlscriptstestexecutionListener]
21: 38: 16,926 ข้อมูลการสนับสนุนหลัก defaultTestContextBootStrapper: 209 - ไม่สามารถสร้างอินสแตนซ์ testexecutionListener [org.springframework.test.context.web.servlettestexecutionListener] ระบุคลาสฟังที่กำหนดเองหรือทำให้คลาสฟังเริ่มต้น (และการพึ่งพาที่ต้องการ) มีให้ คลาสที่ละเมิด: [Javax/servlet/servletContext]
21: 38: 16,927 ข้อมูลการสนับสนุนหลัก DEFAULTTESTCONTEBOOTSTRAPPER: 187 - การใช้ testExecutionListeners: [org.springframework.test.context.support.dirtiescontextebeforemodestexecutionlistener@100fc185 org.springframework.test.context.support.dependencedintestexecutionlistener@643b1d11, org.springframework.test.context.support.dirtiescontextestestexecutionlistener@2ef5e5e3 org.springframework.test.context.transaction.transactionaltestexecutionlistener@36d4b5c, org.springframework.test.context.jdbc.sqlscriptstestexecutionlistener@6d00a15d] 21: 38: 17,025 การโหลดคำจำกัดความของถั่ว xml จากทรัพยากรพา ธ คลาส [Spring-context.xml]
21: 38: 17,263 ข้อมูลการสนับสนุนหลัก GenericApplicationContext: 583 - รีเฟรช org.springframework.context.support.genericapplicationcontext@4f7d0008: วันที่เริ่มต้น [พุธ 30 21:38:17 CST 2018]; รากของลำดับชั้นบริบท
21: 38: 17,405 ข้อมูลหลักพร้อมกัน ThreadPoolTaskexecutor: 165 - การเริ่มต้น ExecutorService
21: 38: 17,407 ข้อมูลการสนับสนุนหลัก PostProcessorRegistrationDelegate $ beanpostprocessorChecker: 325 - ถั่ว 'myExecutor' ประเภท [org.springframework.scheduling.config.taskexecutorbeanbean auto-proxying)
21: 38: 17,407 ข้อมูลการสนับสนุนหลัก PostProcessorRegistrationDelegate $ beanpostprocessorChecker: 325 - ถั่ว 'myExecutor' ประเภท [org.springframework.scheduling auto-proxying)
21: 38: 17,692 ข้อผิดพลาดบริการหลักธุรกิจบริการ: 35 - เริ่มจัดการกับธุรกิจของเรา
21: 38: 20,833 ข้อผิดพลาด MyExecutor -1 Service.Cacheservice: 39 - Success จัดเก็บผลลัพธ์ในการแคช
21: 38: 20,834 ข้อผิดพลาดบริการหลักธุรกิจบริการ: 37 - ความสำเร็จในการดำเนินการ async
21: 38: 20,835 ข้อผิดพลาดบริการหลักบริการธุรกิจ: 38 - การดำเนินการบริการที่สมบูรณ์
21: 38: 25,838 ข้อมูลเธรด -0 Support.GenericApplicationContext: 984 - ปิด org.springframework.context.support.genericapplicationcontext@4f7d0008: วันที่เริ่มต้น [พุธ 30 21:38:17 CST 2018]; รากของลำดับชั้นบริบท
21: 38: 25,839 ข้อมูลเธรด -0 พร้อมกัน ThreadPoolTaskexecutor: 203 - ปิด ExecutorService
@คะแนนการใช้งานของ Async
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น