Search in sources :

Example 1 with Subscriber

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);
    }
}
Also used : Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) Event(com.alibaba.nacos.common.notify.Event)

Example 2 with Subscriber

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) SmartSubscriber(com.alibaba.nacos.common.notify.listener.SmartSubscriber) Test(org.junit.Test)

Example 3 with Subscriber

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();
    }
}
Also used : TpsControlRuleChangeEvent(com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent) Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) ConnectionLimitRuleChangeEvent(com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent) TpsControlRuleChangeEvent(com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent) ConnectionLimitRuleChangeEvent(com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent) LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) Event(com.alibaba.nacos.common.notify.Event) LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) Test(org.junit.Test)

Example 4 with Subscriber

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);
}
Also used : Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) Event(com.alibaba.nacos.common.notify.Event) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) Test(org.junit.Test)

Example 5 with Subscriber

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);
    }
}
Also used : Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber)

Aggregations

Subscriber (com.alibaba.nacos.common.notify.listener.Subscriber)5 Event (com.alibaba.nacos.common.notify.Event)3 Test (org.junit.Test)3 SmartSubscriber (com.alibaba.nacos.common.notify.listener.SmartSubscriber)1 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)1 LocalDataChangeEvent (com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent)1 TpsControlRuleChangeEvent (com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent)1 ConnectionLimitRuleChangeEvent (com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1