线程和任务示例

如果您的 CICS® Java 应用程序是在 OSGi 或 Liberty 环境中运行的,您可以通过 CICSExecutorService.NET Framework 3.0,在单独的 CICS 任务/事务的单独线程下运行工作。

将 Java Runnable 或 Callable 对象提交到 Executor 服务,并且提交的应用程序代码将在新的 CICS 任务下的单独线程上运行。 与通过 Java™ 创建的普通线程不同,受 Executor 控制的线程可以访问 JCICS API 和 CICS 服务。 在 CICS OSGi 或 Liberty 环境中,您可以使用标准 OSGi API 来查找 CICSExecutorService,也可以使用 JCICS API 便捷方法 CICSExecutorService.runAsCICS()来查找服务并代表您提交 Runnable 或 Callable 对象。

注: 对于 Liberty 中的非HTTP 请求,仅当发出第一个类型为 2 连接调用的 JCICS 或 JDBC 时,才会创建 CICS 任务。
以下示例显示了将可运行应用程序代码段提交到 CICSExecutorService的 Java 类的摘录。 应用程序代码仅写入 CICS TSQ。
public class ExecutorTest
{
    public static void main(String[] args)
    {
    	// Get the parent task
    	final Task parentTask = Task.getTask();
    	
        // Inline the new Runnable class
        class CICSJob implements CICSTransactionRunnable
        {
            public void run()
            {
            	// Get the child task
            	Task childTask = Task.getTask();
            			
                // Create a temporary storage queue
                TSQ testTsq = new TSQ();
                testTsq.setType(TSQType.MAIN);

                // Set the TSQ name
                testTsq.setName("TSQWRITE");

                // Write to the temporary storage queue
                String message = "Hello from a transaction: "+ childTask.getTransactionName() + ", " +
                		"started as a Java thread from transaction: " + parentTask.getTransactionName();
                try
                {
                	testTsq.writeString(message);
                }
                catch (CicsException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public String getTranid()
            {
                // *** This transaction id should be installed and available ***
                return "IJSA";
            }
        }

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