Search in sources :

Example 1 with AbstractEventListener

use of com.alibaba.nacos.api.naming.listener.AbstractEventListener in project nacos by alibaba.

the class InstancesChangeNotifier method onEvent.

@Override
public void onEvent(InstancesChangeEvent event) {
    String key = ServiceInfo.getKey(NamingUtils.getGroupedName(event.getServiceName(), event.getGroupName()), event.getClusters());
    ConcurrentHashSet<EventListener> eventListeners = listenerMap.get(key);
    if (CollectionUtils.isEmpty(eventListeners)) {
        return;
    }
    for (final EventListener listener : eventListeners) {
        final com.alibaba.nacos.api.naming.listener.Event namingEvent = transferToNamingEvent(event);
        if (listener instanceof AbstractEventListener && ((AbstractEventListener) listener).getExecutor() != null) {
            ((AbstractEventListener) listener).getExecutor().execute(() -> listener.onEvent(namingEvent));
        } else {
            listener.onEvent(namingEvent);
        }
    }
}
Also used : AbstractEventListener(com.alibaba.nacos.api.naming.listener.AbstractEventListener) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) AbstractEventListener(com.alibaba.nacos.api.naming.listener.AbstractEventListener)

Example 2 with AbstractEventListener

use of com.alibaba.nacos.api.naming.listener.AbstractEventListener in project nacos by alibaba.

the class NamingExample method main.

public static void main(String[] args) throws NacosException, InterruptedException {
    Properties properties = new Properties();
    properties.setProperty("serverAddr", System.getProperty("serverAddr"));
    properties.setProperty("namespace", System.getProperty("namespace"));
    NamingService naming = NamingFactory.createNamingService(properties);
    naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
    System.out.println("instances after register: " + naming.getAllInstances("nacos.test.3"));
    Executor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("test-thread");
            return thread;
        }
    });
    naming.subscribe("nacos.test.3", new AbstractEventListener() {

        // EventListener onEvent is sync to handle, If process too low in onEvent, maybe block other onEvent callback.
        // So you can override getExecutor() to async handle event.
        @Override
        public Executor getExecutor() {
            return executor;
        }

        @Override
        public void onEvent(Event event) {
            System.out.println("serviceName: " + ((NamingEvent) event).getServiceName());
            System.out.println("instances from event: " + ((NamingEvent) event).getInstances());
        }
    });
    naming.deregisterInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
    Thread.sleep(1000);
    System.out.println("instances after deregister: " + naming.getAllInstances("nacos.test.3"));
    Thread.sleep(1000);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NamingService(com.alibaba.nacos.api.naming.NamingService) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AbstractEventListener(com.alibaba.nacos.api.naming.listener.AbstractEventListener) Properties(java.util.Properties)

Aggregations

AbstractEventListener (com.alibaba.nacos.api.naming.listener.AbstractEventListener)2 NamingService (com.alibaba.nacos.api.naming.NamingService)1 Event (com.alibaba.nacos.api.naming.listener.Event)1 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)1 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)1 Properties (java.util.Properties)1 Executor (java.util.concurrent.Executor)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1