Reputation: 6140
In my current project we are using event system which propagates events without priority. What I mean exactly. The problem is that events are called without priority, so it means listening classes fire their 'on message' code in order they were added to listener container.
It can cause sometimes unpredictable difficult to analyze results.
Do you know any existing solutions which handle ordering of events in a 'nice way'?
Upvotes: 1
Views: 199
Reputation: 4583
Prioritizing events is the highway to hell. No literally. You'll need to keep track of what objects need what priority. Then you'll run out of priorities. Next you realize you've hard coded the same dependency (the priority constants class) into 100's of classes and literally have tied them all together. Well maybe not, but the pitfalls are numerous and not humorous. You have to look at what you want to achieve: You want to order process execution sequence. It has nothing to do whatsoever with events, except for the completely arbitrary fact that those processes are triggered by events.
There are many solutions:
Upvotes: 2
Reputation: 3191
The PriorityBlockingQueue
may do you favor.Make your Event implemetns Comparable,and the event dispatcher thread consume the heighest priority event each time.
Upvotes: 1
Reputation: 4202
Add a sort-of dependency flag in your listener classes. That is suppose listener A gets called which has a dependency on listener B, then let B execute first. Similarly, you can put dependency conditions in all your listener classes thereby creating a required hierarchy.
Just make sure you don't add circular dependency or handle it carefully.
Upvotes: 1