Search code examples
apache-camel

Jbang camel app not able to expose jetty component based route


When using the jbang cli jbang RestCamelJbang.java below code expose the REST endpoint successfully. Able to access the endpint at 8080

///usr/bin/env jbang "$0" "$0" : exit $?
// camel-k: language=java

//DEPS org.apache.camel:camel-bom:3.20.1@pom
//DEPS org.apache.camel:camel-core
//DEPS org.apache.camel:camel-main
//DEPS org.apache.camel:camel-jetty
//DEPS org.slf4j:slf4j-nop:2.0.6
//DEPS org.slf4j:slf4j-api:2.0.6

import org.apache.camel.*;
import org.apache.camel.builder.*;
import org.apache.camel.main.*;
import org.apache.camel.spi.*;

import static java.lang.System.*;

public class RestCamelJbang{

 public static void main(String ... args) throws Exception{
        out.println("Starting camel route...");
        setProperty("org.slf4j.simpleLogger.logFile", "System.out");
        Main main = new Main();

        main.configure().addRoutesBuilder(new RouteBuilder(){
            public void configure() throws Exception{
   out.println("Camel configuration started...");
     from("jetty:http://localhost:8080/hello")
       .transform().simple("First Message of Camel"); 
           }
         });
        main.run();
    }
}
  • When using the Jbang Camel app camel run FirstCamel.java command with the jetty component, there are NO exception in the console but states 0 route started. The endpoint http://localhost:8080/hello there is no response.

Am I missing something here, do we need some sort of server to run it?

///usr/bin/env jbang "$0" "$0" : exit $?
// camel-k: language=java

import org.apache.camel.*;
import org.apache.camel.builder.*;
import org.apache.camel.main.*;
import org.apache.camel.spi.*;

public class FirstCamel extends RouteBuilder{

   @Override
   public void configure() throws Exception{
      from("jetty:http://localhost:8081/hello")
       .transform().simple("First Message of Camel")
       .log("${body}");
   } 
}
  • Console ouput
2023-02-13 21:10:22.056  INFO 6884 --- [           main] org.apache.camel.main.MainSupport        : Apache Camel (JBang) 3.20.1 is starting
2023-02-13 21:10:22.511  INFO 6884 --- [           main] org.apache.camel.main.MainSupport        : Using Java 17.0.1 with PID 6884. Started by thirumurthi in C:\thiru\learn\camel\camel_lessons\lesson
2023-02-13 21:10:22.543  INFO 6884 --- [           main] he.camel.cli.connector.LocalCliConnector : Camel CLI enabled (local)
2023-02-13 21:10:24.180  INFO 6884 --- [           main] .main.download.MavenDependencyDownloader : Downloaded: org.apache.camel:camel-rest:3.20.1 (took: 1s55ms)
2023-02-13 21:10:24.471  INFO 6884 --- [           main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 3.20.1 (CamelJBang) is starting
2023-02-13 21:10:24.893  INFO 6884 --- [           main] e.camel.impl.engine.AbstractCamelContext : Routes startup (started:0)
2023-02-13 21:10:24.893  INFO 6884 --- [           main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 3.20.1 (CamelJBang) started in 950ms (build:289ms init:241ms start:420ms JVM-uptime:5s)
  • Below code works when I use the rest DSL with camel run WelcomeRoute.java.
import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;

public class WelcomeRoute extends RouteBuilder {

   @Override
   public void configure() throws Exception {

          restConfiguration().bindingMode("auto");

                rest("/api")
                        .get("/demo/{info}")
                        .to("log:info")
                        .to("direct:msg");
                from("direct:msg")
                                .transform().simple("msg received - ${header.info}");
   }
}

Solution

  • When using Camel JBang run then its for running Camel routes (not Java Main classes). So the first code is not valid.

    Camel JBang detects Java source that are RouteBuilder so your class should extend this class, like the 2nd and 3rd code examples.

    There has been some troubles with Java 11 and older Camel releases to use .java source with java imports. That works with Java 17, so upgrade if you can. Otherwise you may need to use FQN classnames instead of imports.