I have a need to dynamically create and destroy HttpClient
objects, to correspond with clients registering/deregistering themselves with my Micronaut application. As part of this I want to add them as beans to the application context so that they automatically use the custom HttpFilter
s also in the project.
I thought it would be fairly simple to manage these beans using the ApplicationContext
bean methods with a name Qualifier
to keep track of them, but the way these APIs seem to behave is baffling to me:
applicationContext.createBean(HttpClient.class, Qualifiers.byName("myLabel"), myUrl)
fails with:
io.micronaut.context.exceptions.NoSuchBeanException: No bean of type
[io.micronaut.http.client.HttpClient] exists. Make sure the bean is not disabled by bean
requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the
bean is enabled then ensure the class is declared a bean and annotation processing is
enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as
an annotation processor).
Why does it matter if the bean exists? I'm trying to create it!
applicationContext.findBean(HttpClient.class)
fails with:
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [io.micronaut.http.client.DefaultHttpClient]
Message: Missing bean argument [LoadBalancer loadBalancer] for type: io.micronaut.http.client.DefaultHttpClient. Required arguments: LoadBalancer loadBalancer,HttpClientConfiguration configuration,String contextPath,HttpClientFilterResolver filterResolver
Why is it trying to create it? I just want to find it!
(NOTE. applicationContext.getBean(HttpClient.class, Qualifiers.byName("myLabel"))
may work here, but because I haven't been able to resolve point one I haven't been able to verify this)
applicationContext.destroyBean(HttpClient.class)
doesn't allow aQualifier
to be specified in the method which means I can't use it to remove the bean from the context. It also return null after bean creation without a qualifier (applicationContext.createBean(HttpClient.class, myUrl)
) which suggests it can't find the created bean anyway...
I assume I'm using the wrong API here, but what is the correct one?
All-in-all I'm thoroughly confused - any help on the proper use of these APIs would be welcome.