本文整理汇总了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);
}
}
});
}
示例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());
}
}
});
}
示例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);
}
}
});
}
}
示例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);
}
});
}