4.6.2 TSCThreadを利用するサーバアプリケーションの例(Java)

TSCThreadを利用するサーバアプリケーションの処理の流れとコードの例を示します。斜体で示しているコードは,雛形クラスとして自動生成される部分です。太字で示しているコードは,同期型呼び出しのコードと異なる部分です。

サーバアプリケーションの作成時には,ユーザは,自動生成された雛形クラスABC_TSCimplにTSCユーザオブジェクトのコードを記述します。また,雛形クラスABC_TSCfactimplにTSCユーザオブジェクトファクトリのコードを記述します。さらに,TSCユーザスレッドとしてTSCThreadの派生クラスを記述し,TSCユーザスレッドファクトリとしてTSCThreadFactoryの派生クラスを記述します。

<この項の構成>
(1) TSCユーザオブジェクト(ABC_TSCimpl)のコード
(2) TSCユーザオブジェクトファクトリ(ABC_TSCfactimpl)のコード
(3) TSCユーザスレッド(UserTImpl)のコード
(4) TSCユーザスレッドファクトリ(UserTFactImpl)
(5) サービス登録処理の流れ
(6) サービス登録処理のコード

(1) TSCユーザオブジェクト(ABC_TSCimpl)のコード

//
// "ABC_TSCimpl.java"
//

import OctetSeqHelper;
import OctetSeqHolder;
import JP.co.Hitachi.soft.TPBroker.TSC.TSCThread;

// import classes used in this implementation, if necessary.
import java.lang.System;

public class ABC_TSCimpl extends ABC_TSCsk
{
 // Write class variables, if necessary

 public ABC_TSCimpl()
 {
   // Constructor of implementation.
   // Write user own code.
   //TSCユーザオブジェクトのコンストラクタのコードを記述します。
   //引数の数および型を変更できます。
   super();
 };

 public void call(byte[] in_data, OctetSeqHolder out_data)

 {
   // Operation "call".
   // Write user own code.
   //ユーザメソッドのコードを記述します。

   //TSCユーザスレッドの取得
   TSCThread my_thr = this._TSCThread();

   //ユーザクラスにキャスト
   UserTImpl my_thr_impl = (UserTImpl)my_thr;
   
   //UserTImplのメソッドを呼び出し,値を取得します。
   int thr_value = my_thr_impl.getValue();


   //メソッドが呼ばれた回数を増加させます。
   //(このメソッドの処理は引数の値と無関係です)
   m_counter++;
   out_data.value = new byte[4];

   System.out.println("Call method in ABC_TSCprxy");

 };

 //メソッドが呼ばれた回数
 protected int m_counter = 0;

};

(2) TSCユーザオブジェクトファクトリ(ABC_TSCfactimpl)のコード

同期型呼び出しの場合と同様です。「4.2.2(2) TSCユーザオブジェクトファクトリ(ABC_TSCfactimpl)のコード」を参照してください。

(3) TSCユーザスレッド(UserTImpl)のコード

//
// "UserTImpl.Java"
//

import JP.co.Hitachi.soft.TPBroker.TSC.*;

public class UserTImpl
 implements TSCThread
{
 public UserTImpl(int init_info)
 {
   m_value = init_info;
 }
 public int getValue()
 {
   return m_value;
 }

 protected int m_value;
}

(4) TSCユーザスレッドファクトリ(UserTFactImpl)

//
// "UserThreadFactory.java"
//

import JP.co.Hitachi.soft.TPBroker.TSC.*;

class UserTFactImpl
 implements TSCThreadFactory
{
 public UserTFactImpl()
 {}
 public TSCThread create()
 {
   //TSCユーザスレッドを生成します。
   TSCThread usr_thr = new UserTImpl(222);
   return usr_thr;
 }

 public void destroy(TSCThread tsc_thr)
 {
   //後処理を記述します。
 }

};

