LoginSignup
33
32

More than 5 years have passed since last update.

Spring boot を使った超高速開発 - Apache Camel

Posted at

目標

瞬時に実装完了するけど、こんな感じ

スクリーンショット 2015-04-23 21.51.58.png

プロジェクトの作成(今回はIntelliJ)

スタート画面から New Project

スクリーンショット 2015-04-16 22.03.58.png

Mavenを選択

スクリーンショット 2015-04-23 21.56.12.png

スクリーンショット 2015-04-23 21.57.07.png

スクリーンショット 2015-04-23 21.57.24.png

最初の画面

スクリーンショット 2015-04-23 21.59.10.png

ライブラリの追加

JDK8と下記のライブラリを使う
camel-core, camel-spring-boot, spring-boot-starter-web

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>hello</groupId>
    <artifactId>HelloCamelBoot</artifactId>
    <version>1</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <camel-ver>2.15.1</camel-ver>
        <spring-boot>1.2.2.RELEASE</spring-boot>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel-ver}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot</artifactId>
            <version>${camel-ver}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot}</version>
        </dependency>

    </dependencies>

</project>

起動用mainのクラスを作成

javaのいつものmainなやつ
なんか見かけない実装が2つ
@SpringBootApplication
SpringApplication

hello.HelloApp
package hello;

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

/**
 */
@SpringBootApplication
public class HelloApp {

    public static void main(String[] args) throws Exception {
        System.out.println("-- start! --");
        new SpringApplication(HelloApp.class).run();
    }

}

起動してみる

スクリーンショット 2015-04-24 6.40.57.png

動きっぱなしになった。main()に2行いれただけで、Tomcatが起動したっぽい。
左の赤い四角で止まる。

スクリーンショット 2015-04-24 6.42.06.png

Camelのルートを作成

今回はこれで実装終了。
Spring boot + Camel の実装はコードが少なくて助かる。

スクリーンショット 2015-04-24 6.44.29.png

@Componentが重要

hello.route.TimerRoute
package hello.route;

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

import java.util.Date;

@Component
public class TimerRoute extends RouteBuilder{

    @Override
    public void configure() throws Exception {
        from("timer:test?period=1s")
                .process(printDate);
    }

    /** 日時を出力する */
    private Processor printDate = new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            System.out.println(new Date());
        }
    };
}

そして起動・停止

1秒毎に標準出力。
そして、止めたら Graceful shutdown (処理中データがあったら処理完了まで待つ)機能も働いている。

スクリーンショット 2015-04-24 6.45.54.png

ちょっと修正して

下記を追加

pom.xml
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-quartz2</artifactId>
            <version>${camel-ver}</version>
        </dependency>

下記の行を修正・追加

hello.route.TimerRoute
        // from("timer:test?period=1s")
        from("quartz2://myGroup/myTimerName?cron=*+*+10-18+?+*+MON-FRI")  

これで起動!

結果はほぼ同じ。1秒間に1回の間隔で日時が出力される。
だけど、月曜から金曜の10時から18時しか出力されないから注意して。
日曜日にこのサンプル動かしたけど動かないという文句はなし!

33
32
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
33
32