0

I'm trying to use Web Sockets and @Async tasks.

So I created 2 configuration classes:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig {
}

@Configuration
@EnableAsync
public class AsyncConfiguration {
}

I also have a service injecting the AsyncTaskExecutor:

@Service
public class MyService {

    @Autowired
    private AsyncTaskExecutor asyncTaskExecutor;
}

After this, my application doesn't boot, and this error is reported:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field asyncTaskExecutor in mypackage.MyService required a bean of type 'org.springframework.core.task.AsyncTaskExecutor' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'applicationTaskExecutor' in 'TaskExecutionAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: java.util.concurrent.Executor; SearchStrategy: all) found beans of type 'java.util.concurrent.Executor' clientInboundChannelExecutor, clientOutboundChannelExecutor, brokerChannelExecutor
    - Bean method 'taskScheduler' in 'TaskSchedulingAutoConfiguration' not loaded because @ConditionalOnBean (names: org.springframework.context.annotation.internalScheduledAnnotationProcessor; SearchStrategy: all) did not find any beans named org.springframework.context.annotation.internalScheduledAnnotationProcessor


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.core.task.AsyncTaskExecutor' in your configuration.

How can I solve this?

Beto Neto
  • 3,962
  • 7
  • 47
  • 81
  • `@EnableAsync` enables async it doesn't register a `TaskExecutor`. – M. Deinum Jul 08 '20 at 17:31
  • But the `TaskExecutionAutoConfiguration` has a `applicationTaskExecutor` method who defines the bean `applicationTaskExecutor` of type `ThreadPoolTaskExecutor` who is a descendent of `AsyncTaskExecutor`. – Beto Neto Jul 08 '20 at 17:44
  • Which backsoff because of the websocket configuration registering an `Executor` and thus the other one isn't configured. And as that is an `Executor` and not an `AsyncTaskExecutor` it isn't injectable. – M. Deinum Jul 08 '20 at 18:49

1 Answers1

2

A workaround that works for me:

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(11);
        executor.setThreadNamePrefix("AsyncExecutor-");
        executor.initialize();
        return executor;
    }

    @Override
    public Executor getAsyncExecutor() {
        return asyncTaskExecutor();
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

}
Beto Neto
  • 3,962
  • 7
  • 47
  • 81