我想在 Spring Boot (2.1.1) 项目中创建驼峰路由,以从某些(其余)端点 ( http://localhost:8080/getAllUsers ) 获取数据并将该数据发送到 activeMq。
我尝试使用计时器数据在 activeMq 上发送它并使用它,它正在工作。但我在从端点收集数据时遇到问题。
我尝试了很多方法,但没有成功。这是我尝试过的。
在此示例中,我没有将数据发送到 ActiveMq,我只是想查看响应...
public void createNewRoute() {
CamelContext context = new DefaultCamelContext();
try {
ProducerTemplate template = context.createProducerTemplate();
context.start();
Exchange exchange = template.request("http://localhost:8080/getAllUsers",
new Processor() {
public void process(Exchange exchange) throws Exception {
}
});
if (null != exchange) {
Message out = exchange.getOut();
int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
System.out.println("Response: " + String.valueOf(responseCode));
}
Thread.sleep(1000 * 3);
context.stop();
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
System.out.println("DONE!!");
}
另一条路线:
from("servlet://localhost:8080/getAllUsers").to("activemq://all-users");
还有一个:
rest("//localhost:8080/getAllUsers")
.get().consumes("application/json")
.to("activemq://all-users");
最佳答案
我将使用你的第二个例子:
from("timer://test?repeatCount=1").routeId("newRoute")
.streamCaching()
.process(exchange -> exchange.getIn()
.setBody(exchange.getIn()
.getBody()))
.marshal()
.json(JsonLibrary.Jackson)
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://localhost:8080/getAllUsers")
.log(LoggingLevel.INFO, "This is my body: ${body}")
.to("activemq:queue://new-queue");
这将触发一次。
关于Spring Boot Camel Route - 从休息端点获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55299728/