Spring Boot Camel Route - 从休息端点获取数据

标签 spring spring-boot apache-camel spring-camel

我想在 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/

相关文章:

java - 需要帮助为返回 Flux 流作为输出的函数编写 junit 测试

java - 在 Apache Camel 中使用动态路由器与 DSL 选择的含义是什么?

spring - Web 应用程序异常 : javax. ws.rs.NotSupportedException:HTTP 415 不支持的媒体类型

css - Thymeleaf + Spring-Boot - 为什么我不能访问静态资源?

postgresql - Heroku Postgres 升级后 Flyway 无法连接到数据库

spring - Keycloak 与 Spring boot - 如何应用资源范围

apache-camel - 使用 Camel 压缩和密码保护文件

java - Jetty - 如何使 Camel 应用程序将事件记录到 hawtio 日志选项卡

java - 用户 'root' @'localhost' Spring Boot MySql 访问被拒绝

spring - Java Spring Eclipse Tomcat - RestTemplate,使用 HTTPS API