Threads and tasks example

You can create a thread that starts a task in CICS by using the CICSExecutorService. If you use this service to create a thread, a CICS task is created that can use the JCICS API to access CICS services.

You have two options for creating a thread that starts a CICS task. If you want your code to be portable, you can use the ExecutorService in the OSGi framework. When the application is running in a JVM server, the OSGi framework automatically uses the CICS implementation to start a CICS task when a thread is created. If you are writing an application specifically for CICS, you can use the CICSExecutorService class directly using JCICS.

The following example shows an excerpt of a Java class that starts a thread to create a temporary storage queue and writes some data to the queue.
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);                                                           
    }
}