use of com.alipay.sofa.registry.client.api.Subscriber in project sofa-rpc by sofastack.
the class SofaRegistry method unSubscribe.
@Override
public void unSubscribe(ConsumerConfig config) {
String serviceName = SofaRegistryHelper.buildListDataId(config, config.getProtocol());
String appName = config.getAppName();
Subscriber subscriber = subscribers.get(serviceName);
if (subscriber != null) {
SofaRegistrySubscribeCallback callback = (SofaRegistrySubscribeCallback) subscriber.getDataObserver();
callback.remove(serviceName, config);
if (callback.getListenerNum() == 0) {
// 已经没人订阅这个data Key了
SofaRegistryClient.getRegistryClient(appName, registryConfig).unregister(serviceName, subscriber.getGroup(), RegistryType.SUBSCRIBER);
subscribers.remove(serviceName);
// 已经没人订阅这个config Key了
SofaRegistryClient.getRegistryClient(appName, registryConfig).unregister(serviceName, subscriber.getGroup(), RegistryType.CONFIGURATOR);
configurators.remove(serviceName);
}
}
}
use of com.alipay.sofa.registry.client.api.Subscriber in project sofa-rpc by sofastack.
the class SofaRegistrySubscribeCallbackTest method handleData.
@Test
public void handleData() throws Exception {
Subscriber listSub = new MockSubscribe(5);
Configurator attrSub = new MockConfigurator(2);
final AtomicInteger ps = new AtomicInteger(0);
ProviderInfoListener listener = new ProviderInfoListener() {
@Override
public void addProvider(ProviderGroup providerGroup) {
ps.addAndGet(providerGroup.size());
}
@Override
public void removeProvider(ProviderGroup providerGroup) {
ps.addAndGet(-providerGroup.size());
}
@Override
public void updateProviders(ProviderGroup providerGroup) {
ps.set(providerGroup.size());
}
@Override
public void updateAllProviders(List<ProviderGroup> providerGroups) {
ps.set(0);
for (ProviderGroup providerGroup : providerGroups) {
ps.addAndGet(providerGroup.size());
}
}
};
SofaRegistrySubscribeCallback callback = new SofaRegistrySubscribeCallback();
callback.addProviderInfoListener("xxxxx", new ConsumerConfig(), listener);
Assert.assertTrue((!callback.flag[0].get()) && (!callback.flag[1].get()));
callback.handleData("xxxxx", buildConfigPs(2));
try {
Thread.sleep(200);
} finally {
}
Assert.assertTrue(callback.flag[1].get());
Assert.assertTrue(ps.get() == 0);
callback.handleData("xxxxx", buildPs(5));
try {
Thread.sleep(200);
} finally {
}
Assert.assertTrue(callback.flag == null);
// default+localZone
Assert.assertEquals(ps.get(), 5 + 5);
callback = new SofaRegistrySubscribeCallback();
ps.set(0);
callback.addProviderInfoListener("yyyyy", new ConsumerConfig(), listener);
callback.handleData("yyyyy", buildPs(5));
try {
Thread.sleep(200);
} finally {
}
callback.handleData("yyyyy", buildConfigPs(2));
try {
Thread.sleep(200);
} finally {
}
Assert.assertTrue(callback.flag == null);
Assert.assertEquals(ps.get(), 5 + 5);
}
Aggregations