All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.watcher.AbstractResourceWatcher Maven / Gradle / Ivy

There is a newer version: 8.13.4
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */
package org.elasticsearch.watcher;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Abstract resource watcher framework, which handles adding and removing listeners
 * and calling resource observer.
 */
public abstract class AbstractResourceWatcher implements ResourceWatcher {
    private final List listeners = new CopyOnWriteArrayList<>();
    private boolean initialized = false;

    @Override
    public void init() throws IOException {
        if (!initialized) {
            doInit();
            initialized = true;
        }
    }

    @Override
    public void checkAndNotify() throws IOException {
        init();
        doCheckAndNotify();
    }

    /**
     * Registers new listener
     */
    public void addListener(Listener listener) {
        listeners.add(listener);
    }

    /**
     * Unregisters a listener
     */
    public void remove(Listener listener) {
        listeners.remove(listener);
    }

    /**
     * Returns a list of listeners
     */
    protected List listeners() {
        return listeners;
    }

    /**
     * Will be called once on initialization
     */
    protected abstract void doInit() throws IOException;

    /**
     * Will be called periodically
     * 

* Implementing watcher should check resource and notify all {@link #listeners()}. */ protected abstract void doCheckAndNotify() throws IOException; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy