简体   繁体   中英

How to add Asynchronous execution in spring boot application?

In my spring boot application, There is one scheduler class which execute in every 30 mins and fetch around 20000 records and then insert record one by one in database and then convert that inserted record in XML and send to client. But this process takes too much time to completed 20000 records and records is increasing day by day.

So now, We have to implement asynchronous execution programming in this code so that multiple threads can perform operation instead of one by one records. For example: There are 4 threads and list of item size is 6 then thread_1 will take (0) index object to perform, thread_2 will take (1) index object to perform, thread_3 will take (2) index object to perform, thread_4 will take (3) index object to perform, thread_1 will take (4) index object to perform, thread_2 will take (5) index object to perform

Something like that

For this case, How to implement asynchronous execution

@Service
class ItemService 
{
    @autowired
    private DemoDao dao;
    
    @Scheduled(fixedRate=30000)
    public void scheduler(){
        try {
         List<Item> itemList = dao.getItems();
         saveAndSend(itemList);
        } catch(Exception e) {
            e.printStack();
        }
    }
    
    public void saveAndSend(List<Item> itemList) throws Exception {
        
        for(int i = 0; i < itemList.size(); i++){
            if(itemList.get(i).isDone){
                dao.save(itemList.get(i));
                int flag = convertInXMLAndSendToTeam(itemList.get(i));
                if(flag == 1){
                    dao.audit(itemList.get(i), "XEQ");
                }
            }
        }  
    }
}

Please help.

Try putting a name, it works for me like this

@Async("ThreadPoolTaskExecutor")

In SprintBootApplication put the following

@EnableAsync

and then

@Bean("ThreadPoolTaskExecutor")
  public TaskExecutor getAsyncExecutor() {
    final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(100);
    executor.setMaxPoolSize(200);
    executor.setQueueCapacity(30);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setThreadNamePrefix("Async-");
    executor.setTaskDecorator(new APMTaskDecorator());
    return executor;
  }

If you use @Async and @Scheduled in same class the async methods are still executed synchronously. @Async generates a proxy class which encapsulated the original method. So @Async works only if you invoke the method externally. Hence the @Scheduled on the same class is still executed synchronously.

The simpliest solution would be to move the @Scheduled methods to a sperate class.

@Service
public class ItemService {
    @Async
    public void saveAndSend() {

    }
}
@Service
public class ItemSchedulerService {

    private final ItemService itemService;

    public ItemSchedulerService(ItemService itemService) {
        this.itemService = itemService;
    }

    @Scheduled("...")
    public void schedule() {
        itemService.saveAndSend();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM