トランザクショナル分散オブジェクト基盤 TPBroker Object Transaction Monitor プログラマーズガイド

[目次][索引][前へ][次へ]

4.2.2 同期型呼び出しをするサーバアプリケーションの例(Java)

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

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

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

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

 
//
// "ABC_TSCimpl.java"
//
import OctetSeqHelper;
import OctetSeqHolder;
 
// 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.
    //ユーザオブジェクトのコンストラクタのコードを記述します。
    //引数の数および型を変更できます。
    super();
  };
 
  public void call(byte[] in_data, OctetSeqHolder out_data)
 
  {
    // Operation "call".
    // Write user own code.
    //ユーザメソッドのコードを記述します。
 
    //メソッドが呼ばれた回数を増加させます。
    //(このメソッドの処理は引数の値と無関係です)
    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)のコード

 
//
// "ABC_TSCfactimpl.java"
//
 
import JP.co.Hitachi.soft.TPBroker.TSC.TSCObject;
import JP.co.Hitachi.soft.TPBroker.TSC.TSCObjectFactory;
 
import OctetSeqHelper;
import OctetSeqHolder;
 
// import classes used in this implementation, if necessary.
 
public class ABC_TSCfactimpl
    implements TSCObjectFactory
{
  public ABC_TSCfactimpl()
  {
    // Constructor of implementation.
    // Write user own code.
    //TSCユーザオブジェクトファクトリのコードを記述します。
    //引数の数および型を変更することもできます。
  };
 
  public TSCObject create()
  {
    // Method to create user object.
    // Write user own code.
    //サーバオブジェクトを生成するコードを記述します。
    //必要に応じて変更してください。
    return new ABC_TSCimpl();
  };
 
  public void destroy(TSCObject tsc_obj)
  {
    // Method to destroy user object.
    // Write user own code.
    //後処理のコードを記述します。
    //必要に応じて変更してください。
  };
 
};

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

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

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

 
//
//  "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ルートアクセプタの生成および各種設定
    ////////
 
    // TSCRootAcceptorの生成
    TSCRootAcceptor my_rt_acpt = null;
    try
    {
      my_rt_acpt = TSCRootAcceptor.create(tsc_server);
    }
    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);
    }
 
    ////////
    // 6, 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);
    }
 
    ////////
    // 7, 実行制御の受け渡し
    ////////
    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);
    }
 
    ////////
    // 8, 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);
    }
 
    ////////
    // 9, 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);
    }
 
    ////////
    // 10, TPBroker OTMの終了処理
    ////////
    try
    {
      TSCAdm.endServer();
    }
    catch(TSCSystemException tsc_se)
    {
      // 例外処理
      System.out.println(tsc_se);
      System.exit(1);
    }
 
    System.exit(0);
 
  }
}