Search code examples
javaspring-bootapache-camel-3

Apache Camel Endpoint-dsl custom component names


I am trying to use the Apache Camel 3 Endpoint DSL. This is my code so far. It works OK. It is a test, so it is really simple. It reads from a directory and log the file content.

@Component
public class TestRoute extends EndpointRouteBuilder{
    final String componentName = "file";
    final String path = "/in/";
    
    @Override
    public void configure() throws Exception {      
        FileEndpointBuilder srcFileEndpoint = file(componentName, path);        
        from( srcFileEndpoint ).log(LoggingLevel.INFO, "body, ${body}");
    }//configure    
}//TestRoute

But when I try to change the name of the component. For instance final String componentName = "myCustomFileComponent";

I get the following error in the console

Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: myCustomFileComponent:///in/ due to: No component found with scheme: myCustomFileComponent

From here, I understand I can provide custom names for the endpoints, myWMQ and myAMQ in the example. For instance the route reads from a directory and writes to another, and I would like each component to be configured in different ways. But if I specify a custom componentName, I get the error. Because it does not find the custonName component.

I do not know if it is relevant, but the code is inside a Spring Boot project

  • Apache Camel version 3.4.0 Spring Boot version 2.3.1

Solution

  • It seems you are missing the point. That section explains if "multiple Camel components of the same type registered with different names". Those are camel components which are registered not the the uri of the endpoints.

    You cannot have dynamic route endpoints in apache camel as far as I know since the routes are created at start and those endpoint won't change at runtime.

    If you need to have a route that reads from different endpoint what you can do is that having route for them and route those to same endpoint like:

    public class TestRoute extends RouteBuilder {
    
        @Override
        public void configure() {
    
            from(component1)
                .to("direct:myroute")
    
            from(component2)
                .to("direct:myroute")
    
            from(component3)
                .to("direct:myroute")
    
            from("direct:myroute")
                .log(LoggingLevel.INFO, "body, ${body}");
        }
    
    }