# テンプレートを使ってメッセージボディを作成

## このタスクについて

このシナリオでは、cTextTemplateコンポーネントを使ってルート内でメッセージボディを作成する方法を示します。

## コンポーネントをドロップしてリンク

### 手順

1. [Palette] (パレット)からデザインワークスペースに、cTimerコンポーネントを1つ、cSetHeaderコンポーネントを1つ、cMulticastコンポーネントを1つ、cDirectコンポーネントを2つ、cTextTemplateコンポーネントを2つ、cLogコンポーネントを2つドラッグ＆ドロップします。
2. [Row] (行) &gt; [Route] (ルート)接続を使って、下のようにコンポーネントをリンクさせます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_1.png)

## コンポーネントを設定

### 手順

1. cTimerコンポーネントをダブルクリックして、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_2.png)
2. [Repeat] (繰り返し回数)フィールドに2と入力すると、メッセージ交換が2回生成されます。その他のオプションはデフォルト設定のままにしておきます。
3. cSetHeaderコンポーネントをダブルクリックし、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_3.png)
4. [New headers to set] (設定する新しいヘッダー)で[+]ボタンをクリックし、[Headers] (ヘッダー)テーブルにヘッダーを3つ追加して、各ヘッダーで名前と値を指定します。

   最初のヘッダーに、"username"という名前を付けます。[Language] (言語)リストでBeanを選択します。値フィールドにroutines.TalendDataGenerator.class, "getFirstName"と入力すると、ヘッダー用にランダムなファーストネームが生成されます。

   2番目のヘッダーに、"vip"という名前を付けます。[Language] (言語)リストでSimpleを選択します。値フィールドに"${exchangeProperty.CamelTimerCounter} == 1", Boolean.classと入力すると、現在のカウンターが1に等しい場合はヘッダーがTrueに設定されます。

   3番目のヘッダーに、"today"という名前を付けます。[Language] (言語)リストでBeanを選択します。値フィールドに"${date:now:yyyy-MM-dd}"と入力すると、現在の日付がヘッダーとして生成されます。
5. cMulticastコンポーネントをダブルクリックし、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_4.png)
6. [+]をクリックして、URISテーブルに2行追加します。メッセージが送信されるエンドポイントのURIを指定します。このユースケースでは、"direct:velocity-build-in"と"direct:freemarker-repo"です。
7. 最初のcDirectコンポーネントをダブルクリックし、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_5.png)
8. [Name] (名前)フィールドに、エンドポイントURIとして"velocity-build-in"と入力します。

   このステップを繰り返し、2番目のcDirectコンポーネントの名前を"freemarker-repo"に設定します。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_6.png)
9. 最初のcTextTemplateコンポーネントをダブルクリックし、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_7.png)
10. [Template Engine] (テンプレートエンジン)リストで、Velocityを選択します。

    [Type] (タイプ)リストで[Built-in] (組み込み)を選択します。

    [Template] (テンプレート)ボックスに次のコードを入力します:
    ```
    "
    Hello $headers.userName,

    Today is $headers.today
    #if($headers.vip)

    You're a VIP member!
    #end
    "
    ```
11. 2番目のcTextTemplateコンポーネントをダブルクリックし、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

    ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_8.png)
12. [Template Engine] (テンプレートエンジン)リストで、FreeMarkerを選択します。

    [Type] (タイプ)リストで、[Repository] (リポジトリー)を選択します。

    [Resource] (リソース)フィールドの横にある[...]をクリックし、リポジトリー内のResources &gt; templateフォルダーで、message.ftlという事前定義済みテンプレートを選択します。
    ```
    Hello ${headers.userName},

    Today is ${.now?string("yyyy-MM-dd")}.
    <#if headers.vip?? && headers.vip>

    You're a VIP member!
    </#if>
    ```
13. 最初のcLogコンポーネントをダブルクリックし、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。

    ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_9.png)
14. [Specify output log message] (出力ログメッセージを指定)オプションを選択し、[Message] (メッセージ)フィールドに"${body}"というコードを入力すると、メッセージボディが表示されます。

    このステップを繰り返し、2番目のcLogも同様に設定します。
15. Ctrl + Sを押してジョブを保存します。

## コードを表示させてルートを実行

### 手順

1. デザインワークスペースの下部の[Code] (コード)タブをクリックし、生成されたコードを確認します。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_11.png)

   上記のとおり、ソースメッセージはcMulticast\_1を経由してcTimer\_1からdirect:velocity-build-inとdirect:freemarker-repoにルーティングされます。direct:velocity-build-inからのメッセージはcLog\_1にルーティングされ、direct:freemarker-repoからのメッセージはcLog\_2にルーティングされます。
2. [Run] (実行)ビューをクリックして開き、[Run] (実行)ボタンをクリックしてルートの実行を開始します。F6を押して実行することもできます。

   メッセージボディは、テンプレートで指定されたとおりに生成されます。

   ![](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/Resources/images/use_case-ctexttemplate_10.png)

**関連資料**

* [cTextTemplate](https://help.qlik.com/talend/ja-JP/mediation-components/8.0/mediation-text-processing/ctexttemplate "複数のテンプレートエンジンをサポートしており、テキストテンプレート内のプレースホルダーを実際値で置換します。cSetBodyコンポーネントとして使用でき、複数行のテキスト入力と高度なプレースホルダー処理をサポートします。")

**関連情報**

* [cMulticast](https://help.qlik.com/talend/r/ja-JP/8.0/mediation-components/mediation-multicast/cmulticast)
* [cDirect](https://help.qlik.com/talend/r/ja-JP/8.0/mediation-components/mediation-direct/cdirect)
* [cSetHeader](https://help.qlik.com/talend/r/ja-JP/8.0/mediation-components/mediation-setheader/csetheader)
* [cTimer](https://help.qlik.com/talend/r/ja-JP/8.0/mediation-components/mediation-orchestration/ctimer)
* [cLog](https://help.qlik.com/talend/r/ja-JP/8.0/mediation-components/mediation-logs-and-errors/clog)