A change watcher, how to make?
I am currently working on cleaning up some existing code and give it some Robotlegs love. Only I am curious what the best approach is to implement it. The problem is that I need to detect to changes to files on the computer every n seconds and then inform the responsible classes that this file has changed. For example, a file which contains whether the device is logged in or not into the corporate network plus the ip address etc.
My current idea is to make three service classes with the following functionality:
- Timer service which just dispatches
TICKevents every n seconds (TimerEvent(TICK, 'dataChecker') Each time this event gets dispatched the appropriate command - After each
TICKthe methodcheckForChangesgets called in theFileWatcherServiceif a file has changed content this will dispatch an event with the name of the changed fileFileItemChanged(CHANGED,'systemState.xml')after this event a command gets triggered which will call - The
process(filename)method which is responsible actually processing the data in the changed filesystemState.xmlwhich finally will cause the model to be changed.
Now my question is this is a good approach or over-engineered?
Could it be simplified when I am using signals instead of events?
Also if I am using events can I also somehow set a contract
CommandA only should be called when
event.type=TICK and
event.timerName='dateChecker'?
Thanks for your insight!
Comments are currently closed for this discussion. You can start a new one.
2 Posted by Jos Yule on 10 May, 2011 02:18 PM
My only thing would be to wire the TICK event to a CheckForChanges Command. The command would have the FileWatcherService injected into it, and then call the appropriate method on that. Continue on as you had in your description - the Service firing another event, which triggers another Command to handle the changed file.
Peace
jos
Support Staff 3 Posted by Stray on 11 May, 2011 08:19 AM
I'd bind the command to a further customised event instead of
TimerEvent.TICKand then you can control the 'only when timerName = x' part yourself inside the service, or a service wrapper.Stray
4 Posted by Weyert on 11 May, 2011 10:22 AM
Thanks for the suggestions. I am currently worked on it and seems to be working nicely. I am only not sure what you mean Stray.
Currently I am having code like in my CheckForChanges command:
Only this gets a big long when you have multiple tickets going on via the TimerService-class.
Support Staff 5 Posted by Stray on 11 May, 2011 10:44 AM
That's what I mean - rather than having them all bound to one 'tick' event, wouldn't you be better having the timer service wrapped in a timer gateway that handles this logic by dispatching specific events?
So - imagine we have a TimerGateway - this TimerGateway has injected an instance of TimerService. TimerService no longer extends Actor, and instead of dispatching its events to the framework, it just dispatches them locally.
We have a dictionary of payload names to specific events - let's say we also get rid of those flimsy magic strings and put those as constants in an object called TimerPayloadReference.
So - we map the payload references to specific events, and so TimerGateway looks like:
Now all our control flow logic is nicely captured in this bubble. In your context (or bootstrap command) you can bind specific commands to these specific
TimedActionEventevents.You would also need to get timerService to use the TimerPayloadReference constants rather than just plain-old strings.
Does that make sense?
Stray
6 Posted by Weyert on 11 May, 2011 11:34 AM
Love it! Thanks.
7 Posted by Weyert on 11 May, 2011 12:38 PM
Thanks, this seems to be working really nicely.
I have implemented a similar approach for dealing with the actual changed files event so that it will call
ProcessXXXFileCommandwhen that file is updated.In this command you just do
fileWatcherService.getContents( FileWatcherPayloadReference.SYSTEM_STATE ). I am not sure if I should send the actual contents along or not.8 Posted by Weyert on 11 May, 2011 01:12 PM
I have decided to send a VO along with the file name and its contents:
9 Posted by Stray on 11 May, 2011 01:46 PM
Much better :)
Stray closed this discussion on 11 May, 2011 03:49 PM.