Spring Boot Camel Route - 从 rest 端点获取数据
Spring Boot Camel Route - get data from rest endpoint
我想在 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");
试试这个不用 context.start() ....
CamelContext camelContext = new DefaultCamelContext();
ProducerTemplate template = camelContext.createProducerTemplate();
Exchange exchange = template.send("http://localhost:8080/getAllUsers", new Processor() {
public void process(Exchange exchange) throws Exception {}
});
Message out = exchange.getOut();
http 组件是基于流的,因此您可以要求 Camel 以字符串形式给您响应。
String s = exchange.getMessage().getBody(String.class);
在这些链接中查看更多内容
我会用你的第二个例子:
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 (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");
试试这个不用 context.start() ....
CamelContext camelContext = new DefaultCamelContext();
ProducerTemplate template = camelContext.createProducerTemplate();
Exchange exchange = template.send("http://localhost:8080/getAllUsers", new Processor() {
public void process(Exchange exchange) throws Exception {}
});
Message out = exchange.getOut();
http 组件是基于流的,因此您可以要求 Camel 以字符串形式给您响应。
String s = exchange.getMessage().getBody(String.class);
在这些链接中查看更多内容
我会用你的第二个例子:
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");
这会触发一次。