스레드 및 태스크 예제

CICSExecutorService를 사용하여 CICS에서 태스크를 시작하는 스레드를 작성할 수 있습니다. 이 서비스를 사용하여 스레드를 작성하면 JCICS API를 사용하여 CICS 서비스에 액세스할 수 있는 CICS 태스크가 작성됩니다.

CICS 태스크를 시작하는 스레드를 작성할 수 있는 두 가지 선택사항이 있습니다. 코드가 이식될 수 있게 하려면 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);                                                           
    }
}