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