use of com.alibaba.nacos.common.notify.listener.Subscriber in project nacos by alibaba.
the class NamingEventPublisher method handleEvent.
private void handleEvent(Event event) {
Class<? extends Event> eventType = event.getClass();
Set<Subscriber<? extends Event>> subscribers = subscribes.get(eventType);
if (null == subscribers) {
if (Loggers.EVT_LOG.isDebugEnabled()) {
Loggers.EVT_LOG.debug("[NotifyCenter] No subscribers for slow event {}", eventType.getName());
}
return;
}
for (Subscriber subscriber : subscribers) {
notifySubscriber(subscriber, event);
}
}
use of com.alibaba.nacos.common.notify.listener.Subscriber in project nacos by alibaba.
the class NotifyCenterTest method testNoIgnoreExpireEvent.
@Test
public void testNoIgnoreExpireEvent() throws Exception {
NotifyCenter.registerToPublisher(NoExpireEvent.class, 16);
final AtomicInteger count = new AtomicInteger(0);
NotifyCenter.registerSubscriber(new Subscriber() {
@Override
public void onEvent(Event event) {
count.incrementAndGet();
latch2.countDown();
}
@Override
public Class<? extends Event> subscribeType() {
return NoExpireEvent.class;
}
});
for (int i = 0; i < 3; i++) {
Assert.assertTrue(NotifyCenter.publishEvent(new NoExpireEvent()));
}
latch2.await(10000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(3, count.get());
}
use of com.alibaba.nacos.common.notify.listener.Subscriber in project nacos by alibaba.
the class InternalConfigChangeNotifierTest method testOnEvent.
@Test
public void testOnEvent() {
final String groupKey = GroupKey2.getKey("nacos.internal.tps.control_rule_1", "nacos", "tenant");
final String limitGroupKey = GroupKey2.getKey("nacos.internal.tps.nacos.internal.connection.limit.rule", "nacos", "tenant");
NotifyCenter.registerSubscriber(new Subscriber() {
@Override
public void onEvent(Event event) {
ConnectionLimitRuleChangeEvent connectionLimitRuleChangeEvent = (ConnectionLimitRuleChangeEvent) event;
Assert.assertEquals("content", connectionLimitRuleChangeEvent.getLimitRule());
}
@Override
public Class<? extends Event> subscribeType() {
return ConnectionLimitRuleChangeEvent.class;
}
});
NotifyCenter.registerSubscriber(new Subscriber() {
@Override
public void onEvent(Event event) {
TpsControlRuleChangeEvent tpsControlRuleChangeEvent = (TpsControlRuleChangeEvent) event;
Assert.assertEquals("content", tpsControlRuleChangeEvent.getRuleContent());
}
@Override
public Class<? extends Event> subscribeType() {
return TpsControlRuleChangeEvent.class;
}
});
internalConfigChangeNotifier.onEvent(new LocalDataChangeEvent(groupKey));
internalConfigChangeNotifier.onEvent(new LocalDataChangeEvent(limitGroupKey));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of com.alibaba.nacos.common.notify.listener.Subscriber in project nacos by alibaba.
the class ConfigChangePublisherTest method testConfigChangeNotify.
@Test
public void testConfigChangeNotify() throws InterruptedException {
AtomicReference<ConfigDataChangeEvent> reference = new AtomicReference<>();
NotifyCenter.registerToPublisher(ConfigDataChangeEvent.class, NotifyCenter.ringBufferSize);
NotifyCenter.registerSubscriber(new Subscriber() {
@Override
public void onEvent(Event event) {
reference.set((ConfigDataChangeEvent) event);
}
@Override
public Class<? extends Event> subscribeType() {
return ConfigDataChangeEvent.class;
}
});
// nacos is standalone mode and use embedded storage
EnvUtil.setIsStandalone(true);
PropertyUtil.setEmbeddedStorage(true);
ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
Thread.sleep(2000);
Assert.assertNotNull(reference.get());
reference.set(null);
// nacos is standalone mode and use external storage
EnvUtil.setIsStandalone(true);
PropertyUtil.setEmbeddedStorage(false);
ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
Thread.sleep(2000);
Assert.assertNotNull(reference.get());
reference.set(null);
// nacos is cluster mode and use embedded storage
EnvUtil.setIsStandalone(false);
PropertyUtil.setEmbeddedStorage(true);
ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
Thread.sleep(2000);
Assert.assertNull(reference.get());
reference.set(null);
// nacos is cluster mode and use external storage
EnvUtil.setIsStandalone(false);
PropertyUtil.setEmbeddedStorage(false);
ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
Thread.sleep(2000);
Assert.assertNotNull(reference.get());
reference.set(null);
}
use of com.alibaba.nacos.common.notify.listener.Subscriber in project nacos by alibaba.
the class DefaultSharePublisher method receiveEvent.
@Override
public void receiveEvent(Event event) {
final long currentEventSequence = event.sequence();
// get subscriber set based on the slow EventType.
final Class<? extends SlowEvent> slowEventType = (Class<? extends SlowEvent>) event.getClass();
// Get for Map, the algorithm is O(1).
Set<Subscriber> subscribers = subMappings.get(slowEventType);
if (null == subscribers) {
LOGGER.debug("[NotifyCenter] No subscribers for slow event {}", slowEventType.getName());
return;
}
// Notification single event subscriber
for (Subscriber subscriber : subscribers) {
// Whether to ignore expiration events
if (subscriber.ignoreExpireEvent() && lastEventSequence > currentEventSequence) {
LOGGER.debug("[NotifyCenter] the {} is unacceptable to this subscriber, because had expire", event.getClass());
continue;
}
// Notify single subscriber for slow event.
notifySubscriber(subscriber, event);
}
}
Aggregations