bennidi / mbassador

Powerful event-bus optimized for high throughput in multi-threaded applications. Features: Sync and Async event publication, weak/strong references, event filtering, annotation driven
MIT License
955 stars 146 forks source link

Add ability to cancel events #129

Closed nikoliazekter closed 8 years ago

nikoliazekter commented 8 years ago

Sometimes there's a need to cancel event so other handlers won't get it. It theoretically should work good but I tested it only with synchronous events. If you approve this, I'll add unit tests if needed.

bennidi commented 8 years ago

I can not think of a use case where cancellation of a message publication is necessary. I think that the need to cancel a message that is already being published very likely indicates a problem in your code design.

The proposed code in your PR is not thread-safe and comes with various race conditions (for instance when publications are added while the map is being rehashed). Furthermore, it was really difficult to get the core stable and correctly synchronized. I don't want to add new code to it unless the new feature is really worth it.

nikoliazekter commented 8 years ago

I understand. Here's an example where cancellable events are useful https://github.com/MovingBlocks/Terasology/wiki/Events-and-Systems#consumable-events .

nikoliazekter commented 8 years ago

Thinking about it made me realize that "consume the event" sounds more correct than "cancel the event".

tbee commented 8 years ago

I'm currently searching for a way to cancel/consume events in mbassador, to support a PR adding this functionality I'll describe my use case. (I understand that this specific PR has technical issues.)

In my setup MBassador is used in a JEE environment to loosely couple some modules. Most events are handled synchronous and within a single UserTransaction. If one of the events runs into a problem and sets the UserTransaction to "rollback only" the sending of events is not cancelled; the following listeners will flood the logs with "transaction set to rollback only" exceptions. I need to be able to tell MBassador to cancel if such an situation occurs.

bennidi commented 8 years ago

I close this PR because it has not test code attached. It contains modification to core code and I assume that it introduces race conditions. The requested feature would best be implemented with a custom dispatcher.