本文整理汇总了Java中org.apache.mina.common.WriteFuture类的典型用法代码示例。如果您正苦于以下问题:Java WriteFuture类的具体用法?Java WriteFuture怎么用?Java WriteFuture使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WriteFuture类属于org.apache.mina.common包,在下文中一共展示了WriteFuture类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: send
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
public void send(Object message, boolean sent) throws RemotingException {
super.send(message, sent);
boolean success = true;
int timeout = 0;
try {
WriteFuture future = session.write(message);
if (sent) {
timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
success = future.join(timeout);
}
} catch (Throwable e) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}
if(!success) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
+ "in timeout(" + timeout + "ms) limit");
}
}
示例2: send
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
public void send(Object message, boolean sent) throws RemotingException {
if (isClosed()) {
throw new RemotingException(this, "Failed to send message "
+ (message == null ? "" : message.getClass().getName()) + ":" + message
+ ", cause: Channel closed. channel: " + getLocalAddress() + " -> " + getRemoteAddress());
}
boolean success = true;
int timeout = 0;
try {
WriteFuture future = session.write(message);
if (sent) {
timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
success = future.join(timeout);
}
} catch (Throwable e) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}
if(!success) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
+ "in timeout(" + timeout + "ms) limit");
}
}
示例3: send
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
public void send(Object message, boolean sent) throws RemotingException {
super.send(message, sent);
boolean success = true;
int timeout = 0;
try {
WriteFuture future = session.write(message);
if (sent) {
timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
success = future.join(timeout);
}
} catch (Throwable e) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}
if (!success) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
+ "in timeout(" + timeout + "ms) limit");
}
}
示例4: write
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
@Override
public WriteFuture write(Object o) {
try {
writeLock.lock();
if (throwExceptionOnNextWrite) {
throw new RuntimeException();
}
DefaultWriteFuture f = new DefaultWriteFuture(this);
f.setWritten(!closed);
if (!closed) {
written.add(o);
if (latch != null) {
latch.countDown();
}
}
return f;
}
finally {
writeLock.unlock();
}
}
示例5: write
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
@Override
public WriteFuture write(Object message) {
DefaultWriteFuture ret = new DefaultWriteFuture(this);
try {
otherEnd.messageReceived(nextFilter, otherSession, message);
ret.setWritten(true);
}
catch (Exception e) {
e.printStackTrace();
ret.setWritten(false);
}
// behave like the real thing..
if (attributes.get(SSLFilter.DISABLE_ENCRYPTION_ONCE) != null) {
attributes.remove(SSLFilter.DISABLE_ENCRYPTION_ONCE);
}
return ret;
}
示例6: 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);
}
}
});
}
示例7: 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());
}
}
});
}
示例8: 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);
}
}
});
}
}
示例9: 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);
}
});
}
示例10: writeBody
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
/**
* Asynchronously writes the given body to MINA session. Will wait at most for
* 10 seconds until the body has been written.
*
* @param session the MINA session
* @param body the body to write (send)
* @param exchange the exchange
* @throws CamelExchangeException is thrown if the body could not be written for some reasons
* (eg remote connection is closed etc.)
*/
public static void writeBody(IoSession session, Object body, Exchange exchange) throws CamelExchangeException {
// the write operation is asynchronous. Use WriteFuture to wait until the session has been written
WriteFuture future = session.write(body);
// must use a timeout (we use 10s) as in some very high performance scenarios a write can cause
// thread hanging forever
LOG.trace("Waiting for write to complete for body: {} using session: {}", body, session);
future.join(10000L);
if (!future.isWritten()) {
throw new CamelExchangeException("Cannot write body: " + body + " using session: " + session, exchange);
}
}
示例11: toggleCustomBGState
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
public void toggleCustomBGState() {
getQuestNAdd(MapleQuest.getInstance(CUSTOM_BG)).setCustomData(String.valueOf(getCustomBGState() == 1 ? 0 : 1));
for (byte i = 0; i < 127; i++) {
WriteFuture write = client.getSession().write(CField.removeBGLayer((getCustomBGState() == 1), 0, i, 0));
//duration 0 = forever map 0 = current map
}
}
示例12: newSession
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
public static IoSession newSession(byte version) {
final IoSession mockSession = mock(IoSession.class);
when(mockSession.getAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME)).thenReturn(version);
final WriteFuture mockFuture = mock(WriteFuture.class);
when(mockSession.write(any())).thenReturn(mockFuture);
return mockSession;
}
示例13: write
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
@Override
public WriteFuture write(Object message) {
allValuesWritten.add(message);
DefaultWriteFuture future = new DefaultWriteFuture(this);
future.setWritten(true);
return future;
}
示例14: send
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
/**
* Send a message with logging when trace enabled.
*
* @param session The IO session.
* @param serverDomain The server domain.
* @param message The message.
*
* @return The IO future.
*/
public static WriteFuture send (IoSession session, String serverDomain,
Message message)
{
if (shouldLog (TRACE))
{
trace ("Federation session " + idFor (session) + " sent " +
message.name (), Federation.class); }
return session.write (message);
}
示例15: send
import org.apache.mina.common.WriteFuture; //导入依赖的package包/类
private static WriteFuture send (IoSession session, Message message)
{
if (shouldLog (TRACE))
{
trace ("Server sent message to " + Text.idFor (session) + ": " + message,
Router.class);
}
return session.write (message);
}