Spring Boot Camel 入门

x33g5p2x  于2022-09-29 转载在 Spring  
字(5.0k)|赞(0)|评价(0)|浏览(443)

在本教程中,您将在几分钟内学习如何在 Spring Boot 应用程序 中引导 Camel

首先,让我们从一个基本的 Web 模板创建我们的项目:

spring init -dweb spring-camel

好的,我们已经将 web 启动器作为我们工具包的一部分,我们现在将添加 Apache Camel 依赖项,以便我们可以开始触发路由。在你的 pom.xml 中包含Camel依赖项,如下所示:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	
   <modelVersion>4.0.0</modelVersion>
    	
   <parent>
       		
      <groupId>org.springframework.boot</groupId>
       		
      <artifactId>spring-boot-starter-parent</artifactId>
       		
      <version>2.1.1.RELEASE</version>
       		
      <relativePath/>
         	
   </parent>
    	
   <groupId>com.example</groupId>
    	
   <artifactId>spring-camel</artifactId>
    	
   <version>0.0.1-SNAPSHOT</version>
    	
   <packaging>jar</packaging>
    	
   <name>demo</name>
    	
   <description>Demo project for Spring Boot</description>
     	
   <properties>
       		
      <java.version>1.8</java.version>
       	
   </properties>
     	
   <dependencies>
       		
      <dependency>
          			
         <groupId>org.springframework.boot</groupId>
          			
         <artifactId>spring-boot-starter-web</artifactId>
          		
      </dependency>
        		
      <dependency>
          			
         <groupId>org.springframework.boot</groupId>
          			
         <artifactId>spring-boot-starter-test</artifactId>
          			
         <scope>test</scope>
          		
      </dependency>
                       
      <dependency>
          			
         <groupId>org.apache.camel</groupId>
          			
         <artifactId>camel-spring-boot-starter</artifactId>
          			
         <version>2.17.0</version>
                        
      </dependency>
         	
   </dependencies>
     	
   <build>
       		
      <plugins>
          			
         <plugin>
             				
            <groupId>org.springframework.boot</groupId>
             				
            <artifactId>spring-boot-maven-plugin</artifactId>
             			
         </plugin>
          		
      </plugins>
       	
   </build>
      
</project>

为了触发Camel,我们需要在我们的应用程序中注入 CamelContext。为了让它变得非常简单,我们将把它注入到主类中:

package com.example.springcamel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.apache.camel.CamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.camel.builder.RouteBuilder;

@SpringBootApplication
public class DemoApplication {
  @Autowired CamelContext camelContext;

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @Bean
  public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {
      camelContext.addRoutes(
          new RouteBuilder() {
            public void configure() {
              from("file:/home/frank/test/in?noop=true").to("file:/home/frank/test/out");
            }
          });
    };
  }
}

如您所见,我们使用 RouteBuilder 类的流畅语法添加了一条新的 Camel 路由。这个简单的Camel路线将文件从源文件夹复制到目标文件夹。确保您的机器上有 file 组件的路径。

现在以任何可能的方式运行应用程序,例如:

java -jar target/spring-camel-0.0.1-SNAPSHOT.jar

您将从输出中看到 Camel 路由已被引导并执行:

2018-12-11 10:44:31.248  INFO 12432 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor' 2018-12-11 10:44:31.249  INFO 12432 --- [       Thread-3] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.17.0 (CamelContext: camel-1) is shutting down 2018-12-11 10:44:31.250  INFO 12432 --- [       Thread-3] o.a.camel.impl.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 300 seconds) 2018-12-11 10:44:31.254  INFO 12432 --- [ - ShutdownTask] o.a.camel.impl.DefaultShutdownStrategy   : Route: route1 shutdown complete, was consuming from: Endpoint[file:///home/frank/test/in?noop=true] 2018-12-11 10:44:31.254  INFO 12432 --- [       Thread-3] o.a.camel.impl.DefaultShutdownStrategy   : Graceful shutdown of 1 routes completed in 0 seconds

作为概念证明,请检查目标文件夹以查看文件是否已从源复制。

从 Spring REST 服务启动Camel Route

如果您想根据请求启动 Camel Route,最简单的方法是使用 REST 服务。只需插入一个:

package com.example.springcamel;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CamelController {
  @Autowired CamelContext camelContext;
  @Autowired ProducerTemplate producerTemplate;

  @RequestMapping(value = "/camel")
  public void startCamel() {
    System.out.println("Received call!");
    producerTemplate.sendBody("direct:demoRoute", "Calling via Spring Boot Rest Controller");
  }
}

现在我们需要该路由的消费者,所以只需添加一个:

package com.example.springcamel;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class CamelRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("direct:demoRoute").log("Got message: ${body}");
  }
}

现在从浏览器或 shell 访问“/camel”端点:

curl http://localhost:8080/camel

您将看到在 Spring Boot 控制台中打印了日志消息:

Camel body: Calling via Spring Boot Rest Controller

您可以在以下更高级的教程中继续学习 Camel 和 Spring Boot:Camel 与 Spring Boot 示例

相关文章

微信公众号

最新文章

更多