public void handle(final Task task) {
// Iterate through existing connections and try to determine
// whether any of them have been removed from the network.
Iterator<InputConnection> iter = connections.iterator();
while (iter.hasNext()) {
final InputConnection connection = iter.next();
boolean exists = false;
for (InputConnectionContext input : update.connections()) {
if (input.address().equals(connection.address())) {
exists = true;
break;
}
}
// If a connection was removed from the network, close
// and remove the connection regardless of whether the
// close is actually successful.
if (!exists) {
connection.close(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> result) {
if (result.failed()) {
log.error(String.format("%s - Failed to close input connection: %s", DefaultInputPort.this, connection));
} else {
log.info(String.format("%s - Closed input connection: %s", DefaultInputPort.this, connection));
}
}
});
iter.remove();
}
}
// Now try to determine whether any connections were added to the network.
final List<InputConnection> newConnections = new ArrayList<>();
for (InputConnectionContext input : update.connections()) {
boolean exists = false;
for (InputConnection connection : connections) {
if (connection.address().equals(input.address())) {
exists = true;
break;
}
}
if (!exists) {
log.debug(String.format("%s - Creating connection: %s", DefaultInputPort.this, input));
newConnections.add(new DefaultInputConnection(vertx, input));
}
}
// If the port is open then that means connections have already
// been set up. We need to add the new connections carefully
// because it's possible that user code may be sending messages
// on the port. If messages are sent to a connection that's not
// yet open then an exception will be thrown.
if (open) {
final CountingCompletionHandler<Void> counter = new CountingCompletionHandler<Void>(newConnections.size());
counter.setHandler(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> result) {
task.complete();
}
});
// Start each connection and add the connection to the connections
// list only once the connection has been successfully opened.
// The update lock by the task runner will ensure that we don't
// accidentally open up two of the same connection even if the
// configuration is updated again.
for (final InputConnection connection : newConnections) {
connection.open(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> result) {
if (result.failed()) {
log.error(String.format("%s - Failed to open input connection: %s", DefaultInputPort.this, connection));
} else {