LoginSignup
3
0

More than 3 years have passed since last update.

Apache Camelの流量制御(Throttler)パターンを使ってみる

Last updated at Posted at 2018-09-22

Apache Camelの流量制御(Throttler)パターンを使ってみる

Throttlerパターンを使用すると、送信先のシステム(エンドポイント)に対してのデータの流量を制御することができます。例えば、1秒間に500個のデータまでしか送信しないなど。
それにより相手側システムが過負荷にならないように制御することができます。

Throttlerパターンを試してみた

まずはSimpleDataSetコンポーネントで大量のテストデータを準備します。

・size: テストデータの数。
・reportCount: reportCountの数だけ生成後にログを出力する。例えば、100にすると、100個のデータを生成するたびにログが出力されます。

今回はデータの内容は関係ないので設定しませんでしたが、header、bodyのデータも指定することができます。例えば、ファイルにデータを準備しbodyにセットするなど。

    <bean id="testDataSet"
        class="org.apache.camel.component.dataset.SimpleDataSet">
        <property name="size" value="10000" />
        <property name="reportCount" value="10000" />
    </bean>

次に、データを処理するrouteを作成します。

"dataset:testDataSet?produceDelay=-1"でデータを受信します。
"produceDelay=-1"としないと1件のデータごとに3ms(デフォルト)のwaitが発生します。

"<throttle timePeriodMillis="1000">"で流量制限する時間を指定しています。1000を設定することで1秒(1000ms)単位に制限することになります。
"1000"で単位時間で処理するデータ件数を指定します。
今回の設定では1秒(1000ms)単位に1000個のデータを処理することになります。

    <camelContext
        xmlns="http://camel.apache.org/schema/spring">
        <route id="test">
            <from uri="dataset:testDataSet?produceDelay=-1" />
            <split parallelProcessing="true">
                <simple>${header.CamelDataSetIndex}</simple>
                <throttle timePeriodMillis="1000">
                    <constant>1000</constant>
                    <to uri="mock:result"/>
                    <to
                        uri="log:sample?level=INFO&amp;groupInterval=1000&amp;groupDelay=200&amp;groupActiveOnly=false" />
                </throttle>
            </split>
        </route>
    </camelContext>

実行すると以下のように出力されます。
1秒ごとに"Received: 1000 new messages"のログが出力され、想定どおりに1秒(1000ms)単位に1000個のデータを処理できていることがわかります。
これだけで実現できるなんて簡単すぎますね。

[2018-09-22 09:23:04.609], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 31 new messages, with total 31 so far. Last group took: 912 millis which is: 33.991 messages per second. average: 33.991
[2018-09-22 09:23:05.601], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 1031 so far. Last group took: 999 millis which is: 1,001.001 messages per second. average: 539.508
[2018-09-22 09:23:06.601], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 2031 so far. Last group took: 1000 millis which is: 1,000 messages per second. average: 697.698
[2018-09-22 09:23:07.603], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 3031 so far. Last group took: 1002 millis which is: 998.004 messages per second. average: 774.597
[2018-09-22 09:23:08.602], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 4031 so far. Last group took: 999 millis which is: 1,001.001 messages per second. average: 820.643
[2018-09-22 09:23:09.603], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 5031 so far. Last group took: 1001 millis which is: 999.001 messages per second. average: 850.837
[2018-09-22 09:23:10.603], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 6031 so far. Last group took: 1000 millis which is: 1,000 messages per second. average: 872.414
[2018-09-22 09:23:11.601], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 7031 so far. Last group took: 998 millis which is: 1,002.004 messages per second. average: 888.762
[2018-09-22 09:23:12.603], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 8031 so far. Last group took: 1002 millis which is: 998.004 messages per second. average: 901.043
[2018-09-22 09:23:13.601], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 1000 new messages, with total 9031 so far. Last group took: 998 millis which is: 1,002.004 messages per second. average: 911.21
[2018-09-22 09:23:14.016], [INFO ], dataset://testDataSet?produceDelay=-1, Camel (camel-1) thread #3 - dataset://testDataSet, dataset://testDataSet?produceDelay=-1, Sent: 10000 messages so far. Last group took: 10324 millis which is: 968.617 messages per second. average: 968.617
[2018-09-22 09:23:14.602], [INFO ], sample, Camel (camel-1) thread #2 - ThroughputLogger, sample, Received: 969 new messages, with total 10000 so far. Last group took: 1001 millis which is: 968.032 messages per second. average: 916.422

オプション

Throttlerパターンで設定できるオプションは以下に記載されています。

設定により単位時間あたりに処理するリクエスト数を動的に変更することもできるようです。

3
0
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
3
0