Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Enable async processing

    Code Block
    languagejava
    @EnableAsync
    public class AsyncConfig {
    ...
    }


  • Define the thread pool which will execute the async method. The thread pool setup properties are configurable.

    Code Block
    languagejava
    titleAsyncConfig
    @ConfigurationProperties("notification.async-executor")
    @Validated
    public class AsyncConfig {
    
    	@Min(0)
        private int corePoolSize = 2;
        @Min(2)
        private int maxPoolSize = 10;
        @Min(0)
        private int queueCapacity = 2147483647;
        private boolean waitForTasksToCompleteOnShutdown = true;
        private String threadNamePrefix = "Async-";
    
    	@Bean("notificationExecutor")
        public TaskExecutor getThreadAsyncExecutorForNotification() {
            final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(corePoolSize);
            executor.setMaxPoolSize(maxPoolSize);
            executor.setQueueCapacity(queueCapacity);
            executor.setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown);
            executor.setThreadNamePrefix(threadNamePrefix);
            return executor;
        }
    }


    Code Block
    languageyml
    titleapplication.yml
    notification:
        async-executor:
            core-pool-size: 2
            max-pool-size: 10
            queue-capacity: 500
            wait-for-tasks-to-complete-on-shutdown: true
            thread-name-prefix: Async-


  • Mark the method async and mention the thread-pool name

    Code Block
    languagejava
    titleNotificationService
    public class NotificationService {	
    ...
    	@Async("notificationExecutor")
        public Future<Void> processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
    	...	
    	}
    ...
    }