Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Event handling

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone point me to a sample involving non-component based event handling?
All the samples that i find are event handlers being tied to AWT components?
I just wish to write a simple server which calls back on registered listeners(clients),by programmatically raising an event from one of the clients.
Any help will be appreciated.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you taken a look to the java.util.Observer and java.util.Observable?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a fan of Observer and Observable in the library. Before I even found them I made my own pub-sub with an intermediary. The flow is described HERE. I make an interface with methods for the events, implement it once in the intermediary and again in each subscriber. Here's an example with two events:

The abstract Publisher has (dead simple) methods to subscribe and unsubscribe. Some may be offended that I have to copy-paste that little loop for every event. But the flexibility I have here is pretty cool. I can pass parameters and get return types. I could let all the listeners vote on what to do next or let one of them signal that it has consumed the event and stop looping. I can have multiple publishers for the same event. Publishers can come and go while the subscription persists. Good fun.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic