使用JBang运行Camel应用时无法暴露Jetty组件路由的问题
Camel CLI运行Jetty路由无启动问题排查
用jbang RestCamelJbang.java执行代码时,能正常暴露8080端口的REST端点;但用camel run FirstCamel.java运行含Jetty组件的路由时,控制台无报错却显示0路由启动,http://localhost:8081/hello端点无响应。而采用REST DSL的代码通过camel run WelcomeRoute.java可正常运行,请问是否遗漏配置或需要额外服务器?
成功运行的代码(jbang执行)
///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(); } }
无法正常运行的代码(camel run执行)
///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}"); } }
控制台输出
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)
正常运行的REST DSL代码(camel run执行)
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}"); } }
问题原因与解决方案
核心原因
无法运行的代码缺少Jetty组件依赖声明。Camel CLI的camel run命令默认不会自动下载所有组件依赖,REST DSL代码能运行是因为Camel自动引入了camel-rest相关依赖(控制台日志也显示下载了camel-rest),但Jetty作为独立组件需要显式声明。
解决方案
修改FirstCamel.java,添加Jetty及必要依赖的声明:
///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.*; 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}"); } }
重新执行camel run FirstCamel.java,此时Camel会自动下载Jetty组件依赖,路由即可正常启动,http://localhost:8081/hello端点也能正常响应。
补充说明
- 成功运行的jbang代码中已经显式声明了所有依赖,所以能正常加载Jetty组件;
- 也可以通过命令行参数指定依赖,无需修改代码:
camel run FirstCamel.java --dependency org.apache.camel:camel-jetty:3.20.1
内容的提问来源于stack exchange,提问作者Tim
相关产品推荐
相关产品推荐

