スレッドとタスクの例

CICSExecutorService を使用すると、CICS でタスクを開始するスレッドを作成できます。このサービスを使用してスレッドを作成する場合、CICS サービスにアクセスするために JCICS API を使用可能な CICS タスクが作成されます。

CICS タスクを開始するスレッドを作成するには、2 つのオプションがあります。移植可能なコードにする場合には、OSGi フレームワークで ExecutorService を使用できます。アプリケーションを JVM サーバーで実行している場合、 OSGi フレームワークは自動的に CICS 実装環境を使用して、スレッドの作成時に CICS タスクを開始します。CICS 専用のアプリケーションを作成している場合、JCICS を使用して CICSExecutorService クラスを直接使用できます。

以下の例では、一時記憶域キューを作成するためのスレッドを開始し、データをそのキューに書き込む Java クラスの抜粋を示します。
package com.ibm.cics.executor.test;

import com.ibm.cics.server.CICSExecutorService;
import com.ibm.cics.server.TSQ;
import com.ibm.cics.server.TSQType;

public class ExecutorTest 
{
    public static void main(String[] args) 
    {                                        
        // Inline the new Runnable class 
        class CICSJob implements Runnable
        {             
            public void run()
            {
                // Create a temporary storage queue
                TSQ test_tsq = new TSQ();
                test_tsq.setType(TSQType.MAIN);

                // Set the TSQ name
                test_tsq.setName("TSQWRITE");
                   
                // Write to the temporary storage queue 
                // Use the CICS region local CCSID so it is readable
                String test_string = "Hello from a non CICS Thread - "+ threadId;
                try
                {
                    test_tsq.writeItem(test_string.getBytes(System.getProperty("com.ibm.cics.jvmserver.local.ccsid")));                                                     
                } 
                catch (Exception e)
                {
                    e.printStackTrace();
                }                   
            }
        }

        // Create and run the new CICSJob Runnable
        Runnable task = new CICSJob();
        CICSExecutorService.runAsCICS(task);                                                           
    }
}