vendredi 25 septembre 2015

How to implement Watcher Service for any Java object and this watcher service's threads will maintain/compare state of objects

In a system, I have many in-memory objects processing data. And I have to monitor all required objects. And if these objects crossing their thresholds user must notify for this incident. So I am thinking for creating a WatcherService for this. And any required objects must have to register for this. Here is the skeleton of my thinking.

class Foo {
    private Integer a;
    private String b;
    private Long c;
    private List<ASD> asds;

    //setter and getter
}

class Bar {
    private Integer aa;
    private Integer ab;
    private Float cc;

    //setter and getter
}

class WatchThread<T> implements Runnable, IWatchThread { // Actually IWatchThread extends Runnable
    private final T t;

    private final Set<String> paramsToWatch;

    private Boolean isRunning = false; 

    public WatchThread(T t, Set<String> paramsToWatch) {
        this.t = t;
        this.paramsToWatch = paramsToWatch;
    }

    public void run() {
        while(isRunning) {
            //Do some comparsion work by Reflection !!??!!
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
    }
}

class WatcherService {

    private static final Map<Object, IWatchThread> watchThreads = new HashMap<>();  

    public static <T> void registerForWatch(T t, Object keyToMaintain, Set<String> paramsToWatch) {
        IWatchThread watchThread = new WatchThread<T>(objectValue, paramsToWatch);
        watchThread.setIsRunning = true;
        watchThread.start();
        watchThreads.put(keyToMaintain, watchThread);
    }

    public static unregister(Object keyToMaintain) {
        IWatchThread watchThread = watchThreads.get(keyToMaintain);
        watchThread.setIsRunning(false);
        watchThreads.remove(keyToMaintain);
    }
}

class GlobalService {
    private static Map<String, Foo> foos = new HashMap<>();
    private static Map<String, Bar> bars = new HashMap<>();

    public static void loadObjects(List<Foo> foos, List<Bar> bars) {

        //Parameters that required for watch, so by reflection a thread can access objects
        Set<String> paramsToWatchForFoo = new HashSet<>();
        paramsToWatchForFoo.add("a");
        paramsToWatchForFoo.add("b");
        paramsToWatchForFoo.add("c");

        for(Foo foo : foos) {
            //do some other stuff
            WatcherService.registerForWatch(foo, paramsToWatchForFoo);      
        }

        Set<String> paramsToWatchForBar = new HashSet<>();
        paramsToWatchForBar.add("aa");
        paramsToWatchForBar.add("cc");

        for(Bar bar : bars) {
            //do some other stuff
            WatcherService.registerForWatch(bar, paramsToWatchForBar);      
        }
    }
}

As you can see, I don't want to touch Foo and Bar. And watcher thread in back they do their work without interruption. Is this correct way to implement this? Or any other way to achieve this, required your valuable suggestion.

Aucun commentaire:

Enregistrer un commentaire