(5) サービス登録処理の流れ

  1. TPBrokerの初期化処理
  2. TPBroker OTMの初期化処理
  3. TSCデーモンへの接続
  4. TSCユーザオブジェクトファクトリ,TSCユーザアクセプタの生成(new),および各種設定
  5. TSCユーザスレッドファクトリの生成および各種設定
  6. TSCルートアクセプタの生成および各種設定
  7. TSCルートアクセプタの活性化
  8. 実行制御の受け渡し
  9. TSCルートアクセプタの非活性化
  10. TSCデーモンへの接続解放
  11. TPBroker OTMの終了処理

(6) サービス登録処理のコード

//
// "ServerAP.java"
//

import JP.co.Hitachi.soft.TPBroker.TSC.*;

public class ServerAP
{

 public static void main(String[] args)
 {

   ////////
   // 1, TPBrokerの初期化処理
   ////////

   org.omg.CORBA.ORB orb = null;
   try
   {
     // ORBの初期化
     orb = org.omg.CORBA.ORB.init(args, null);
   }
   catch(org.omg.CORBA.SystemException ce)
   {
     // 例外処理
     System.out.println(ce);
     System.exit(1);
   }

   ////////
   // 2, TPBroker OTMの初期化処理
   ////////
   try
   {
     // TSCの初期化
     TSCAdm.initServer(args, orb);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     System.exit(1);
   }

   ////////
   // 3, TSCデーモンへの接続
   ////////
   TSCDomain domain = null;
   try
   {
     domain = new TSCDomain(null, null);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   TSCServer tsc_server = null;
   try
   {
     // TSCデーモンの参照オブジェクトを取得
     tsc_server = TSCAdm.getTSCServer(domain);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 4, TSCユーザオブジェクトファクトリ,TSCユーザアクセプタの
   //    生成(new)および各種設定
   ////////
   // ABC_TSCfactimplの生成
   TSCObjectFactory my_fact = new ABC_TSCfactimpl();

   // TSCAcceptorの生成
   TSCAcceptor my_acpt = null;
   try
   {
     my_acpt = new ABC_TSCacpt(my_fact);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.releaseTSCServer(tsc_server);
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 5, TSCユーザスレッドファクトリの生成および各種設定
   ////////

   TSCThreadFactory my_thr_fact = new UserTFactImpl();

   ////////
   // 6, TSCルートアクセプタの生成および各種設定
   ////////

   // TSCRootAcceptorの生成
   TSCRootAcceptor my_rt_acpt = null;
   try
   {
     my_rt_acpt =
       TSCRootAcceptor.create(tsc_server, my_thr_fact);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.releaseTSCServer(tsc_server);
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   try
   {
     // TSCRootAcceptorへの登録
     my_rt_acpt.registerAcceptor(my_acpt);

     // TSCRootAcceptorのパラレルカウント指定もできます。
     // デフォルト値は1
     // オプション引数でデフォルト値を変更することもできます。
     // my_rt_acpt.setParallelCount(5);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.releaseTSCServer(tsc_server);
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 7, TSCルートアクセプタの活性化
   ////////
   try
   {
     // オブジェクトの活性化
     my_rt_acpt.activate("serviceX");
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.releaseTSCServer(tsc_server);
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 8, 実行制御の受け渡し
   ////////
   try
   {
     TSCAdm.serverMainloop();
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       my_rt_acpt.deactivate();
       TSCAdm.releaseTSCServer(tsc_server);
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 9, TSCルートアクセプタの非活性化
   ////////
   try
   {
     // オブジェクトの非活性化
     my_rt_acpt.deactivate();
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.releaseTSCServer(tsc_server);
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 10, TSCデーモンへの接続解放
   ////////
   try
   {
     TSCAdm.releaseTSCServer(tsc_server);
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     try
     {
       TSCAdm.endServer();
     }
     catch(TSCSystemException se)
     {
       System.exit(1);
     }
     System.exit(1);
   }

   ////////
   // 11, TPBroker OTMの終了処理
   ////////
   try
   {
     TSCAdm.endServer();
   }
   catch(TSCSystemException tsc_se)
   {
     // 例外処理
     System.out.println(tsc_se);
     System.exit(1);
   }

   System.exit(0);

 }
}