# ユースケース: 複数のジョブにアクセスできる変数を定義

このユースケースでは、ジョブにアクセスできる変数が作成されます。また、変数へのアクセス方法に関する例も示します。

## ルーチンを作成し、静的変数を宣言

### 手順

1. [Repository] (リポジトリー)ツリービューで、[MyRoutine]という新しいルーチンを作成します。
2. ルーチン内で静的変数を宣言し、その値をnullに設定します。例:
   ```
   private static String name = null;                      
   ```
3. この変数にセッター/ゲッターメソッドを追加し、Ctrl + Sを押して変更を保存します。
   ```
   public static synchronized void setValue(String message) {
                        name=message;
                        }
                        
                        public static synchronized String getValue() {
                        return name;
                        }                      
   ```

   情報メモ注: このルーチンの完全なJavaコードは次のとおりです。
   ```
   package routines;
                           /*
                           * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
                           * it must be before the "{talendTypes}" key.
                           *
                           * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
                           * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
                           * Short
                           *
                           * 3. {Category} define a category for the Function. it is required. its value is user-defined .
                           *
                           * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
                           *
                           * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
                           * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
                           * added. you can have many parameters for the Function.
                           *
                           * 5. {example} gives a example for the Function. it is optional.
                           */
                           public class MyRoutine {
                           private static String name = null;
                           
                           /**
                           * helloExample: not return value, only print "hello" + message.
                           *
                           *
                           * {talendTypes} String
                           *
                           * {Category} User Defined
                           *
                           * {param} string("world") input: The string need to be printed.
                           *
                           * {example} helloExemple("world") # hello world !.
                           */
                           public static void helloExample(String message) {
                           if (message == null) {
                           message = "World"; //$NON-NLS-1$
                           }
                           System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
                           }
                           
                           public static synchronized void setValue(String message) {
                           name = message;
                           }
                           
                           public static synchronized String getValue() {
                           return name;
                           }
                           }
                        
   ```

## 子ジョブを設定する

以下の例は、以前に定義したルーチンを使用して異なるジョブの間で値を共有する方法を示しています。

### 手順

1. childJob1という名前のジョブを作成し、デザインワークスペースで名前を入力するか、[Palette] (パレット)からデザインワークスペースにドロップして、2つのコンポーネントを追加します。
   * 入力データフローを生成するためのtFixedFlowInput。
   * データを受け取るためのtJavaRow。このコンポーネントでは、変数に新しい値を与えるためのセッターメソッドをジョブが呼び出します。
2. tFixedFlowInputコンポーネントをダブルクリックして、[Basic settings] (基本設定)ビューを開きます。
3. [Edit schema] (スキーマを編集)の横にある[...]ボタンをクリックし、[Schema] (スキーマ)ダイアログボックスを開いて入力データのスキーマを定義します。この例では、スキーマには文字列型のカラムnameが1つだけあります。

   ![tFixedFlowInputコンポーネントの\[スキーマ\]ダイアログボックス。](https://help.qlik.com/talend/ja-JP/studio-user-guide/Resources/images/tme1573031583110_LowRes.png)
4. [Mode] (モード)エリアで[Use Single Table] (単一テーブルを使用)オプションを選択し、[Values] (値)テーブルの*message*カラムで対応する値を定義します。この例では、値は"Talend"です。

   ![\[Basic settings\] (基本設定)ビュー。](https://help.qlik.com/talend/ja-JP/studio-user-guide/Resources/images/heh1573031583158_LowRes.png)

   情報メモ注: tJavaコンポーネントがゲッターメソッドを呼び出し、戻り値を文字列変数に割り当て、変数値をコンソールに出力します。
5. tJavaRowコンポーネントをダブルクリックして、[Basic settings] (基本設定)ビューを開きます。
6. [Code] (コード)エリアに次のコードを入力して、セッターメソッドを追加します。
   ```
   MyRoutine.setValue(input_row.name);
   ```

   ![\[Basic settings\] (基本設定)ビュー。](https://help.qlik.com/talend/ja-JP/studio-user-guide/Resources/images/heh1573031583158_LowRes.png)
7. childJob2という名前のジョブを作成し、そのジョブ内にtJavaコンポーネントを作成します。
8. tJavaコンポーネントをダブルクリックして、[Basic settings] (基本設定)ビューを開きます。
9. [Code] (コード)エリアに次のコードを入力して、ゲッターメソッドを追加します。
   ```
   String name=MyRoutine.getValue();
                        System.out.println(name);
   ```

   ![\[Basic settings\] (基本設定)ビュー。](https://help.qlik.com/talend/ja-JP/studio-user-guide/Resources/images/rik1573031583174_LowRes.png)

## 親ジョブのセットアップ

### 手順

1. parentJobという名前のジョブを作成し、2つのtRunJobコンポーネントを追加します。
2. OnSubjobOkリンクを使用して、2番目のtRunJobコンポーネントを最初のtRunJobコンポーネントに接続します。
3. 両方のtRunJobコンポーネントをダブルクリックして、[Basic settings] (基本設定)ビューを開きます。
4. tRunJob1とtRunJob2の[Job] (ジョブ)フィールドにそれぞれ、childJob1とchildJob2を入力して、作成された2つの子ジョブを呼び出します。

   ![デザインワークスペース内の完了ジョブのスクリーンショット。](https://help.qlik.com/talend/ja-JP/studio-user-guide/Resources/images/btb1573031583086_LowRes.png)

   ルーチンで変数を定義して異なるジョブの間で共有するというこのアプローチは、tRunJobコンポーネントの[Use an independent process to run subjob] (サブジョブを独立したプロセスで実行)機能には使用できません。

## ジョブを実行してルーチンを呼び出す

### 手順

parentJobを実行します。コンソールに以下の結果が表示されます。

```
Starting job parentJob at 19:52 21/06/2013.
                     [statistics] connecting to socket on port 3397
                     [statistics] connected
                     Talend
                     [statistics] disconnected
                     Job parentJob ended at 19:52 21/06/2013. [exit code=0]
```