当前位置: 首页>>代码示例>>Java>>正文


Java WriteFuture.addListener方法代码示例

本文整理汇总了Java中org.apache.mina.common.WriteFuture.addListener方法的典型用法代码示例。如果您正苦于以下问题:Java WriteFuture.addListener方法的具体用法?Java WriteFuture.addListener怎么用?Java WriteFuture.addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.mina.common.WriteFuture的用法示例。


在下文中一共展示了WriteFuture.addListener方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendRequest

import org.apache.mina.common.WriteFuture; //导入方法依赖的package包/类
public void sendRequest(final RequestWrapper wrapper, final int timeout) throws Exception {
  final long beginTime = System.currentTimeMillis();
  WriteFuture writeFuture = session.write(wrapper);
  final Client self = this;
  writeFuture.addListener(new IoFutureListener() {
    public void operationComplete(IoFuture future) {
      WriteFuture wfuture = (WriteFuture) future;
      if (wfuture.isWritten()) {
        return;
      }
      String error = "send message to server: " + session.getRemoteAddress()
          + " error,maybe because sendbuffer is full or connection closed: " + !session.isConnected();
      if (System.currentTimeMillis() - beginTime >= timeout) {
        error = "write message to os send buffer timeout,consumetime is: " + (System.currentTimeMillis() - beginTime) + "ms,timeout is:"
            + timeout;
      }
      LOGGER.error(error);
      ResponseWrapper response = new ResponseWrapper(wrapper.getId(), wrapper.getCodecType(), wrapper.getProtocolType());
      response.setException(new Exception(error));
      try {
        putResponse(response);
      } catch (Exception e) {
        // IGNORE, should not happen
      }
      if (session.isConnected()) {
        if (isWarnEnabled) {
          LOGGER.warn("close the session because send request error,server:" + session.getRemoteAddress());
        }
        session.close();
      } else {
        // TODO: exception handle
        MinaClientFactory.getInstance().removeClient(key, self);
      }
    }
  });
}
 
开发者ID:leeyazhou,项目名称:nfs-rpc,代码行数:37,代码来源:MinaClient.java

示例2: sendErrorResponse

import org.apache.mina.common.WriteFuture; //导入方法依赖的package包/类
private void sendErrorResponse(final IoSession session, final RequestWrapper request) {
  ResponseWrapper responseWrapper = new ResponseWrapper(request.getId(), request.getCodecType(), request.getProtocolType());
  responseWrapper.setException(new Exception("server threadpool full,maybe because server is slow or too many requests"));
  WriteFuture wf = session.write(responseWrapper);
  wf.addListener(new IoFutureListener() {
    public void operationComplete(IoFuture future) {
      if (!((WriteFuture) future).isWritten()) {
        LOGGER.error("server write response error,request id is: " + request.getId());
      }
    }
  });
}
 
开发者ID:leeyazhou,项目名称:nfs-rpc,代码行数:13,代码来源:MinaServerHandler.java

示例3: run

import org.apache.mina.common.WriteFuture; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
public void run() {
  // pipeline
  if (message instanceof List) {
    List messages = (List) message;
    for (Object messageObject : messages) {
      threadPool.execute(new HandlerRunnable(session, messageObject, threadPool));
    }
  } else {
    RequestWrapper request = (RequestWrapper) message;
    long beginTime = System.currentTimeMillis();
    ResponseWrapper responseWrapper = ProtocolFactory.getServerHandler(request.getProtocolType()).handleRequest(request);
    final int id = request.getId();
    // already timeout,so not return
    if ((System.currentTimeMillis() - beginTime) >= request.getTimeout()) {
      LOGGER.warn("timeout,so give up send response to client,requestId is:" + id + ",client is:" + session.getRemoteAddress()
          + ",consumetime is:" + (System.currentTimeMillis() - beginTime) + ",timeout is:" + request.getTimeout());
      return;
    }
    WriteFuture wf = session.write(responseWrapper);
    wf.addListener(new IoFutureListener() {
      public void operationComplete(IoFuture future) {
        if (!((WriteFuture) future).isWritten()) {
          LOGGER.error("server write response error,request id is: " + id);
        }
      }
    });
  }
}
 
开发者ID:leeyazhou,项目名称:nfs-rpc,代码行数:30,代码来源:MinaServerHandler.java

示例4: invokeAsync

import org.apache.mina.common.WriteFuture; //导入方法依赖的package包/类
public void invokeAsync(final BasePacket packet, final long timeout,ResponseListener listener){
	if(isDebugEnabled){
		LOGGER.debug("send request ["+packet.getChid()+"] async,time is:"+System.currentTimeMillis());
	}
	if(minTimeout>timeout){
		minTimeout=timeout;
	}
	final ResponseCallbackTask callbackTask=new ResponseCallbackTask(packet.getChid(),listener, this.session, timeout);
	callbackTasks.put(packet.getChid(), callbackTask);
	
	ByteBuffer bb = packet.getByteBuffer();
	bb.flip();
	byte[] data = new byte[bb.remaining()];
	bb.get(data);
	WriteFuture writeFuture=session.write(data);
	writeFuture.addListener(new IoFutureListener(){

		public void operationComplete(IoFuture future) {
			WriteFuture wfuture=(WriteFuture)future;
			if(wfuture.isWritten()){
				return;
			}
			String error = "send message to tair server error [" + packet.getChid() + "], tair server: " + session.getRemoteAddress()+", maybe because this connection closed :"+ !session.isConnected();
            LOGGER.warn(error);
            callbackTask.setResponse(new TairClientException(error));
            
			// close this session
			if(session.isConnected())
				session.close();
			else
				clientFactory.removeClient(key);
		}
		
	});
}
 
开发者ID:alibaba,项目名称:tair-java-client,代码行数:36,代码来源:TairClient.java


注:本文中的org.apache.mina.common.WriteFuture.addListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。