ICE 学习进阶2-AMI 方式


1. AMI :

When a client issues an AMI request, the Ice run time hands the message off to the local transport buffer or, if the buffer is currently full, queues the request for later delivery. The application can then continue its activities and poll or wait for completion of the invocation, or receive a callback when the invocation completes.
AMI is transparent to the server: there is no way for the server to tell whether a client sent a request synchronously or asynchronously.

2. ICE 支持AMI方式,即为每个接口Func,自动增加begin_Func() 和 end_Func()两个接口,如下示例:

  Ice::AsyncResultPtr r = APrintPrx->begin_PrintString("hello,world"); // 不阻塞
  APrintPrx->end_PrintString(r); //因为PrintString没有返回值  //阻塞


Note that begin_getName returns a value of type AsyncResultPtr.The AsyncResult associated with this smart pointer contains the state that the Ice run time requires to keep track of the asynchronous invocation. You must pass the AsyncResultPtr that is returned by the begin_ method to the corresponding end_ method.
The begin_ method has one parameter for each in-parameter of the corresponding Slice operation. Similarly, the end_ method has one out-parameter for each out-parameter of the corresponding Slice operation (plus the AsyncResultPtr
parameter
).
begin_函数和end_函数要捕获 CommunicatorDestroyedException.异常; 而且end_函数还有捕获函数执行的异常

3. AsyncResult 类介绍
The AsyncResult that is returned by the begin_ method encapsulates the state of the asynchronous invocation,有如下几个接口
waitForCompleted(): This method blocks the caller until the result of an invocation becomes available.

void waitForSent() :   This method blocks the calling thread until a request has been written to the client-side transport

 CommunicatorPtr getCommunicator() const :This method returns the communicator that sent the invocation.
 virtual ConnectionPtr getConnection() const :This method returns the connection that was used for the invocation.
virtual ObjectPrx getProxy() const :This method returns the proxy that was used to call the begin_ method. 

 const string& getOperation() const :This method returns the name of the operation.
 LocalObjectPtr getCookie() const:This method returns the cookie that was passed to the begin_ method ,If you did not pass a cookie to the begin_ method, the return value is null.

bool isCompleted() const:This method returns true if, at the time it is called, the result of an invocation is available,indicating that a call to the end_ method will not block the caller.Otherwise, if the result is not yet available, the method returns false.

 

bool sentSynchronously() const:This method returns true if a request was written to the client-side transport without first being queued. If the request was initially queued, sentSynchronously returns false

 

bool isSent() const: isSent returns true if, at the time it is called, the request has been written to the local transport (whether it was initially queued or not). Otherwise, if the
request is still queued, isSent returns false.

4 一个异步调用,提高系统性能的例子:

//

FileHandle file = open(...);
FileTransferPrx ft = ...;
const int chunkSize = ...;
Ice::Int offset = 0;

list<Ice::AsyncResultPtr> results;
const int numRequests = 5;

while (!file.eof()) {
ByteSeq bs;
bs = file.read(chunkSize);

// Send up to numRequests + 1 chunks asynchronously.
Ice::AsyncResultPtr r = ft->begin_send(offset, bs);
offset += bs.size();

// Wait until this request has been passed to the transport.
r->waitForSent();
results.push_back(r);

// Once there are more than numRequests, wait for the least
// recent one to complete.
while (results.size() > numRequests) {
Ice::AsyncResultPtr r = results.front();
results.pop_front();
r->waitForCompleted();
}
}

// Wait for any remaining requests to complete.
while (!results.empty()) {
Ice::AsyncResultPtr r = results.front();
results.pop_front();
r->waitForCompleted();
}

/

 

 

5. 将异步请求,批量请求

Applications that send batched requests (see Section 32.16) can either flush a
batch explicitly or allow the Ice run time to flush automatically. The proxy method
ice_flushBatchRequests performs an immediate flush using the synchronous
invocation model and may block the calling thread until the entire message
can be sent. Ice also provides asynchronous versions of this method so you can
flush batch requests asynchronously.
begin_ice_flushBatchRequests and
end_ice_flushBatchRequests are proxy methods that flush any batch
requests queued by that proxy.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值