本文整理汇总了Java中org.apache.mina.common.IoFuture类的典型用法代码示例。如果您正苦于以下问题:Java IoFuture类的具体用法?Java IoFuture怎么用?Java IoFuture使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IoFuture类属于org.apache.mina.common包,在下文中一共展示了IoFuture类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
import org.apache.mina.common.IoFuture; //导入依赖的package包/类
/**
* Kick off a connection attempt.
*/
synchronized void connect ()
{
closing = false;
cancelAsyncConnect ();
connector.connect
(remoteAddress, this, connectorConfig).addListener
(new IoFutureListener ()
{
public void operationComplete (IoFuture future)
{
connectFutureComplete (future);
}
});
}
示例2: connectFutureComplete
import org.apache.mina.common.IoFuture; //导入依赖的package包/类
/**
* Called by future created by connect () when complete.
*/
protected void connectFutureComplete (IoFuture future)
{
if (closing)
return;
try
{
if (!future.isReady ())
{
diagnostic ("Connection attempt to federator at " + uri +
" timed out, retrying", this);
asyncConnect ();
} else
{
open (future.getSession ());
}
} catch (RuntimeIOException ex)
{
diagnostic ("Failed to connect to federator at " + uri + ", retrying: " +
shortException (ex.getCause ()), this, ex);
asyncConnect ();
}
}
示例3: sendRequest
import org.apache.mina.common.IoFuture; //导入依赖的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);
}
}
});
}
示例4: sendErrorResponse
import org.apache.mina.common.IoFuture; //导入依赖的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());
}
}
});
}
示例5: run
import org.apache.mina.common.IoFuture; //导入依赖的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);
}
}
});
}
}
示例6: invokeAsync
import org.apache.mina.common.IoFuture; //导入依赖的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);
}
});
}