use of org.apache.mina.core.future.CloseFuture in project camel by apache.
the class Mina2Producer method closeSessionIfNeededAndAwaitCloseInHandler.
private void closeSessionIfNeededAndAwaitCloseInHandler(IoSession sessionToBeClosed) throws InterruptedException {
closeLatch = new CountDownLatch(1);
if (!sessionToBeClosed.isClosing()) {
CloseFuture closeFuture = sessionToBeClosed.closeNow();
closeFuture.await(timeout, TimeUnit.MILLISECONDS);
closeLatch.await(timeout, TimeUnit.MILLISECONDS);
}
}
use of org.apache.mina.core.future.CloseFuture in project camel by apache.
the class Mina2Consumer method doStop.
@Override
protected void doStop() throws Exception {
if (configuration.isClientMode() && configuration.getProtocol().equals("tcp")) {
LOG.info("Disconnect from server address: {} using connector: {}", address, connector);
if (session != null) {
CloseFuture closeFuture = session.closeNow();
closeFuture.awaitUninterruptibly();
}
connector.dispose(true);
} else {
LOG.info("Unbinding from server address: {} using acceptor: {}", address, acceptor);
if (address instanceof InetSocketAddress) {
// need to check if the address is IPV4 all network address
if ("0.0.0.0".equals(((InetSocketAddress) address).getAddress().getHostAddress())) {
LOG.info("Unbind the server address {}", acceptor.getLocalAddresses());
acceptor.unbind(acceptor.getLocalAddresses());
} else {
acceptor.unbind(address);
}
} else {
acceptor.unbind(address);
}
}
super.doStop();
}
use of org.apache.mina.core.future.CloseFuture in project bigbluebutton by bigbluebutton.
the class BlockStreamEventMessageHandler method closeSession.
private void closeSession(IoSession session) {
String room = (String) session.getAttribute(ROOM, null);
if (room != null) {
log.info("Closing session [" + room + "]. ");
} else {
log.info("Cannot determine session to close.");
}
CloseFuture future = session.close(true);
}
use of org.apache.mina.core.future.CloseFuture in project cxf by apache.
the class IoSessionOutputStream method close.
@Override
public void close() throws IOException {
try {
flush();
} finally {
CloseFuture future = session.closeOnFlush();
future.awaitUninterruptibly();
}
}
use of org.apache.mina.core.future.CloseFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method connect.
// -------------------------- The methods ---------------------------//
/**
* {@inheritDoc}
*/
@Override
public boolean connect() throws LdapException {
if ((ldapSession != null) && connected.get()) {
// No need to connect if we already have a connected session
return true;
}
// Create the connector if needed
if (connector == null) {
createConnector();
}
// Build the connection address
SocketAddress address = new InetSocketAddress(config.getLdapHost(), config.getLdapPort());
// And create the connection future
timeout = config.getTimeout();
long maxRetry = System.currentTimeMillis() + timeout;
ConnectFuture connectionFuture = null;
boolean interrupted = false;
while (maxRetry > System.currentTimeMillis() && !interrupted) {
connectionFuture = connector.connect(address);
boolean result = false;
// Wait until it's established
try {
result = connectionFuture.await(timeout);
} catch (InterruptedException e) {
connector.dispose();
connector = null;
LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
interrupted = true;
throw new LdapOtherException(e.getMessage(), e);
} finally {
if (result) {
boolean isConnected = connectionFuture.isConnected();
if (!isConnected) {
Throwable connectionException = connectionFuture.getException();
if ((connectionException instanceof ConnectException) || (connectionException instanceof UnresolvedAddressException)) {
// We know that there was a permanent error such as "connection refused".
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03245_CONNECTION_ERROR, connectionFuture.getException().getMessage()));
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03244_CONNECTION_RETRYING));
}
// Wait 500 ms and retry
try {
Thread.sleep(500);
} catch (InterruptedException e) {
connector = null;
LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
interrupted = true;
throw new LdapOtherException(e.getMessage(), e);
}
} else {
break;
}
}
}
}
if (connectionFuture == null) {
connector.dispose();
throw new InvalidConnectionException("Cannot connect");
}
boolean isConnected = connectionFuture.isConnected();
if (!isConnected) {
// disposing connector if not connected
try {
close();
} catch (IOException ioe) {
// Nothing to do
}
Throwable e = connectionFuture.getException();
if (e != null) {
StringBuilder message = new StringBuilder("Cannot connect to the server: ");
// (most of the time no message is associated with this exception)
if ((e instanceof UnresolvedAddressException) && (e.getMessage() == null)) {
message.append("Hostname '");
message.append(config.getLdapHost());
message.append("' could not be resolved.");
throw new InvalidConnectionException(message.toString(), e);
}
// Default case
message.append(e.getMessage());
throw new InvalidConnectionException(message.toString(), e);
}
return false;
}
// Get the close future for this session
CloseFuture closeFuture = connectionFuture.getSession().getCloseFuture();
// Add a listener to close the session in the session.
closeFuture.addListener(new IoFutureListener<IoFuture>() {
@Override
public void operationComplete(IoFuture future) {
// Process all the waiting operations and cancel them
LOG.debug(I18n.msg(I18n.MSG_03238_NOD_RECEIVED));
for (ResponseFuture<?> responseFuture : futureMap.values()) {
LOG.debug(I18n.msg(I18n.MSG_03235_CLOSING, responseFuture));
responseFuture.cancel();
try {
if (responseFuture instanceof AddFuture) {
((AddFuture) responseFuture).set(AddNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof BindFuture) {
((BindFuture) responseFuture).set(BindNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof CompareFuture) {
((CompareFuture) responseFuture).set(CompareNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof DeleteFuture) {
((DeleteFuture) responseFuture).set(DeleteNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ExtendedFuture) {
((ExtendedFuture) responseFuture).set(ExtendedNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ModifyFuture) {
((ModifyFuture) responseFuture).set(ModifyNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ModifyDnFuture) {
((ModifyDnFuture) responseFuture).set(ModifyDnNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof SearchFuture) {
((SearchFuture) responseFuture).set(SearchNoDResponse.PROTOCOLERROR);
}
} catch (InterruptedException e) {
LOG.error(I18n.err(I18n.ERR_03202_ERROR_PROCESSING_NOD, responseFuture), e);
}
futureMap.remove(messageId.get());
}
futureMap.clear();
}
});
// Get back the session
ldapSession = connectionFuture.getSession();
connected.set(true);
// Store the container into the session if we don't have one
@SuppressWarnings("unchecked") LdapMessageContainer<MessageDecorator<? extends Message>> container = (LdapMessageContainer<MessageDecorator<? extends Message>>) ldapSession.getAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR);
if (container != null) {
if ((schemaManager != null) && !(container.getBinaryAttributeDetector() instanceof SchemaBinaryAttributeDetector)) {
container.setBinaryAttributeDetector(new SchemaBinaryAttributeDetector(schemaManager));
}
} else {
BinaryAttributeDetector atDetector = new DefaultConfigurableBinaryAttributeDetector();
if (schemaManager != null) {
atDetector = new SchemaBinaryAttributeDetector(schemaManager);
}
ldapSession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, new LdapMessageContainer<MessageDecorator<? extends Message>>(codec, atDetector));
}
// Initialize the MessageId
messageId.set(0);
// And return
return true;
}
Aggregations