Java Apache Camel - 创建一个 CamelContext 对象

Java Apache Camel - Create a CamelContext object

“入门”部分中的文档说明如下:
"1. 创建一个 CamelContext 对象。"
https://camel.apache.org/manual/book-getting-started.html#BookGettingStarted-CamelContext

我这样试过:

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.model.dataformat.JsonLibrary;

public class CamelDemoMainClass {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new CamelContext();
    }
}

但是 VS Code 中的语言服务器显示如下:
“无法实例化 CamelContext 类型”

而且我也尝试使用 DefaultCamelContext 而不是 CamelContext 因为我在其他一些项目中看到过它,但结果相同。

有多种创建 CamelContext 的方法,具体取决于您希望如何启动 Camel(独立、Spring Boot、Quarkus、CDI...)。

假设您想以独立模式启动 Camel,您可以使用 DefaultCamelContext.

创建它

就像下一个基本示例:

public final class CamelBasic {

    public static void main(String[] args) throws Exception {
        // create a CamelContext
        try (CamelContext camel = new DefaultCamelContext()) {

            // add routes which can be inlined as anonymous inner class
            // (to keep all code in a single java file for this basic example)
            camel.addRoutes(createBasicRoute());

            // start is not blocking
            camel.start();

            // so run for 10 seconds
            Thread.sleep(10_000);
        }
    }

    static RouteBuilder createBasicRoute() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("timer:foo")
                        .log("Hello Camel");
            }
        };
    }
}

来源:https://github.com/apache/camel-examples/tree/main/examples/basic