TAO 处理接口访问超时例程

TAO 处理接口访问超时例程


在CORBA中有时需要判断接口访问是否超时,因为接口之间访问往往是同步操作,没有超时机制的话调用者很容易阻塞,下面例子可以帮助我们解决这个问题:

#include "helloC.h"
#include "ace/Profile_Timer.h"
#include "tao/Messaging/Messaging.h"
#include "tao/Messaging/TAO_ExtC.h"
#include <fstream><br>#include <iostream><br>#include <string></string></iostream></fstream>

ACE_Profile_Timer timer; //定时器
TimeBase::TimeT timeout = 6*1000*10000; //超时6秒

int main(int argc, char ** argv)
{
CORBA::ORB_var orb;

if (argc printf("usage: %s <file:>", argv[0]); <br> return -1; <br>} <br>orb = CORBA::ORB_init(argc, <br> argv, <br> 0 <br> ACE_ENV_ARG_PARAMETER); <br>if (CORBA::is_nil(orb.in())) <br> return -1; <br>CORBA::Object_var obj = orb-&gt;string_to_object(argv[1] ACE_ENV_ARG_PARAMETER); <br>if (CORBA::is_nil(obj.in())) <br> return -1; <br>/*获取ORB的策略对象*/ <br>CORBA::Object_var objPolicy = orb-&gt;resolve_initial_references("PolicyCurrent" <br> ACE_ENV_ARG_PARAMETER); <br>CORBA::PolicyCurrent_var policyCur = <br> CORBA::PolicyCurrent::_narrow (objPolicy.in () ACE_ENV_ARG_PARAMETER); <br>/*设置超时*/ <br>CORBA::Any anyTm; <br>anyTm /*设置ORB策略*/ <br>CORBA::PolicyList policy_list(1); <br>policy_list.length (1); <br>policy_list[0] = orb-&gt;create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE, <br> anyTm <br> ACE_ENV_ARG_PARAMETER); <br>policyCur-&gt;set_policy_overrides (policy_list, <br> CORBA::ADD_OVERRIDE <br> ACE_ENV_ARG_PARAMETER); <br>policy_list[0]-&gt;destroy (ACE_ENV_SINGLE_ARG_PARAMETER); <br>timer.start(); //启动定时器 <br>ACE_TRY <br>{ <br> Hello_var hello = Hello::_narrow(obj.in()); <br> CORBA::String_var msg; <br> hello-&gt;message(msg); //访问服务接口 <br> std::cout} ACE_CATCHANY { <br> timer.stop(); <br> ACE_Profile_Timer::ACE_Elapsed_Time el; <br> timer.elapsed_time (el); <br> printf("elapse time is %d\n", el.real_time); <br>} <br>ACE_ENDTRY; <br>orb-&gt;destroy();</file:>

return 0;
}

你可能感兴趣的:(接口)