Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
15
Help us understand the problem. What is going on with this article?
@daikuro

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

More than 3 years have passed since last update.

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>
15
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
daikuro

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
15
Help us understand the problem. What is going on with this article?