Search in sources :

Example 1 with RecordListener

use of com.alibaba.nacos.naming.consistency.RecordListener in project nacos by alibaba.

the class DistroConsistencyServiceImpl method processData.

private boolean processData(byte[] data) throws Exception {
    if (data.length > 0) {
        Map<String, Datum<Instances>> datumMap = serializer.deserializeMap(data, Instances.class);
        for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
            dataStore.put(entry.getKey(), entry.getValue());
            if (!listeners.containsKey(entry.getKey())) {
                // pretty sure the service not exist:
                if (switchDomain.isDefaultInstanceEphemeral()) {
                    // create empty service
                    Loggers.DISTRO.info("creating service {}", entry.getKey());
                    Service service = new Service();
                    String serviceName = KeyBuilder.getServiceName(entry.getKey());
                    String namespaceId = KeyBuilder.getNamespace(entry.getKey());
                    service.setName(serviceName);
                    service.setNamespaceId(namespaceId);
                    service.setGroupName(Constants.DEFAULT_GROUP);
                    // now validate the service. if failed, exception will be thrown
                    service.setLastModifiedMillis(System.currentTimeMillis());
                    service.recalculateChecksum();
                    // The Listener corresponding to the key value must not be empty
                    RecordListener listener = listeners.get(KeyBuilder.SERVICE_META_KEY_PREFIX).peek();
                    if (Objects.isNull(listener)) {
                        return false;
                    }
                    listener.onChange(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName), service);
                }
            }
        }
        for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
            if (!listeners.containsKey(entry.getKey())) {
                // Should not happen:
                Loggers.DISTRO.warn("listener of {} not found.", entry.getKey());
                continue;
            }
            try {
                for (RecordListener listener : listeners.get(entry.getKey())) {
                    listener.onChange(entry.getKey(), entry.getValue().value);
                }
            } catch (Exception e) {
                Loggers.DISTRO.error("[NACOS-DISTRO] error while execute listener of key: {}", entry.getKey(), e);
                continue;
            }
            // Update data store if listener executed successfully:
            dataStore.put(entry.getKey(), entry.getValue());
        }
    }
    return true;
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) EphemeralConsistencyService(com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService) Service(com.alibaba.nacos.naming.core.Service) RecordListener(com.alibaba.nacos.naming.consistency.RecordListener) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)1 Datum (com.alibaba.nacos.naming.consistency.Datum)1 RecordListener (com.alibaba.nacos.naming.consistency.RecordListener)1 EphemeralConsistencyService (com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService)1 Service (com.alibaba.nacos.naming.core.Service)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1