Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Consider the following elements in an event-driven architecture:

  1. Observable Object
  2. Event Dispatcher
  3. Observer(Listener)

Now, we have two messages:

  1. From Observable Object to Event Dispatcher
  2. From Event Dispatcher to Observer(s).

How do you decide to pass these messages Sync or Async? Do you use any particular API (in Java) to implement that?

For example, in Java JFC/Swing, the first is Async and the second is Sync. The former is the reason that you can still fire more events even if the program is processing another event. The latter is the reason that you should avoid long operations in your listeners since they block the event dispatcher.

share|improve this question
add comment

2 Answers

A queue or priority queue from observable object to event dispatcher using Asyncronous message passing would be perfectly fine (due to the reasons you have already provided).

Unless you are using parallelism the event dispatcher should be Syncronous and even then it should have some sort of semaphore to make sure that no more than X number of observers (or instances of the same observer being careful around areas of mutex) are being pushed to; where X should coincide with the number of cores that you're running parallel on. Simple semaphores are fairly easy to implement in any language and I think Java has some system built in to implement them.

share|improve this answer
add comment

There is no hard-and-fast rule for this. Async delivery implies multiple threads, which increases the complexity of your solution, but also increases the flexibility. Synchronous might be better for cases where event handlers could benefit from sharing resources (e.g. a database connection), since the handlers would be executing on the same thread.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.