Spring Boot를 사용하기 전에, 우리의 접근 방식은 구성 파일에서 작업 풀을 정의한 다음 @async 주석화 작업을 작업 풀에 버리기 위해 실행하는 것이 었습니다. 따라서 Spring Boot에서는 비동기 작업 호출을 구현하는 방법이 더 간단합니다.
이전을 결합합시다
Spring Boot Integrated JMS (ActiveMQ 구현)
이 블로그의 코드가 구현되었습니다.
1. 함수 설명
소비자가 큐의 메시지를 듣는 경우 메시지를 비동기 작업으로받는 작업을 처리합니다.
2. 코드 수정
소비자 1 :
패키지 com.chhliu.springboot.jms; import org.springframework.jms.annotation.jmslistener; import org.springframework.scheduling.annotation.async; org.springframework.stereotyp.component import; @Component public class consumer {@jmslistener (destination = "myTest.queue") @Async //이 메소드는 비동기 적으로 실행되며, 이는 주 스레드가 메소드를 직접 건너 뛰지 만 스레드 풀의 스레드를 사용하여 공개 avoid lecequeue (string {system.out.println (). "+텍스트); }}소비자 2 :
패키지 com.chhliu.springboot.jms; import org.springframework.jms.annotation.jmslistener; org.springframework.messaging.handler.annotation.sendto; org.springframework.stereotyp.component import; @Component public class consumer2 {@jmslistener (대상 = "mytest.queue") @sendto ( "out.queue") public string lecequeue (String text) {system.out.println (stride.currentThread (). getName ()+":"+text); return "return message"+텍스트; }}테스트 클래스에 다음 주석을 추가하십시오.
패키지 com.chhliu.springboot.jms; javax.jms.destination import; import org.apache.activemq.command.activemqqueue; import org.junit.test; import org.junit.runner.runwith; org.springframework.beans.factory.annotation.autowired; org.springframework.boot.test.context.springboottest; org.springframework.scheduling.annotation.enableasync; org.springframework.test.context.junit4.springrunner import; @runwith (springrunner.class) @springboottest @enableAsync // 공개 클래스를 지원하기위한 비동기 작업 활성화 SpringBootjmsApplicationTests {@autowired Private Producer Producer; @test public void contextloads ()는 interruptedException {대상 대상 = new activemqqueue ( "mytest.queue"); for (int i = 0; i <100; i ++) {producer.sendmessage (대상, "myname is chhliu !!!"); }}} 3. 테스트 결과
DefaultMessAgelistenerContainer-1 : Consumer2 수신 : MyName은 chhliu !!! queue equeue에서받은 응답 메시지는 : return messagemyname은 chhliu입니다 !!! Simpleasynctaskexecutor-45 : 소비자가받은 메시지는 : MyName은 chhliu입니다 !!! DefaultMessAgelistenerContainer-1 : Consumer2 수신 : MyName은 chhliu !!! queue equeue에서받은 응답 메시지는 : return messagemyname은 chhliu입니다 !!! Simpleasynctaskexecutor-46 : 소비자가받은 메시지는 : myname은 chhliu입니다 !!! DefaultMessAgelistenerContainer-1 : Consumer2가받은 메시지는 : MyName은 chhliu !!! queue equeue에서받은 응답 메시지는 : return messagemyname은 chhliu입니다 !!! Simpleasynctaskexecutor-47 : 소비자가받은 메시지는 : MyName은 chhliu입니다 !!! DefaultMessAgelistenerContainer-1 : Consumer2가받은 메시지는 : MyName은 chhliu !!! queue equeue에서받은 응답 메시지는 : return messagemyname은 chhliu입니다 !!! simpleasynctaskexecutor-48 : 소비자가받은 메시지는 : myname은 chhliu입니다 !!! DefaultMessAgelistenerContainer-1 : Consumer2 IS : MyName은 chhliu !!! queue equeue에서받은 응답 메시지는 : return messagemyname은 chhliu입니다 !!! Simpleasynctaskexecutor-49 : 소비자가받은 메시지는 : MyName은 chhliu입니다 !!! DefaultMessAgelistenerContainer-1 : Consumer2가받은 메시지는 : MyName은 chhliu !!! queue equeue에서받은 응답 메시지는 : return messagemyname은 chhliu입니다 !!! Simpleasynctaskexecutor-50 : 소비자가받은 메시지는 : MyName은 chhliu입니다 !!! DefaultMessAgelistenerContainer-1 : Consumer2가받은 메시지는 : MyName은 chhliu !!!
위의 테스트 결과에서 소비자 2가 비동기 작업 방법을 사용하지 않기 때문에 소비자 2는 고정 스레드 DefaultMesseGelistenerContainer-1에 의해 처리된다. 소비자 1은 비동기 작업 방법을 사용하며,마다 수신 된 메시지가 다른 스레드로 처리됩니다. 메시지가 수신되면 작업이 처리를 위해 작업 풀에 직접 던져지고 기본 스레드가 계속 실행됩니다. Spring Boot가 기본적으로 NewCachedthreadpool 스레드 풀을 사용한다는 테스트 결과에서 추론 할 수 있습니다.
스레드 풀의 특정 사용에 대해서는 ME의 다른 블로그 게시물을 참조하십시오 : //www.vevb.com/article/134870.htm
4. 비동기 작업이 반환되었습니다
실제 발전에서, 우리는 종종 비동기 작업이 돌아 오는 상황을 만나게됩니다. 그렇다면 Spring Boot에서 어떻게 구현합니까?
다음은 설명으로 비동기 이메일을 보내는 비동기 이메일의 예입니다. 예제 코드는 다음과 같습니다.
@async ( "taskexecutepool") // 비동기 작업은 TaskexeCutepool 작업 풀에 제출되어 공개 미래를 실행하여 <songeenmail (mailinfo mailinfo)을 실행합니다. DosendEmail 비동기 방법이 호출되었습니다! "); sendmailsession session = null; 응답 res = 새로운 응답 (); boolean isok = sendemail (mailinfo); // 이메일을 보내는 특정 방법 if (isok) {res.setsuccess (true); } else {res.setSuccess (false); } 새로운 Asyncresult <sonation> (RES)을 반환합니다.반환 후 사용하는 방법은 무엇입니까? 샘플 코드는 다음과 같습니다.
미래의 <sonation> result = taskjob.dosendemail (mailinfo); res = result.get (6, timeUnit.seconds);
이렇게하면 비동기 작업의 반환을 얻을 수 있습니다!
요약
위는 스프링 부트 비동기 (Async) 작업 스케줄링의 구현 방법입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!