gpt4 book ai didi

java - Apache Camel : consume a rest service with jetty

转载 作者:行者123 更新时间:2023-12-02 00:16:37 25 4
gpt4 key购买 nike

我想在 http://localhost:8080/ 享受休息服务与 apache-camel 使用 jetty。但这段代码不会产生任何对 API 的请求。我是 apache-camel 的初学者,我想用来编排不同的微服务。

代码:

package example;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;



public class ejemplo {

public static void main(String[] args) throws Exception {



CamelContext context = new DefaultCamelContext();
context.setTracing(true);
context.addRoutes(new RouteBuilder(){

@Override
public void configure() throws Exception {

from("direct:start")
.log("Http Route started")
.setHeader(Exchange.HTTP_METHOD,simple("GET"))
.setHeader(Exchange.CONTENT_TYPE,simple("application/json"))
.to("jetty:http://0.0.0.0:8080/")
.process(new Processor(){

public void process(Exchange exchange) throws Exception {
System.out.println("I am a process....");
String msg = exchange.getIn().getBody().toString();
System.out.println(msg);

}

});

}

});

context.start();

}

}

日志:

sep 24, 2019 7:57:05 PM org.apache.camel.impl.DefaultCamelContext start
INFORMACIÓN: Apache Camel 2.17.1 (CamelContext: camel-1) is starting
sep 24, 2019 7:57:05 PM org.apache.camel.impl.DefaultCamelContext doStartCamel
INFORMACIÓN: Tracing is enabled on CamelContext: camel-1
sep 24, 2019 7:57:05 PM org.apache.camel.management.ManagedManagementStrategy doStart
INFORMACIÓN: JMX is enabled
sep 24, 2019 7:57:05 PM org.apache.camel.impl.converter.DefaultTypeConverter doStart
INFORMACIÓN: Loaded 208 type converters
sep 24, 2019 7:57:05 PM org.apache.camel.impl.DefaultRuntimeEndpointRegistry doStart
INFORMACIÓN: Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
sep 24, 2019 7:57:05 PM org.apache.camel.impl.DefaultCamelContext doStartCamel
INFORMACIÓN: AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
sep 24, 2019 7:57:05 PM org.apache.camel.impl.DefaultCamelContext doStartCamel
INFORMACIÓN: StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
sep 24, 2019 7:57:05 PM org.eclipse.jetty.util.log.Log initialized
INFORMACIÓN: Logging initialized @1372ms
sep 24, 2019 7:57:06 PM org.apache.camel.impl.DefaultCamelContext doStartOrResumeRouteConsumers
INFORMACIÓN: Route: route1 started and consuming from: Endpoint[direct://httpRouter]
sep 24, 2019 7:57:06 PM org.apache.camel.impl.DefaultCamelContext start
INFORMACIÓN: Total 1 routes, of which 1 are started.
sep 24, 2019 7:57:06 PM org.apache.camel.impl.DefaultCamelContext start
INFORMACIÓN: Apache Camel 2.17.1 (CamelContext: camel-1) started in 1.466 seconds

此输出不会在 localhost:8080 中产生 API 的任何响应,但我认为该路由是正确的。我想知道是否还有其他方法可以使用 apache-camel 来使用 API REST 的 REST 服务。

最佳答案

请注意,start 只是一个名称,direct 组件允许您从其他路由同步调用您的路由,看起来您不只是这样做

要检查路由是否正确,请替换从端点到 timer 中的 direct,例如from("timer://foo?fixedRate=true&period=10000"),有关更多详细信息,请参阅 camel docs

关于java - Apache Camel : consume a rest service with jetty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58086603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com