7.4.4 デフォルトサーバントによる活性化
オブジェクトIDが何であろうとPOAに同じサーバントを起動させるには,RequestProcessing::USE_DEFAULT_SERVANTポリシー(C++),またはRequestProcessing.USE_DEFAULT_SERVANTポリシー(Java)を使用してください。これは各オブジェクトに少量のデータしか持たせていない場合に便利です。
- コードサンプル7-9 同じサーバントによるすべてのオブジェクトの活性化例(C++)
int main(int argc, char* const* argv) { try { // Initialize the ORB. CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv); PortableServer::Current_var cur = PortableServer::Current::_instance(); DataStore::_create(); // get a reference to the rootPOA CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj); CORBA::PolicyList policies; policies.length(3); // Create policies for our persistent POA policies[(CORBA::ULong)0] = rootPOA->create_lifespan_policy (PortableServer::PERSISTENT); policies[(CORBA::ULong)1] = rootPOA->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT); policies[(CORBA::ULong)2] = rootPOA->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID); // Create myPOA with the right policies PortableServer::POAManager_var rootManager = rootPOA->the_POAManager(); PortableServer::POA_var myPOA = rootPOA->create_POA ("bank_default_servant_poa", rootManager, policies); // Set the default servant AccountManagerImpl * managerServant = new AccountManagerImpl(cur); myPOA->set_servant( managerServant ); // Activate the POA Manager rootManager->activate(); // Generate two references - one for checking // and another for savings. // Note that we are not creating any // servants here and just manufacturing a reference // which is not yet backed by a servant PortableServer::ObjectId_var an_oid = PortableServer::string_to_ObjectId ("CheckingAccountManager"); CORBA::Object_var cref = myPOA->create_reference_with_id(an_oid.in(), "IDL:Bank/AccountManager:1.0"); an_oid = PortableServer::string_to_ObjectId ("SavingsAccountManager"); CORBA::Object_var sref = myPOA->create_reference_with_id (an_oid.in(), "IDL:Bank/AccountManager:1.0"); // Write out Checking reference CORBA::String_var string_ref = orb->object_to_string(cref.in()); ofstream crefFile("cref.dat"); crefFile << string_ref << endl; crefFile.close(); // Now write out the Savings reference string_ref = orb->object_to_string(sref.in()); ofstream srefFile("sref.dat"); srefFile << string_ref << endl; srefFile.close(); cout << "Bank Manager is ready" << endl; // Wait for incoming requests orb->run(); DataStore::_destroy(); } catch(const CORBA::Exception& e) { cerr << e << endl; } return 1; }
- コードサンプル7-10 同じサーバントによるすべてのオブジェクトの活性化例(Java)
import org.omg.PortableServer.*; public class Server { public static void main(String[ ] args) { try { // Initialize the ORB. org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); // get a reference to the rootPOA POA rootPOA = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); // Create policies for our persistent POA org.omg.CORBA.Policy[ ] policies = { rootPOA.create_lifespan_policy( LifespanPolicyValue.PERSISTENT), rootPOA.create_request_processing_policy( RequestProcessingPolicyValue.USE_DEFAULT_SERVANT ) }; // Create myPOA with the right policies POA myPOA = rootPOA.create_POA( "bank_default_servant_poa", rootPOA.the_POAManager(), policies ); // Create the servant AccountManagerImpl managerServant = new AccountManagerImpl(); // Set the default servant on our POA myPOA.set_servant(managerServant); org.omg.CORBA.Object ref; // Activate the POA manager rootPOA.the_POAManager().activate(); // Generate the reference and write it out. One // for each Checking and Savings account types. // Note that we are not creating any servants // here and just manufacturing a reference // which is not yet backed by a servant. try { ref = myPOA.create_reference_with_id( "CheckingAccountManager".getBytes(), "IDL:Bank/AccountManager:1.0"); // Write out checking object ID java.io.PrintWriter pw = new java.io.PrintWriter( new java.io.FileWriter("cref.dat")); pw.println(orb.object_to_string(ref)); pw.close(); ref = myPOA.create_reference_with_id( "SavingsAccountManager".getBytes(), "IDL:Bank/AccountManager:1.0"); // Write out savings object ID pw = new java.io.PrintWriter( new java.io.FileWriter("sref.dat")); System.gc(); pw.println(orb.object_to_string(ref)); pw.close(); } catch ( java.io.IOException e ){ System.out.println("Error writing the IOR to file "); return; } System.out.println("Bank Manager is ready."); // Wait for incoming requests orb.run(); }catch (Exception e){ e.printStackTrace(); } } }