我正在尝试与Camel一起使用Jetty。 我已将依赖项添加到我的pom中:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>2.15.5</version>
</dependency>
我的CamelContext初始化如下:
public void startCamelContext() throws Exception {
CamelContext camelContext = new DefaultCamelContext();
camelContext.addComponent("jetty", new JettyHttpComponent8());
camelContext.start();
}
当我尝试启动我的服务时,该服务的路由端点定义为:
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)
关于如何设置Jetty组件的文档充其量是缺乏的。我找到了一个邮件列表条目,据说JettyHttpComponent自Camel 2.15开始变得抽象,现在该组件必须使用JettyHttpComponent8或9进行配置。link
在我的情况下,我使用Camel 2.15.5并且JettyHttpComponent9在类路径中不可用,并且使用8给出了上述异常。 我还发现了相关的讨论here,但没有关于如何实际使用该组件的信息。
答案 0 :(得分:0)
通常不是CamelContext的初始化/启动方式。请考虑使用原型开始,然后添加Jetty Maven依赖项并查看是否可以重现错误。
可在此处找到骆驼原型:http://camel.apache.org/camel-maven-archetypes.html
答案 1 :(得分:0)
要在春天以外开始骆驼的上下文,你需要创建一个连续的线程来保持骆驼活着,如下所述:http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html
别担心,我有一些代码会在localhost上设置一个码头:8081给你:
的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!");
}
}
}
接下来只需转到http://localhost:8081,您就会看到欢迎讯息。 玩得更开心。