Search in sources :

Example 1 with WriteFuture

use of org.apache.mina.common.WriteFuture in project camel by apache.

the class MinaHelper method writeBody.

/**
     * 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);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) WriteFuture(org.apache.mina.common.WriteFuture)

Example 2 with WriteFuture

use of org.apache.mina.common.WriteFuture in project dubbo by alibaba.

the class MinaChannel method send.

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");
    }
}
Also used : WriteFuture(org.apache.mina.common.WriteFuture) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Aggregations

WriteFuture (org.apache.mina.common.WriteFuture)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1