1

I have a spring boot application & this piece of code as below

@ConfigurationProperties(prefix = “asynchronous-helper”)
public class AsynchronousHelper {
   private transient ExecutorService executor;
}

In properties file I have

asynchronous-helper.executor.maximumPoolSize=10
asynchronous-helper.executor.corePoolSize=10

While maximumPoolSize works corePoolSize fails with below error

Failed to bind properties under ‘asynchronous-helper.executor’ to java.util.concurrent.ExecutorService:

Property: asynchronous-helper.executor.corepoolsize
Value: 10
Origin: “asynchronous-helper.executor.corePoolSize” from property source “class path resource [backend-product.properties]”
Reason: Failed to bind properties under ‘asynchronous-helper.executor’ to java.util.concurrent.ExecutorService

Action:
Update your application’s configuration

Concrete class of the executor is java.util.concurrent.ThreadPoolExecutor

Any idea why this happens & how to resolve it?

Venkat
  • 23
  • 6
  • spring boot version ? – R.G Feb 22 '20 at 05:41
  • will [this](https://stackoverflow.com/a/59691341/4214241) help ? – R.G Feb 22 '20 at 05:45
  • spring-boot version 2.1.1.RELEASE – Venkat Feb 22 '20 at 05:59
  • Try annotating the class `AsynchronousHelper ` with `@Component` or `@Configuration` so that it will be auto detected during component scan . – R.G Feb 22 '20 at 06:39
  • What is the concrete type of your `executor` and how have you created an instance of it? – Andy Wilkinson Feb 22 '20 at 09:34
  • R.G - There is no issue in detecting. Like I have mentioned maximumPoolSize works only corePoolSize fails. – Venkat Feb 22 '20 at 11:54
  • @AndyWilkinson Concrete class of the executor is java.util.concurrent.ThreadPoolExecutor. Added details in the question. – Venkat Feb 22 '20 at 13:58
  • How do you know maximumPoolSize is working? Not saying that it is not, but just want to know how exactly you proved that it is working. – Jose Martinez Feb 22 '20 at 15:00
  • @JoseMartinez Our framework provides a Bean navigator implementation to read & set all properties of any bean. It uses reflection. But we want to move to standard way of overriding. That's when I encountered the above issue. – Venkat Feb 24 '20 at 04:54

1 Answers1

0

This worked. Created a config bean

@Configuration
public class AsyncHelperConfig {
  @Value("${asynchronous-helper.executor.core-pool-size:10}")
  private Integer corePoolSize;

  @Value("${asynchronous-helper.executor.maximum-pool-size:10}")
  private Integer maximumPoolSize;

  @Value("${asynchronous-helper.executor.keep-alive-time:10}")
  private Integer keepAliveTime;

  private transient ExecutorService executor;

@Bean
public AsynchronousHelper asynchronousHelper(){
    AsynchronousHelper asynchronousHelper = new AsynchronousHelper();
    executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 
   keepAliveTime, TimeUnit.MINUTES,
            new LinkedBlockingQueue<Runnable>());
    asynchronousHelper.setExecutor(executor);
    return asynchronousHelper;
}

And in the beans.xml added the below config

<bean id="AsyncHelperConfig" 
    class="asynchonous.AsyncHelperConfig"/>
Venkat
  • 23
  • 6