turing85 opened a new issue, #4725:
URL: https://github.com/apache/camel-quarkus/issues/4725

   ### Description
   We added the support to automatically register beans annotated with 
`@Identifier` in #4374 (commit 
https://github.com/apache/camel-quarkus/commit/6f4228848ef68eff290ef735d8552dcc23cf7266#diff-79ee191174431bde21e06eae2287b5a2e61d6a7b39a3e6d5915d5b2c2455f532)
   Bean registration fails, however, when multiple beans with different types 
have the same `@Identifier("name")`.
   
   ---
   ### Example
   
   Bean defining class:
   ```
   public class BeanProducer {
       @Produces
       @Typed({ ConnectionFactory.class })
       @ApplicationScoped
       @Identifier("externally-defined")
       ActiveMQConnectionFactory externallyDefinedConnectionFactory(
               @ConfigProperty(name = "artemis.externally-defined.url") String 
externallyDefinedUrl) {
           return new ActiveMQConnectionFactory(externallyDefinedUrl);
       }
   
       @Produces
       @ApplicationScoped
       @Typed({ ArtemisJmsConsumerManager.class })
       @Identifier("externally-defined")
       ArtemisJmsConsumerManager externalConsumerManager(
               @Identifier("externally-defined") ConnectionFactory 
connectionFactory) {
           return new ArtemisJmsConsumerManager(connectionFactory, "out");
       }
       // other beans with differend @Identifiers
   }
   ```
   Usage in route:
   ```
   public class TransferRoute extends RouteConfigurationBuilder {
   
       @Override
       public void configuration() {
           from(jms("queue:in").connectionFactory("named"))
                   
.to(jms("queue:out").connectionFactory("externally-defined"));
       }
   }
   ```
   
   results in:
   ```
   ...
   Caused by: jakarta.enterprise.inject.AmbiguousResolutionException: 
[PRODUCER_METHOD bean 
[class=io.quarkus.it.artemis.camel.jms.withnamedandexternal.BeanProducer, 
id=993a3bbccfc0b1c95e14b9f04ff12402c4215353], PRODUCER_METHOD bean 
[class=io.quarkus.it.artemis.camel.jms.withnamedandexternal.BeanProducer, 
id=8ebca384354a86c6da0c74eaf28ad6affbcf0ee1]]
       at 
io.quarkus.arc.impl.ArcContainerImpl.resolve(ArcContainerImpl.java:592)
       at io.quarkus.arc.impl.BeanManagerImpl.resolve(BeanManagerImpl.java:114)
       at 
org.apache.camel.quarkus.core.RuntimeBeanRepository.getReferenceByName(RuntimeBeanRepository.java:80)
       at 
org.apache.camel.quarkus.core.RuntimeBeanRepository.lambda$lookupByNameAndType$2(RuntimeBeanRepository.java:119)
       at java.base/java.util.Optional.flatMap(Optional.java:289)
       at 
org.apache.camel.quarkus.core.RuntimeBeanRepository.lookupByNameAndType(RuntimeBeanRepository.java:119)
       at 
org.apache.camel.quarkus.core.RuntimeBeanRepository.lookupByName(RuntimeBeanRepository.java:113)
       at 
org.apache.camel.support.DefaultRegistry.lookupByName(DefaultRegistry.java:220)
       at 
org.apache.camel.support.CamelContextHelper.lookup(CamelContextHelper.java:190)
       at 
org.apache.camel.support.CamelContextHelper.mandatoryLookupAndConvert(CamelContextHelper.java:246)
       at 
org.apache.camel.support.EndpointHelper.resolveReferenceParameter(EndpointHelper.java:351)
       at 
org.apache.camel.support.EndpointHelper.resolveReferenceParameter(EndpointHelper.java:298)
       at 
org.apache.camel.support.DefaultComponent.resolveAndRemoveReferenceParameter(DefaultComponent.java:629)
       at 
org.apache.camel.support.DefaultComponent.resolveAndRemoveReferenceParameter(DefaultComponent.java:592)
       at 
org.apache.camel.component.jms.JmsComponent.createEndpoint(JmsComponent.java:1139)
       at 
org.apache.camel.support.DefaultComponent.createEndpoint(DefaultComponent.java:170)
       at 
org.apache.camel.impl.engine.AbstractCamelContext.doGetEndpoint(AbstractCamelContext.java:876)
       ... 77 more
   ```
   
   ---
   ### Expected behaviour
   Bean is properly resolved through its type
   
   ---
   ### Actual behavour
   Application fails to start up with above exception
   
   ---
   ### Analysis and possible solution
   The problem is cased in 
[`RuntimeBeanRepository`](https://github.com/apache/camel/blob/a1c585eeaedacc4b4e90200c904289ab650d61f0/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java#L351),
 because the bean is looked up without its type, thus two beans are found and 
the above exception is thrown.
   
   One possible solution would be to first try to get the bean with its type, 
and only if this fails get the bean without type and call the converter. Risk: 
it might be possible that some code relies on some converter call, even if 
conversion is not strictly necessary.
   
   @jamesnetherton already did some experimentation 
[here](https://github.com/jamesnetherton/camel/commit/b20b570ad2242b3286aceb4e7e0399622727765c).
   
   ---
   FTR: [corresponding Zulip 
conversation](https://camel.zulipchat.com/#narrow/stream/257302-camel-quarkus/topic/Resolution.20of.20.40Identifier.20dependency).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to