public void update(OutputPortContext context) {
log.debug(String.format("%s - Out port configuration has changed, updating streams", this));
// Copy the context in order to ensure that future changes via the
// observer will not effect this update.
final OutputPortContext update = context.copy();
// All updates are run sequentially to prevent race conditions
// during configuration changes. Without essentially locking the
// object, it could be possible that connections are simultaneously
// added and removed or opened and closed on the object.
tasks.runTask(new Handler<Task>() {
@Override
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;