3

I have a question about Spring @Async annotation. I have a controller autowired a service(GnInsuranceDetailsService)

@RequestMapping(value="/agn/AP0W01A_010/insertDetail.do")
public ResponseEntity<?> insertDetail(@ModelAttribute("dto")GnInsuranceDetailsDTO dto,HttpSession session) throws Exception {
    gnInsuranceDetailsDTO.setBkFlag(getAgnUserInfo(session).getBkFlag());
    gnInsuranceDetailsService.insertGnInsuranceDetail(dto);//open another thread to insert data
    MessageDTO dto = new MessageDTO(AgnConstant.INSERT_SUCCESS);
    return new ResponseEntity<MessageDTO>(dto,HttpStatus.OK);// increase return time for client
    }

And the Service insertGnInsuranceDetail method I declare @Async up method.

@Transactional(readOnly = false)
@Async
public void insertGnInsuranceDetail(GnInsuranceDetailsDTO gnInsuranceDetailsDTO) throws Exception{
GnInsuranceDetails entity = gnInsuranceDetailsDTO.convert();
gnInsuranceDetailsDAO.save(detailsEntity);
}

I put the @Async for the service method to increase controller response time for client side,but it does not work as I think. Do I lose somethings?Or How can I modify in the easiest way to use?

Hamilton Lin
  • 117
  • 1
  • 3
  • 10

3 Answers3

3

You would not loose anything, when you put @Async in the method service will be executed in a different thread, Controllers insertDetail method will not be returned until your insertGnInsuranceDetail is returned or thrown any exception.

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Hellow,@kuhajeyan.I use the test Code Thread.sleep(10000L) in front of insertGnInsuranceDetail method begin.but not work.Controller will wait > 10s for service insertGnInsuranceDetail method finished. – Hamilton Lin Oct 05 '16 at 06:06
  • @HamiltonLin are you sure you enabled @ EnableAsync in configuration for @ Async to work – kuhajeyan Oct 05 '16 at 06:12
  • How to set this config in spring – Hamilton Lin Oct 05 '16 at 06:18
  • @HamiltonLin you need to annotate your configuration class (where you instantiate beans) or else if it xml based add refer : http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html – kuhajeyan Oct 05 '16 at 09:35
  • 1
    Yes,I get it and success. Only set the in application.xml.Then you can use @Async in method – Hamilton Lin Oct 05 '16 at 12:55
0

I put the @Async for the service method to increase controller response time for client side,but it does not work as I think.

@Async - Annotation that marks a method as a candidate for asynchronous execution. Can also be used at the type level, in which case all of the type's methods are considered as asynchronous.

This @Async annotation will not help you in delaying the response time. To introduce a delay, use Thread.sleep(milliseconds);


If you want main thread(controller) to wait for insert data thread to get a result (successful/failure) you can just invoke db layer code from the controller. Whereas if you want the client response to be sent earlier, then create a new thread inside the controller and use that thread to insert data. In the latter approach of creating a new thread for insert data your client will not know about the status of data insertion, since the thread is created in the controller and it will end in the controller, no feedback/response can be given to the client, since we will not be having client details.

Hope it Helps!

Reference: https://www.tutorialspoint.com/java/lang/thread_sleep_millis.htm

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
0

@Async should be first enabled in your @Configuration bean:

@Configuration
@EnableAsync
public class AppConfiguration {

    [...]
}

for xml configuration add this: <task:annotation-driven>

alex
  • 8,904
  • 6
  • 49
  • 75