Apache camel 在Camel 2.15中配置Jetty组件

Apache camel 在Camel 2.15中配置Jetty组件,apache-camel,jetty,Apache Camel,Jetty,我正试着开始用骆驼码头。 我已将依赖项添加到我的pom中: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jetty</artifactId> <version>2.15.5</version> </dependency> 当我尝试启动我的服务时,该

我正试着开始用骆驼码头。 我已将依赖项添加到我的pom中:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jetty</artifactId>
        <version>2.15.5</version>
    </dependency>
当我尝试启动我的服务时,该服务具有一个定义为以下端点的路由:

jetty:http://0.0.0.0:9000/httpInput
我得到一个例外:

java.lang.NullPointerException: null
at org.apache.camel.component.jetty8.JettyHttpComponent8.createConnectorJettyInternal(JettyHttpComponent8.java:48)
at org.apache.camel.component.jetty.JettyHttpComponent.createConnector(JettyHttpComponent.java:585)
at org.apache.camel.component.jetty.JettyHttpComponent.getSocketConnector(JettyHttpComponent.java:527)
at org.apache.camel.component.jetty.JettyHttpComponent.getConnector(JettyHttpComponent.java:517)
at org.apache.camel.component.jetty.JettyHttpComponent.connect(JettyHttpComponent.java:320)
at org.apache.camel.component.http.HttpEndpoint.connect(HttpEndpoint.java:185)
at org.apache.camel.component.http.HttpConsumer.doStart(HttpConsumer.java:53)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.startService(DefaultCamelContext.java:2885)
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:3179)
at org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:3115)
at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3045)
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:2813)
at org.apache.camel.impl.DefaultCamelContext.startAllRoutes(DefaultCamelContext.java:865)
关于如何设置码头组件的文档充其量是缺乏的。我发现了一个邮件列表条目,其中说JettyHttpComponent从Camel 2.15开始就被抽象化了,现在该组件必须使用JettyHttpComponent8或9进行配置

在我的例子中,我使用的是Camel 2.15.5,而JettyHttpComponent9在类路径中不可用,使用8会产生上述异常。
我还发现相关讨论中没有关于如何实际使用该组件的信息。

这通常不是CamelContext的初始化/启动方式。请考虑使用原型来启动,然后添加JETTY Maven依赖项,并查看是否可以复制错误。


骆驼原型可以在这里找到:

这通常不是初始化/启动骆驼上下文的方式。请考虑使用原型来启动,然后添加JETTY Maven依赖项,并查看是否可以复制错误。


驼峰原型可以在这里找到:

要在spring之外启动驼峰上下文,您需要创建一个连续的线程来保持驼峰的活力,如下所述:

别担心,我下面有一些代码将在localhost:8081上为您设置jetty:

pom.xml

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jetty</artifactId>
        <version>2.16.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.16.1</version>
    </dependency>


import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.MainSupport;

import java.util.Date;

/**
 * Created by mkbrv on 22/06/16.
 */
public class CamelJetty {

        private Main main;

        public static void main(String[] args) throws Exception {
            CamelJetty example = new CamelJetty();
            example.boot();
        }

        public void boot() throws Exception {
            // create a Main instance
            main = new Main();
            // bind MyBean into the registry
            main.bind("foo", new MyBean());
            // add routes
            main.addRouteBuilder(new MyJettyRouteBuilder());
            // add event listener
            main.addMainListener(new Events());

            // run until you terminate the JVM
            System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
            main.run();
        }

        private static class MyJettyRouteBuilder extends RouteBuilder {
            @Override
            public void configure() throws Exception {
                from("jetty:http://localhost:8081")
                        .process(exchange -> {
                            System.out.println("Invoked timer at " + new Date());
                            exchange.getOut().setBody("Hi, this is Camel!");
                        })
                        .bean("foo");
            }
        }

        public static class MyBean {
            public void callMe() {
                System.out.println("MyBean.callMe method has been called");
            }
        }

        public static class Events extends MainListenerSupport {

            @Override
            public void afterStart(MainSupport main) {
                System.out.println("MainExample with Camel is now started!");
            }

            @Override
            public void beforeStop(MainSupport main) {
                System.out.println("MainExample with Camel is now being stopped!");
            }
        }
    }

org.apache.camel
你应该会看到一条欢迎信息。

享受进一步调整的乐趣

要在spring之外启动camel上下文,您需要创建一个连续的线程来保持camel活动,如下所述:

别担心,我下面有一些代码将在localhost:8081上为您设置jetty:

pom.xml

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jetty</artifactId>
        <version>2.16.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.16.1</version>
    </dependency>


import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.MainSupport;

import java.util.Date;

/**
 * Created by mkbrv on 22/06/16.
 */
public class CamelJetty {

        private Main main;

        public static void main(String[] args) throws Exception {
            CamelJetty example = new CamelJetty();
            example.boot();
        }

        public void boot() throws Exception {
            // create a Main instance
            main = new Main();
            // bind MyBean into the registry
            main.bind("foo", new MyBean());
            // add routes
            main.addRouteBuilder(new MyJettyRouteBuilder());
            // add event listener
            main.addMainListener(new Events());

            // run until you terminate the JVM
            System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
            main.run();
        }

        private static class MyJettyRouteBuilder extends RouteBuilder {
            @Override
            public void configure() throws Exception {
                from("jetty:http://localhost:8081")
                        .process(exchange -> {
                            System.out.println("Invoked timer at " + new Date());
                            exchange.getOut().setBody("Hi, this is Camel!");
                        })
                        .bean("foo");
            }
        }

        public static class MyBean {
            public void callMe() {
                System.out.println("MyBean.callMe method has been called");
            }
        }

        public static class Events extends MainListenerSupport {

            @Override
            public void afterStart(MainSupport main) {
                System.out.println("MainExample with Camel is now started!");
            }

            @Override
            public void beforeStop(MainSupport main) {
                System.out.println("MainExample with Camel is now being stopped!");
            }
        }
    }

org.apache.camel
你应该会看到一条欢迎信息。
享受进一步调整的乐趣