com.netflix.eureka2.utils.StreamedDataCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eureka-core Show documentation
Show all versions of eureka-core Show documentation
eureka-core developed by Netflix
The newest version!
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.eureka2.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;
import com.netflix.eureka2.interests.ChangeNotification;
import com.netflix.eureka2.registry.InstanceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Func1;
/**
* @author Tomasz Bak
*/
public abstract class StreamedDataCollector {
public abstract List latestSnapshot();
public abstract void close();
public static StreamedDataCollector from(Collection collection) {
final List copy = Collections.unmodifiableList(new ArrayList(collection));
return new StreamedDataCollector() {
@Override
public List latestSnapshot() {
return copy;
}
@Override
public void close() {
// No-op
}
};
}
public static StreamedDataCollector from(Observable> notifications,
Func1 converter) {
return new ChangeNotificationCollector<>(notifications, converter);
}
static class ChangeNotificationCollector extends StreamedDataCollector {
private static final Logger logger = LoggerFactory.getLogger(ChangeNotificationCollector.class);
private final ConcurrentSkipListSet servers = new ConcurrentSkipListSet<>();
private final Subscription subscription;
ChangeNotificationCollector(Observable> notifications,
final Func1 converter) {
subscription = notifications.subscribe(new Subscriber>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
logger.error("Change notification stream terminated with error", e);
}
@Override
public void onNext(ChangeNotification notification) {
R converted = converter.call(notification.getData());
switch (notification.getKind()) {
case Add:
case Modify:
servers.add(converted);
break;
case Delete:
servers.remove(converted);
}
}
});
}
@Override
public List latestSnapshot() {
if (subscription.isUnsubscribed()) {
throw new IllegalStateException("change notification stream is closed");
}
return new ArrayList(servers);
}
@Override
public void close() {
subscription.unsubscribe();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy