Apache Camel Advent Calendar 5日目の記事は、サポート担当古市が担当します。 テーマは、「Camel Route から Hello World」

雛形作成と実行までを紹介しますが、ここでは 0から10まで文法やオブジェクト構造を説明しません。
0からしっかりと時間をかけて学びたいという素晴らしい志を持たれた方は、こちらのリソースをご覧ください:
初日に紹介した Japan Camel User Group(JCUG)の記事
Community Document
書籍: Camel in Action 2nd edition
それでは、雛形の作成です。一番簡単に始めるのであれば、やはり camel-jbang です。 camel init コマンドで、サンプル camel route が自動生成されます。
% camel init Hello.java
%
% cat Hello.java
import org.apache.camel.builder.RouteBuilder;
public class Hello extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:java?period=1000")
.setBody()
.simple("Hello Camel from ${routeId}")
.log("${body}");
}
}
拡張子を変更することで、ご利用されたい camel DSLのサンプルを生成します。(java, xml-io, yaml)
% camel init hello.yaml
%
% cat hello.yaml
- route:
from:
uri: timer:yaml
parameters:
period: "1000"
steps:
- setBody:
simple: Hello Camel from ${routeId}
- log: ${body}
%
% camel init hello.xml
% cat hello.xml
Hello Camel from ${routeId}
どの Camel DSL を推奨するかご質問をいただくことがありますが、これといった正解はございません。
後日ご紹介する visual camel editor kaoto は執筆時点で yamlとxml-io をサポートしています。kaoto のご利用をご検討される場合、この2つから選択していただく必要があります。
kaotoはドキュメント生成機能があるので、こちらを利用するためには yamlもしくはxml-ioの選択が必要となります。
visual editor を使わず、普段から Javaのコーディングをされている場合、java DSL をラクに感じるかもしれません。
(camel transform route コマンドを使い、他DSLへ変換することも可能です。)
ここでは、 Java DSL を使っていきます。生成した camel route は、camel runコマンドで実行可能です。
% camel run Hello.java
Running integration with the following configuration:
--camel-version=4.10.3.redhat-00025
Running integration with the following configuration:
--camel-version=4.10.3.redhat-00025
2025-07-17 09:53:18.394 INFO 2807 --- [ main] org.apache.camel.main.MainSupport : Apache Camel (JBang) 4.10.3.redhat-00025 is starting
2025-07-17 09:53:18.495 INFO 2807 --- [ main] org.apache.camel.main.MainSupport : Running Mac OS X 15.5 (aarch64)
2025-07-17 09:53:18.495 INFO 2807 --- [ main] org.apache.camel.main.MainSupport : Using Java 21.0.7 (Java HotSpot(TM) 64-Bit Server VM) with PID 2807
2025-07-17 09:53:18.495 INFO 2807 --- [ main] org.apache.camel.main.MainSupport : Started by hfuruich in /Users/hfuruich/sandbox
2025-07-17 09:53:18.557 INFO 2807 --- [ main] org.apache.camel.main.ProfileConfigurer : The application is starting with profile: dev
2025-07-17 09:53:19.036 INFO 2807 --- [ main] he.camel.cli.connector.LocalCliConnector : Camel JBang CLI enabled
2025-07-17 09:53:19.067 INFO 2807 --- [ main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 4.10.3.redhat-00025 (Hello) is starting
2025-07-17 09:53:19.159 INFO 2807 --- [ main] e.camel.impl.engine.AbstractCamelContext : Routes startup (total:1)
2025-07-17 09:53:19.159 INFO 2807 --- [ main] e.camel.impl.engine.AbstractCamelContext : Started route1 (timer://java)
2025-07-17 09:53:19.159 INFO 2807 --- [ main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 4.10.3.redhat-00025 (Hello) started in 92ms (build:0ms init:0ms start:92ms boot:670ms)
2025-07-17 09:53:20.119 INFO 2807 --- [ - timer://java] Hello.java:10 : Hello Camel from route1
2025-07-17 09:53:21.111 INFO 2807 --- [ - timer://java] Hello.java:10 : Hello Camel from route1
お手元で試された実行結果とこちらの実行結果で、使用された camel ライブラリーのバージョンが異なるかもしれません。
camel-jbang は、実行時に利用する camel ライブラリーのバージョンを指定できます。Red Hat Build of Apache Camel(RHBAC)で使われるバージョンも指定できますので、必要の際にはご活用ください。
% camel version set 4.10.3.redhat-00025
%
% camel version
JBang version: 0.127.18
Camel JBang version: 4.10.3.redhat-00020
User configuration:
camel-version = 4.10.3.redhat-00025
次に、既存のひな形コードを多少修正してみます。
camel-jbang はデフォルトで、コマンド実行ディレクトリにある application.properties を読み取ります。プロパティ値を読みとるようにコード修正してみます。
% cat application.properties
location="Tokyo" %
% cat Hello.java
import org.apache.camel.builder.RouteBuilder;
public class Hello extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:java?period=1000")
.setBody()
.simple("Hello Camel from {{location}}!")
.log("${body}");
}
}
%
実行結果はこのようになります。
% camel run Hello.java application.properties
Running integration with the following configuration:
--camel-version=4.10.3.redhat-00025
Running integration with the following configuration:
--camel-version=4.10.3.redhat-00025
2025-07-17 14:57:01.394 INFO 7918 --- [ main] org.apache.camel.main.MainSupport : Apache Camel (JBang) 4.10.3.redhat-00025 is starting
2025-07-17 14:57:01.483 INFO 7918 --- [ main] org.apache.camel.main.MainSupport : Running Mac OS X 15.5 (aarch64)
2025-07-17 14:57:01.483 INFO 7918 --- [ main] org.apache.camel.main.MainSupport : Using Java 21.0.7 (Java HotSpot(TM) 64-Bit Server VM) with PID 7918
2025-07-17 14:57:01.484 INFO 7918 --- [ main] org.apache.camel.main.MainSupport : Started by hfuruich in /Users/hfuruich/sandbox
2025-07-17 14:57:01.513 INFO 7918 --- [ main] org.apache.camel.main.BaseMainSupport : Properties location: file:application.properties
2025-07-17 14:57:01.532 INFO 7918 --- [ main] org.apache.camel.main.ProfileConfigurer : The application is starting with profile: dev
2025-07-17 14:57:01.948 INFO 7918 --- [ main] he.camel.cli.connector.LocalCliConnector : Camel JBang CLI enabled
2025-07-17 14:57:01.974 INFO 7918 --- [ main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 4.10.3.redhat-00025 (Hello) is starting
2025-07-17 14:57:02.013 INFO 7918 --- [ main] org.apache.camel.main.BaseMainSupport : Property-placeholders summary
2025-07-17 14:57:02.013 INFO 7918 --- [ main] org.apache.camel.main.BaseMainSupport : [application.properties] location = "Tokyo"
2025-07-17 14:57:02.063 INFO 7918 --- [ main] e.camel.impl.engine.AbstractCamelContext : Routes startup (total:1)
2025-07-17 14:57:02.064 INFO 7918 --- [ main] e.camel.impl.engine.AbstractCamelContext : Started route1 (timer://java)
2025-07-17 14:57:02.064 INFO 7918 --- [ main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 4.10.3.redhat-00025 (Hello) started in 89ms (build:0ms init:0ms start:89ms boot:578ms)
2025-07-17 14:57:03.026 INFO 7918 --- [ - timer://java] Hello.java:10 : Hello Camel from "Tokyo"!
2025-07-17 14:57:04.019 INFO 7918 --- [ - timer://java] Hello.java:10 : Hello Camel from "Tokyo"!
プロファイル毎に専用の application-
Timer で camel route を始めるのではなく、http request を受け付けるよう修正してみます。
% cat application.properties
location="Tokyo" %
% cat Hello.java
import org.apache.camel.builder.RouteBuilder;
public class Hello extends RouteBuilder {
@Override
public void configure() throws Exception {
from("netty-http:http://0.0.0.0:8080/foo")
.setBody()
.simple("Hello Camel from {{location}}!");
}
}
実行結果はこのようになります。
% camel run Hello.java application.properties
Running integration with the following configuration:
--camel-version=4.10.3.redhat-00025
Running integration with the following configuration:
--camel-version=4.10.3.redhat-00025
2025-07-17 15:17:14.152 INFO 9949 --- [ main] org.apache.camel.main.MainSupport : Apache Camel (JBang) 4.10.3.redhat-00025 is starting
2025-07-17 15:17:14.240 INFO 9949 --- [ main] org.apache.camel.main.MainSupport : Running Mac OS X 15.5 (aarch64)
2025-07-17 15:17:14.240 INFO 9949 --- [ main] org.apache.camel.main.MainSupport : Using Java 21.0.7 (Java HotSpot(TM) 64-Bit Server VM) with PID 9949
2025-07-17 15:17:14.240 INFO 9949 --- [ main] org.apache.camel.main.MainSupport : Started by hfuruich in /Users/hfuruich/sandbox
2025-07-17 15:17:14.270 INFO 9949 --- [ main] org.apache.camel.main.BaseMainSupport : Properties location: file:application.properties
2025-07-17 15:17:14.289 INFO 9949 --- [ main] org.apache.camel.main.ProfileConfigurer : The application is starting with profile: dev
2025-07-17 15:17:14.706 INFO 9949 --- [ main] he.camel.cli.connector.LocalCliConnector : Camel JBang CLI enabled
2025-07-17 15:17:14.805 INFO 9949 --- [ main] nt.netty.http.HttpServerBootstrapFactory : BootstrapFactory on port 8080 is using bootstrap configuration: [NettyServerBootstrapConfiguration{protocol='http', host='0.0.0.0', port=8080, broadcast=false, sendBufferSize=65536, receiveBufferSize=65536, receiveBufferSizePredictor=0, workerCount=0, bossCount=1, keepAlive=true, tcpNoDelay=true, reuseAddress=true, connectTimeout=10000, backlog=0, serverInitializerFactory=org.apache.camel.component.netty.http.HttpServerInitializerFactory@60b1ff3b, nettyServerBootstrapFactory=null, options=null, ssl=false, sslHandler=null, sslContextParameters='null', needClientAuth=false, enabledProtocols='TLSv1.2,TLSv1.3, keyStoreFile=null, trustStoreFile=null, keyStoreResource='null', trustStoreResource='null', keyStoreFormat='JKS', securityProvider='SunX509', passphrase='null', bossGroup=null, workerGroup=null, networkInterface='null', reconnect='true', reconnectInterval='10000', unixDomainSocketPath='null'}]
2025-07-17 15:17:14.807 INFO 9949 --- [ main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 4.10.3.redhat-00025 (Hello) is starting
2025-07-17 15:17:14.841 INFO 9949 --- [ main] che.camel.component.netty.NettyComponent : Creating shared NettyConsumerExecutorGroup with 29 threads
2025-07-17 15:17:14.870 INFO 9949 --- [ main] tty.SingleTCPNettyServerBootstrapFactory : ServerBootstrap binding to 0.0.0.0:8080
2025-07-17 15:17:14.894 INFO 9949 --- [ main] ache.camel.component.netty.NettyConsumer : Netty consumer bound to: 0.0.0.0:8080
2025-07-17 15:17:14.895 INFO 9949 --- [ main] org.apache.camel.main.BaseMainSupport : Property-placeholders summary
2025-07-17 15:17:14.896 INFO 9949 --- [ main] org.apache.camel.main.BaseMainSupport : [application.properties] location = "Tokyo"
2025-07-17 15:17:14.946 INFO 9949 --- [ main] e.camel.impl.engine.AbstractCamelContext : Routes startup (total:1)
2025-07-17 15:17:14.946 INFO 9949 --- [ main] e.camel.impl.engine.AbstractCamelContext : Started route1 (http://0.0.0.0:8080/foo)
2025-07-17 15:17:14.946 INFO 9949 --- [ main] e.camel.impl.engine.AbstractCamelContext : Apache Camel 4.10.3.redhat-00025 (Hello) started in 139ms (build:0ms init:0ms start:139ms boot:652ms)
別ターミナルから httpリクエストを送信します。
% curl http://0.0.0.0:8080/foo Hello Camel from "Tokyo"!% %
最後に、ちょっとしたネタを。 stream:in,out を使いつつ、スクリプト実行に対応するため、先頭行にコメントを追記しています。
% cat application.properties
location="Tokyo"
% cat Hello.java
///usr/bin/env jbang --quiet camel@apache/camel script "$0" --properties=application.properties "$@" ; exit $?
import org.apache.camel.builder.RouteBuilder;
public class Hello extends RouteBuilder {
@Override
public void configure() throws Exception {
from("stream:in")
.setBody()
.simple("Hello ${body.toUpperCase()} from {{location}}!")
.to("stream:out");
}
}
Hello.java に実行権限を付与し、実行してみます。echoコマンドを使い、スクリプトに文字列を渡しています。
% chmod +x Hello.java % % echo "Camel" | ./Hello.java Hello CAMEL from "Tokyo"! %
普段何気なく作業している雑務を、camel routeを使ったスクリプトで効率化しても面白いかもしれません。
ぜひ、楽しみながら camel tool box を色々と試してみてください。
今回紹介した camel jbang の類似サンプルは、コミュニティーページでも紹介されています。他にも沢山の機能がありますので、ぜひこちらもご覧ください。
Quarkus runtime や Spring Boot runtime用ひな形プロジェクトを使い試されたい方は、それぞれオフィシャルドキュメントに生成方法が記載されています。こちらをご覧ください。(camel-jbang を使い、maven project を生成する機能もあります。)
明日の advent calendar 6日目では、「Testing Camel」 についてご紹介します。
アドベントカレンダーの一覧はこちらです。
qiita.com