ICE 异步方法分批和调用

来源:互联网 发布:超基因优化液下载免费 编辑:程序博客网 时间:2024/04/29 08:59

异步方法调用(Asynchronous Method Invocation,简称AMI)

下面这种情况就是AMI调用:“斧头帮”大哥(客户端)叫小弟(服务器端)去干收租的活(远程调用),并且给小弟一把烟花炮竹(回调类)。嘱咐说: “我还有其它事情要打扫打扫,如果你的事情办完了,就放'OK'烟花;如果遇到反抗,就放'斧头'烟花!”(服务器答复)。说完,这位大哥就可以放心的做 其它事去了,直到看到天上烟花盛开,根据"OK"或"斧头"状再作处理。

AMI是针对客户端而言的,当客户端使用AMI发出远程调用时,客户端需要提供一个实现了回调接口的类用于接收通知。然后不等待调用完成立即返回,这时可以继续其它活动,当得到服务器端的答复时,客户端的回调类中的方法就会被执行。

例:修改原Helloworld 客户端,使用异步方法远程调用printString。

首先,要修改原来的Printer.ice定义文件,在printString方法前加上["ami"]元标识符。

  1. module Demo{
  2. interface Printer
  3. {
  4.     ["ami"  void  printString(string s);
  5. };
  6. };

同样,再用slice2cpp Printer.ice生成Printer.h和Printer.cpp文件,并把这两个文件加入原项目(如果是直接修改之前的代码的话,因为原先已经加入了这两个文件,这步可以跳过)。

观察生成的Printer.h文件,可以找到这个定义:

  1. namespace  Demo
  2. {
  3. class  AMI_Printer_printString :  public  ::IceInternal::OutgoingAsync
  4. {
  5. public :
  6.     virtual   void  ice_response() = 0;
  7.     virtual   void  ice_exception( const  ::Ice::Exception&) = 0;
  8. ...
  9. };
  10. }

这里的AMI_Printer_printString 就是printString方法的AMI回调接口,可以发现它AMI回调接口类名的规律是AMI_类名_方法名。

这个接口提供了两个方法:

void ice_response(<params>); 
    表明操作已成功完成。各个参数代表的是操作的返回值及out 参数。如果操作的有一个非 void返回类型,ice_response 方法的第一个参数就是操作的返回值。操作的所有out 参数都按照声明时的次序,跟在返回值的后面。

void ice_exception(const Ice::Exception &); 
    表明抛出了本地或用户异常。

同时,slice2cpp还为Printer代理类生成了异步版本的printString方法:

  1. namespace  IceProxy  //是代理类
  2. {
  3. namespace  Demo
  4. {
  5. class  Printer :  virtual   public  ::IceProxy::Ice::Object
  6. {
  7.     ...
  8. public :
  9.     bool  printString_async( const  ::Demo::AMI_Printer_printStringPtr&,
  10.         const  ::std::string&);
  11.     bool  printString_async( const  ::Demo::AMI_Printer_printStringPtr&,
  12.         const  ::std::string&,  const  ::Ice::Context&);
  13.     ...
  14. };
  15. }
  16. }

结合这两样东西(AMI_Printer_printString 接口和printString_async 方法),我们的客户端AMI调用方法为:

  1. 实现AMI_Printer_printString接口的ice_response和ice_exception方法,以响应远程调用完成后的工作。
  2. 把上面实现的回调对象作为printString_async的参数启动远程调用,然后可以做其它事了。
  3. 当得到服务端答复后,AMI_Printer_printString接口的ice_response的方法被调用。

演示代码(客户端):

  1. #include <ice/ice.h>
  2. #include <printer.h>
  3.  
  4. using   namespace  std;
  5. using   namespace  Demo;
  6.  
  7. //实现AMI_Printer_printString接口
  8. struct  APP : AMI_Printer_printString
  9. {
  10.     virtual   void  ice_response()
  11.     {
  12.         cout << "printString完成"  << endl;
  13.     }
  14.     virtual   void  ice_exception( const  ::Ice::Exception& e)
  15.     {
  16.         cout << "出错啦:"  << e << endl;
  17.     }
  18. };
  19.  
  20. class  MyApp:  public  Ice::Application{
  21. public :
  22.     virtual   int  run( int  argc,  char *argv[])
  23.     {
  24.         Ice::CommunicatorPtr ic = communicator();
  25.  
  26.         Ice::ObjectPrx base =
  27.             ic->stringToProxy("SimplePrinter:default -p 10000" );
  28.  
  29.         Demo::PrinterPrx printer = PrinterPrx::checkedCast(base);
  30.         if (!printer)  throw   "Invalid Proxy!" ;
  31.        
  32.         // 使用AMI异步调用
  33.         printer->printString_async(new  APP, "Hello World!" );
  34.         cout << "做点其它的事情..."  << endl;
  35.         system("pause" );
  36.         return  0;
  37.     }
  38. };
  39.  
  40. int  main( int  argc,  char * argv[])
  41. {
  42.     MyApp app;
  43.     return  app.main(argc,argv);
  44. }

服务端代码不变,编译运行,效果应该是调用printer->printString_async之后还能"做点其它的事情...",当服务端完成后客户端收到通知,显示"printString完成"。

另外,为了突出异步效果,可以修改服务器端代码,故意把printString执行得慢一点:

  1. struct  PrinterImp : Printer{
  2.     virtual   void  printString( const  ::std::string& s,
  3.         const  ::Ice::Current&)
  4.     {
  5.         Sleep(1000);
  6.         cout << s << endl;   
  7.     }
  8. };

异步方法分派(Asynchronous Method Dispatch,简称AMD)

AMD是针对服务器端而言的,在同步的情况下,服务器端收到一个调用请求后,在线程池中拿出一个空闲线程用于执行这个调用。这样,服务器在同一时刻所能支持的同步请求数受到线程池大小的限制。

如果线程池内的线程都在忙于执行长时间的操作,那么新的请求到来时就会处于长时间得不到答复的状态,这可能会造成客户端长时间等待(如果客户端没使用AMI的话)。

ICE的解决方法是:服务器收到请求时并不马上执行具体工作,而是把执行这项工作所需的参数以及回调类保存到一个地方(比如队列)后就返回。而另外的线程(或线程池)负责取出保存的参数并执行之,执行结束后使用回调类通知客户端工作已完成(或异常)。

还是用上面“斧头帮”来举例:“斧头帮”大哥(客户端)叫小弟(服务器端)去干收租的活(远程调用),这位小弟并不是马上就去收租去了,而是把这件 工作记录到他的日程表里(同时还有好几个老板叫他干活呢,可怜的人啊~~)。然后等有空的时候再按日程表一项项的做(或者叫其它有空的弟兄帮忙做),做完 工作后该放烟花的就放烟花(回调智能客户端),该砍人的就放信号弹啥的。

例:修改原Helloworld 服务器端,使用异步方法分派处理printString方法。

首先,要修改原来的Printer.ice定义文件,在printString方法前加上["amd"]元标识符。

  1. module Demo{
  2. interface Printer
  3. {
  4.     ["amd"  void  printString(string s);
  5. };
  6. };

同样,再用slice2cpp Printer.ice生成Printer.h和Printer.cpp文件,并把这两个文件加入原项目(如果是直接修改之前的代码的话,因为原先已经加入了这两个文件,这步可以跳过)。

观察生成的Printer.h文件,可以发现和AMI类似的一个回调接口AMD_Printer_printString :

  1. namespace  Demo
  2. {
  3.  
  4. class  AMD_Printer_printString :  virtual   public  ::IceUtil::Shared
  5. {
  6. public :
  7.  
  8.     virtual   void  ice_response() = 0;
  9.     virtual   void  ice_exception( const  ::std::exception&) = 0;
  10.     virtual   void  ice_exception() = 0;
  11. };
  12. ...
  13. }

这个回调接口由ICE自己实现,我们只要拿来用就可以了。在哪里用呢?马上就会发现:我们要实现的Printer接口的printString方法不见了,取而代之的是printString_async 方法:

  1. namespace  Demo
  2. {
  3. class  Printer :  virtual   public  ::Ice::Object
  4. {
  5.     ...
  6.     virtual   void  printString_async(
  7.         const  ::Demo::AMD_Printer_printStringPtr&,
  8.         const  ::std::string&,  const  ::Ice::Current& = ::Ice::Current()) = 0;
  9.     ...
  10. };
  11. }

这个printString_async 方法就是我们要实现的异步分派方法,它的第一个参数就是由ICE实现的回调类AMD_Printer_printString ,在这个方法里,我们要两种方案:

  1. 直接做具体工作,完成后在末尾调用回调类的ice_response方法告知客户端已完成。这种方案就和之前普通版的服务端一样,是同步执行的。
  2. 把 回调类和请求所需要的参数放入一个指定的位置,再由其它线程取出执行和通知客户端。这种方案就是异步分派方法,具体实现时还可以有多种方式,如使用命令模 式把参数和具体操作直接封装成一个对象放入队列,然后由另一线程(或线程池)取出执行。后面的示例代码为了简单起见直接使用了Windows API中的线程池功能,而且也没有使用队列。

示例代码

  1. #include <ice/ice.h>
  2. #include "printer.h"
  3.  
  4. using   namespace  std;
  5. using   namespace  Demo;
  6.  
  7. // 传递给线程函数的参数
  8. struct  CallbackEntry{
  9.     AMD_Printer_printStringPtr callback;
  10.     string str;
  11. };
  12.  
  13. // 线程函数
  14. DWORD  WINAPI DoPrintString( LPVOID  lpParameter)
  15. {
  16.     // 取得参数
  17.     CallbackEntry *pCE = (CallbackEntry *)lpParameter;
  18.     // 工作:打印字符(延时1秒模拟长时间操作)
  19.     Sleep(1000);
  20.     cout << pCE->str << endl;   
  21.     // 回调,工作完成。如果工作异常,则调用ice_exception();
  22.     pCE->callback->ice_response();
  23.     // 删除参数(这里使用堆直接传递,其实更好的方法是使用队列)
  24.     delete  pCE;
  25.     return  TRUE;
  26. }
  27.  
  28. struct  PrinterImp : Printer{
  29.     virtual   void  printString_async(
  30.         const  AMD_Printer_printStringPtr &callback,
  31.         const  string& s,  const  Ice::Current&)
  32.     {
  33.         // 参数打包(回调类和pringString方法的参数)
  34.         CallbackEntry *pCE = new  CallbackEntry;
  35.         pCE->callback = callback;
  36.         pCE->str = s;
  37.         // 让Windows线程池来执行具体任务
  38.         ::QueueUserWorkItem(DoPrintString,pCE,WT_EXECUTEDEFAULT);
  39.     }
  40. };
  41.  
  42. class  MyApp :  public  Ice::Application{
  43. public :
  44.     virtual   int  run( int  n,  char * v[]){
  45.         Ice::CommunicatorPtr& ic = communicator();
  46.         Ice::ObjectAdapterPtr adapter
  47.             = ic->createObjectAdapterWithEndpoints("SP" , "default -p 10000" );
  48.         Ice::ObjectPtr object = new  PrinterImp;
  49.         adapter->add(object, ic->stringToIdentity("SimplePrinter" ));
  50.  
  51.         adapter->activate();
  52.         ic->waitForShutdown();
  53.         return  0;
  54.     }
  55. };
  56.  
  57. int  main( int  argc,  char * argv[])
  58. {
  59.     MyApp app;
  60.     return  app.main(argc, argv);
  61. }

客户端不需要改变,编译运行服务器然后用客户端测试效果。(其实效果不是很明显,因为AMD提高的是服务器的负荷能力)

0 0
原创粉丝点击