17
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

spring boot と camel を連携してcamelコンポーネントを使いたい放題に

Last updated at Posted at 2016-08-27

camelと連携することで大量のcamelコンポーネントが使いたい放題になる。

camelコンポーネント一覧
http://camel.apache.org/components.html

spring bootアプリでcamelを動かす

spring bootアプリでcamelを簡単に動かせるようになっていた。
spring bootアプリのpomにcamelを追記するだけ

pom.xml
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.17.3</version>
        </dependency>

他は触らなくて良い。
全体のコードは一番下に記載。
起動部分も変わらない。camelの記述は必要無し。

AppMain.java
package helloboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppMain {

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

}

ルートを追加

@Componentを書いてRouteBuilderをextendsするだけ。
これだけでcamelのrouteが起動する。

HelloRoute.java
package helloboot.route;

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

@Component
public class HelloRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct://hello")
                .setBody(constant("hello"));
    }
}

ルートから色々なcamelコンポーネントは呼べるので、使いたいコンポーネントはここに記載していく。

camelのルートを呼ぶ

起動したルートにrestコントローラから呼び出してみる

package helloboot.controller;

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

@RestController
public class HelloRestController {

    @Autowired
    private CamelContext camelContext;

    @RequestMapping("/")
    public String hello() {

        // camelで流通するデータの作成
        Exchange exchange = new DefaultExchange(camelContext);

        // routeにデータを送信
        ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
        producerTemplate.send("direct://hello", exchange);

        // camelのrouteが例外で終了したかどうかの確認
        if (exchange.getException() != null) {
            return "NG";
        }
        
        return exchange.getIn().getBody(String.class);
    }
}

起動

mvn spring-boot:run
hello.png

pom.xml

pom.xml
<?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>

    <groupId>suburi</groupId>
    <artifactId>HelloSpringBoot</artifactId>
    <version>1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.17.3</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?