use of org.apache.mina.core.future.WriteFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method writeRequest.
/**
* a reusable code block to be used in various bind methods
*/
private void writeRequest(Request request) throws LdapException {
// Send the request to the server
WriteFuture writeFuture = ldapSession.write(request);
long localTimeout = timeout;
while (localTimeout > 0) {
// Wait only 100 ms
boolean done = writeFuture.awaitUninterruptibly(100);
if (done) {
return;
}
// Wait for the message to be sent to the server
if (!ldapSession.isConnected()) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03207_SOMETHING_WRONG_HAPPENED));
}
Exception exception = (Exception) ldapSession.removeAttribute(EXCEPTION_KEY);
if (exception != null) {
if (exception instanceof LdapException) {
throw (LdapException) exception;
} else {
throw new InvalidConnectionException(exception.getMessage(), exception);
}
}
throw new InvalidConnectionException("Error while sending some message : the session has been closed");
}
localTimeout -= 100;
}
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03208_TIMEOUT));
}
throw new LdapException(TIME_OUT_ERROR);
}
use of org.apache.mina.core.future.WriteFuture in project camel by apache.
the class Mina2Helper 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);
if (!future.awaitUninterruptibly(10000L)) {
String message = "Cannot write body: " + body.getClass().getCanonicalName() + " using session: " + session;
if (future.getException() != null) {
throw new CamelExchangeException(message, exchange, future.getException());
} else {
throw new CamelExchangeException(message, exchange);
}
}
}
use of org.apache.mina.core.future.WriteFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method unBind.
// ------------------------ The LDAP operations ------------------------//
// Unbind operations //
// ---------------------------------------------------------------------//
/**
* {@inheritDoc}
*/
@Override
public void unBind() throws LdapException {
// If the session has not been establish, or is closed, we get out immediately
checkSession();
// Creates the messageID and stores it into the
// initial message and the transmitted message.
int newId = messageId.incrementAndGet();
// Create the UnbindRequest
UnbindRequest unbindRequest = new UnbindRequestImpl();
unbindRequest.setMessageId(newId);
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03233_SENDING_UNBIND, unbindRequest));
}
// Send the request to the server
// Use this for logging instead: WriteFuture unbindFuture = ldapSession.write( unbindRequest )
WriteFuture unbindFuture = ldapSession.write(unbindRequest);
unbindFuture.awaitUninterruptibly(timeout);
authenticated.set(false);
// Close all the Future for this session
for (ResponseFuture<? extends Response> responseFuture : futureMap.values()) {
responseFuture.cancel();
}
// clear the mappings
clearMaps();
// We now have to close the session
try {
close();
} catch (IOException e) {
LOG.error(e.getMessage());
throw new LdapException(e.getMessage());
}
connected.set(false);
// Last, not least, reset the MessageId value
messageId.set(0);
// And get out
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03234_UNBINDSUCCESSFUL));
}
}
use of org.apache.mina.core.future.WriteFuture in project streamsx.topology by IBMStreams.
the class TesterSink method processBatch.
@Override
protected boolean processBatch(Queue<BatchedTuple> batch) throws Exception {
List<WriteFuture> futures = new ArrayList<>(batch.size());
for (BatchedTuple bt : batch) {
int portIndex = bt.getStream().getPortNumber();
TCPTestClient client = clients[portIndex];
BinaryEncoding be = encoders[portIndex];
byte[] tupleData = new byte[(int) be.getEncodedSize(bt.getTuple())];
be.encodeTuple(bt.getTuple(), ByteBuffer.wrap(tupleData));
TestTuple tt = new TestTuple(portIndex, tupleData);
futures.add(client.writeTuple(tt));
}
for (WriteFuture future : futures) {
future.await();
}
return false;
}
use of org.apache.mina.core.future.WriteFuture in project zm-mailbox by Zimbra.
the class NioOutputStream method writeToSession.
private synchronized void writeToSession(Object output) throws IOException {
long writeBytes = session.getScheduledWriteBytes();
WriteFuture future = session.write(output);
if (writeBytes > maxScheduledBytes) {
ZimbraLog.nio.debug("IOSession has %d scheduled write bytes; waiting for buffer to catch up", writeBytes);
long start = System.currentTimeMillis();
if (maxWritePause > 0) {
boolean done = future.awaitUninterruptibly(maxWritePause);
if (!done) {
throw new IOException("Write stalled, client may have gone away");
}
} else {
future.awaitUninterruptibly();
}
if (ZimbraLog.nio.isDebugEnabled()) {
ZimbraLog.nio.debug("waited %d for %d scheduled bytes", (System.currentTimeMillis() - start), writeBytes);
ZimbraLog.nio.debug("now have %d scheduled bytes, %d messages; %d written bytes %d messages", session.getScheduledWriteBytes(), session.getScheduledWriteMessages(), session.getWrittenBytes(), session.getWrittenMessages());
}
}
}
Aggregations