Hitachi

VisiBroker Version 5 Borland(R) Enterprise Server VisiBroker(R) デベロッパーズガイド


7.4.5 オブジェクトの非活性化

POAはアクティブオブジェクトマップからサーバントを削除できます。例えば,これはガーベッジコレクション手法の一形態として行います。サーバントをマップから削除すると,そのサーバントは非活性化されます。deactivate_object()メソッドを使用してオブジェクトを非活性化できます。オブジェクトを非活性化すると,このオブジェクトが永遠に失われるということではありません。あとで再度活性化できます。

コードサンプル7-11 オブジェクトの非活性化例(C++)
// DeActivatorThread
class DeActivatorThread: public VISThread {
   private :
      PortableServer::ObjectId _oid;
      PortableServer::POA_ptr _poa;
 
   public :
      virtual ~DeActivatorThread(){}
      // Constructor
      DeActivatorThread(const PortableServer::ObjectId& oid,
            PortableServer::POA_ptr poa ): _oid(oid), 
            _poa(poa) {
         // start the thread
         run();
      }
 
      // implement begin() callback
      void begin() {
         // Sleep for 15 seconds
         VISPortable::vsleep(15);
 
         CORBA::String_var s = 
            PortableServer::ObjectId_to_string (_oid);
         // Deactivate Object
         cout << "\nDeactivating the object with ID = 
            " << s << endl;
         if ( _poa )
            _poa->deactivate_object( _oid );
      }
};
 
// Servant Activator
class AccountManagerActivator : 
      public PortableServer::ServantActivator {
   public:
      virtual PortableServer::Servant incarnate 
            (const PortableServer::ObjectId& oid,
            PortableServer::POA_ptr poa) {
         CORBA::String_var s = 
            PortableServer::ObjectId_to_string (oid);
         cout << "\nAccountManagerActivator.incarnate called 
            with ID = " << s << endl;
         PortableServer::Servant servant;
 
         if ( VISPortable::vstricmp( (char *)s, 
               "SavingsAccountManager" ) == 0 )
            // Create CheckingAccountManager Servant
            servant = new SavingsAccountManagerImpl;
         else if ( VISPortable::vstricmp( (char *)s, 
               "CheckingAccountManager" ) == 0 )
            // Create CheckingAccountManager Servant
            servant = new CheckingAccountManagerImpl;
         else
            throw CORBA::OBJECT_NOT_EXIST();
         // Create a deactivator thread
         new DeActivatorThread( oid, poa );
         // return the servant
         servant->_add_ref();
         return servant;
      }
 
      virtual void etherealize (
               const PortableServer::ObjectId& oid,
            PortableServer::POA_ptr adapter,
            PortableServer::Servant servant,
            CORBA::Boolean cleanup_in_progress,
            CORBA::Boolean remaining_activations) {
      // If there are no remaining activations i.e ObjectIds 
      // associated with the servant delete it.
 
      CORBA::String_var s = 
            PortableServer::ObjectId_to_string (oid);
      cout << "\nAccountManagerActivator.etherealize called 
            with ID = " << s << endl;
      if (!remaining_activations)
         delete servant;
   }
};
コードサンプル7-12 オブジェクトの非活性化例(Java)
import org.omg.PortableServer.*;
public class AccountManagerActivator extends
                                       ServantActivatorPOA {
   public Servant incarnate (
            byte[ ] oid, POA adapter) throws ForwardRequest {
      Servant servant;
      String accountType = new String(oid);
      System.out.println(
        "\nAccountManagerActivator. incarnate 
         called with ID = " + accountType + "\n");
      // Create Savings or Checking Servant based on
      // AccountType
      if (accountType.equalsIgnoreCase(
                                   "SavingsAccountManager"))
         servant = (Servant )new SavingsAccountManagerImpl();
      else
         servant = (Servant)new CheckingAccountManagerImpl();
         new DeactivateThread(oid, adapter).start();
         return servant;
      }
      public void etherealize (byte[ ] oid,
            POA adapter,
            Servant serv,
            boolean cleanup_in_progress,
            boolean remaining_activations) {
         System.out.println(
           "\nAccountManagerActivator.etherealize
            called with ID = " + new String(oid) + "\n");
         System.gc();
      }
}
class DeactivateThread extends Thread {
   byte[ ] _oid;
   POA _adapter;
   public DeactivateThread(byte[ ] oid, POA adapter) {
      _oid = oid;
      _adapter = adapter;
   }
   public void run() {
      try {
         Thread.currentThread().sleep(15000);
         System.out.println(
                    \nDeactivating the object with ID = " + 
                    new String(_oid) + "\n");
         _adapter.deactivate_object(_oid);
   } catch (Exception e) {
     e.printStackTrace();
   }
}
}