Borland(R) Enterprise Server VisiBroker(R) デベロッパーズガイド
コードサンプル13-5および13-6では,TriggerHandlerのインプリメントと登録をします。TriggerHandlerImplのimpl_is_ready()メソッドとimpl_is_down()メソッドは,トリガーを起動する原因となったインスタンスの記述を表示し,オプションとしてそれ自体の登録を解除します。
TriggerHandlerImplクラスは,このクラスの生成に使われたdescパラメタとAgentパラメタのコピーを保持していることに注意してください。unreg_trigger()メソッドにはdescパラメタが必要です。Agentパラメタは,メインプログラムからのリファレンスが解放された場合に備えて複製されています。
// AccountTrigger.c #include "locate_s.hh" // USE_STD_NS is a define set up by VisiBroker to use the // std namespace USE_STD_NS Instances of this class // will be called back by the Agent when the // event for which it is registered happens. class TriggerHandlerImpl : public _sk_ObjLocation::_sk_TriggerHandler { public: TriggerHandlerImpl( ObjLocation::Agent_var agent, const ObjLocation::TriggerDesc& initial_desc) : _agent(ObjLocation::Agent::_duplicate(agent)), _initial_desc(initial_desc) {} void impl_is_ready(const ObjLocation::Desc& desc) { notification(desc, 1); } void impl_is_down(const ObjLocation::Desc& desc){ notification(desc, 0); } private: void notification(const ObjLocation::Desc& desc, CORBA::Boolean isReady){ if (isReady) { cout << "Implementation is ready:" << endl; } else { cout << "Implementation is down:" << endl; } cout << "\tRepository Id = " << desc.repository_id << endl; cout << "\tInstance Name = " << desc.instance_name << endl; cout << "\tHost Name = " << desc.iiop_locator.host << endl; cout << "\tPort = " << desc.iiop_locator.port << endl; cout << "\tAgent Host = " << desc.agent_hostname << endl; cout << "\tActivable = " << (desc.activable? "YES" : "NO") << endl; cout << endl; cout << "Unregister this handler and exit (yes/no)? " << endl; char prompt [256]; cin >> prompt; if ((prompt[0] == 'y') || (prompt[0] == 'Y')) { try { _agent->unreg_trigger(_initial_desc, this); } catch (const ObjLocation::Fail& e) { cout << "Failed to unregister trigger with reason=[" << (int) e.reason << "]" << endl; } cout << "exiting..." << endl; CORBA::ORB::shutdown(); } } private: ObjLocation::Agent_var _agent; ObjLocation::TriggerDesc _initial_desc; }; int main(int argc,char* const *argv) { try { CORBA::ORB_var the_orb = CORBA::ORB_init(argc, argv); CORBA::BOA_var boa = the_orb->BOA_init(argc, argv); CORBA::Object_var obj = the_orb-> resolve_initial_references("LocationService"); if ( CORBA::is_nil(obj) ) { cout << "Unable to locate initial LocationService" << endl; return 0; } ObjLocation::Agent_var the_agent = ObjLocation::Agent::_narrow(obj); // Create the trigger descriptor to notify us about // osagent changes with respect to Account objects ObjLocation::TriggerDesc desc; desc.repository_id = (const char*) "IDL:Bank/AccountManager:1.0"; desc.instance_name = (const char*) ""; desc.host_name = (const char*) ""; ObjLocation::TriggerHandler_var trig = new TriggerHandlerImpl(the_agent, desc); boa->obj_is_ready(trig); the_agent->reg_trigger(desc,trig); boa->impl_is_ready(); } catch (const CORBA::Exception& e) { cout << "account_trigger caught Exception: " << e << endl; return 0; } return 1; }
// AccountTrigger.java import java.io.*; import org.omg.PortableServer.*; class TriggerHandlerImpl extends com.inprise.vbroker.ObjLocation.TriggerHandlerPOA { public TriggerHandlerImpl( com.inprise.vbroker.ObjLocation.Agent agent, com.inprise.vbroker.ObjLocation.TriggerDesc initial_desc){ agent = agent; initial_desc = initial_desc; } public void impl_is_ready( com.inprise.vbroker.ObjLocation.Desc desc){ notification(desc, true); } public void impl_is_down( com.inprise.vbroker.ObjLocation.Desc desc){ notification(desc,false); } private void notification( com.inprise.vbroker.ObjLocation.Desc desc, boolean isReady){ if (isReady){ System.out.println("Implementation is ready:"); } else { System.out.println("Implementation is down:"); } System.out.println( "\tRepository Id = " + desc.repository_id + "\n" + "\tInstance Name = " + desc.instance_name + "\n" + "\tHost Name = " + desc.iiop_locator.host + "\n" + "\tActivable = " + desc.activable + "\n" + "\n"); System.out.println( "Unregister this handler and exit (yes/no)?"); try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String line = in.readLine(); if(line.startsWith("y") || line.startsWith("Y")) { try { agent.unreg_trigger(_initial_desc, _this()); } catch (com.inprise.vbroker.ObjLocation.Fail e){ System.out.println( "Failed to unregister trigger with reason=[" + e.reason + "]"); } System.out.println("exiting..."); System.exit(0); } } catch (java.io.IOException e){ System.out.println( "Unexpected exception caught: " + e); System.exit(1); } } private com.inprise.vbroker.ObjLocation.Agent _agent; private com.inprise.vbroker.ObjLocation.TriggerDesc _initial_desc; } public class AccountTrigger { public static void main(String args[ ]){ try { // Initialize the ORB. org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); POA rootPoa = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); rootPoa.the_POAManager().activate(); com.inprise.vbroker.ObjLocation.Agent the_agent = com.inprise.vbroker.ObjLocation.AgentHelper.narrow( orb.resolve_initial_references("LocationService")); // Create a trigger description and an appropriate // TriggerHandler. The TriggerHandler will be // invoked when the osagent become aware of any // new implementations of the interface // "Bank::AccountManager" com.inprise.vbroker.ObjLocation.TriggerDesc desc = new com.inprise.vbroker.ObjLocation.TriggerDesc( "IDL:Bank/AccountManager:1.0", "", ""); TriggerHandlerImpl trig = new TriggerHandlerImpl(the_agent, desc); rootPoa.activate_object(trig); the_agent.reg_trigger(desc, trig._this()); orb.run(); }catch (Exception e){ e.printStackTrace(); System.exit(1); } } }
All Rights Reserved. Copyright (C) 2008, Hitachi, Ltd.
COPYRIGHT (C) 1992-2004 Borland Software Corporation. All rights reserved.