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

   ### Bug description
   
   Support for beans annotated with `@Identifier(...)` instead of `@Named(...)` 
was added in https://github.com/apache/camel-quarkus/issues/4374.
   
   This integration only partially works. As of now, it is unclear whether this 
is a regression, or never fully worked.
   
   ---
   
   A case that works:
   - application uses `quarkus-artemis` and `camel-quarkus-jms`
   - application defines a connection factory programmatically through a 
`@Produces` method:
   ```java
   public class ConnectionFactoryProducer {
     public static final String EXTERNALLY_DEFINED = "externally-defined";
   
     @Produces
     @Identifier(EXTERNALLY_DEFINED)
     @Unremovable
     public ConnectionFactory createConnectionFactory(
         @ConfigProperty(name = "quarkus.artemis.url") String url) {
       return new ActiveMQConnectionFactory(url);
     }
   }
   ```
   - both connection factories are used in camel routes:
   ```java
   public class JmsRoutes extends RouteBuilder {
     @Override
     public void configure() {
       // @formatter:off
       from(jms("default").connectionFactory(ArtemisUtil.DEFAULT_CONFIG_NAME))
           .log("body: ${body}");
       
from(jms("external").connectionFactory(ConnectionFactoryProducer.EXTERNALLY_DEFINED))
           .log("body: ${body}");
       // @formatter:on
     }
   }
   ```
     messages send to queue `default` and `externally-defined` are received 
(verified with JMSToolBox)
   
   ---
   
   A case that does not work:
   - application defines an interface `Greeter`:
   ```java
   public interface Greeter {
     String greet();
   }
   ```
   -  one implementation `HelloGreeter` of said interface:
   ```java
   public class HelloGreeter implements Greeter {
     @Override
     public String greet() {
       return "Hello";
     }
   }
   ```
   - and a `@Produces`-method for `HelloGreeter`:
   ```java
   public class GreeterProducer {
     public static final String NAME_HELLO_GREETER = "hello-greeter";
   
     @Produces
     @Singleton
     @Identifier(NAME_HELLO_GREETER)
     public Greeter helloGreeter() {
       Log.info("Hello greeter initialized");
       return new HelloGreeter();
     }
   }
   ```
   - Route `GreeterRoute` looks up the `Greeter`-instance by its name 
programmatically:
   ```java
   public class GreeterRoute extends RouteBuilder {
     public static final String HTTP_PATH = "/hello";
     public static final String ROUTE_ID = "hello";
   
     @Override
     public void configure() {
       // @formatter:off
       from(
           platformHttp(HTTP_PATH)
               .httpMethodRestrict("GET"))
           .id(ROUTE_ID)
           .setBody(exchange -> 
Objects.requireNonNull(exchange.getContext().getRegistry()
                   .findByTypeWithName(Greeter.class)
                   .get(GreeterProducer.NAME_HELLO_GREETER))
               .greet());
       // @formatter:on
     }
   }
   ```
   - When the route is triggered, the `Objects.requireNonNull(...)`, wrapping 
the registry lookup, throws a `NullPointerException`
   - Debugging the route by setting a breakpoint in the processor and calling 
`exchange.getContext().getRegistry().findByTypeWithName(Greeter.class)` shows 
that a map with a single entry is returned. The key of the entry, however is 
`null` instead of `GreeterProducer.NAME_HELLO_GREETER` (`= "hello-greeter"`)
   
   - If we replace `@Identifier` in `GreeterProducer` with `@Named`, everything 
works as expected. The following patch can be applied to realize this change:
   ```diff
   Subject: [PATCH] Switch from @Identifier to @Named
   ---
   Index: 
src/main/java/de/turing85/quarkus/camel/identifier/GreeterProducer.java
   IDEA additional info:
   Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
   <+>UTF-8
   ===================================================================
   diff --git 
a/src/main/java/de/turing85/quarkus/camel/identifier/GreeterProducer.java 
b/src/main/java/de/turing85/quarkus/camel/identifier/GreeterProducer.java
   --- 
a/src/main/java/de/turing85/quarkus/camel/identifier/GreeterProducer.java       
 (revision 54c79975f55301b0f98adba59a6aaf18ed566819)
   +++ 
b/src/main/java/de/turing85/quarkus/camel/identifier/GreeterProducer.java       
 (date 1753804135797)
   @@ -1,17 +1,17 @@
    package de.turing85.quarkus.camel.identifier;
    
    import jakarta.enterprise.inject.Produces;
   +import jakarta.inject.Named;
    import jakarta.inject.Singleton;
    
    import io.quarkus.logging.Log;
   -import io.smallrye.common.annotation.Identifier;
    
    public class GreeterProducer {
      public static final String NAME_HELLO_GREETER = "hello-greeter";
    
      @Produces
      @Singleton
   -  @Identifier(NAME_HELLO_GREETER)
   +  @Named(NAME_HELLO_GREETER)
      public Greeter helloGreeter() {
        Log.info("Hello greeter initialized");
        return new HelloGreeter();
   ```
   
   ---
   
   The sample code can be found [here 
(`github.com`)](https://github.com/turing85/quarkus-camel-identifier). The 
project holds a test for the `GretterRoute`.
   
   Furthermore, the devmode is configured to pin the artemis port on the host 
to `61616`, so if you want to play around with the setup, you can connect your 
JMS tool to `localhost:61616`, username `artemis`, password `artemis`.
   
   The issue exists at least since quarkus `3.8.4`.
   
   ---
   
   Relevant zulip chat discussions:
   - [For this issue 
(`camel.zulipchat.com`)](https://camel.zulipchat.com/#narrow/channel/257302-camel-quarkus)
   - [For issue 
#4374`camel.zulipchat.com`)](https://camel.zulipchat.com/#narrow/channel/257302-camel-quarkus/topic/Supporting.20.40Identifier.20annotation.20for.20named.20beans/with/319190010)


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to