This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Event handling !!!  RSS feed

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to write an Event based notification system in java,have no idea where to start with though have gone through some study materials regarding Event handling, but could not find any thing worth while for me.

I want to write my own custom events. These custom events will be triggered when a method executes. I am pasting a small code...


Now when ever the method display gets called, a custom event gets triggered saying the display method is called.

Just one last thing the custom event is not a window based event,it should rather be a console based..

will be really thankful if any one of you can help, a small example would be of really great help. Hope i make sense and is asking for something which is not against ranch policy...


Rgds
[ May 27, 2007: Message edited by: Neha Mohit ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when ever the method display gets called, a custom event gets triggered saying the display method is called.

One way to handle it is by using AOP, which might be a bit advanced. Spring allows you to do this quite easily, using advices like BeforeAdvice.
You need to know how to use Spring though. Have as look at the following documentation and judge if you can get something out of it :
http://static.springframework.org/spring/docs/2.0.x/reference/aop-api.html#aop-api-advice-before
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant we do only in java , i mean without using springs or some other framework or any 3rd party api.

Cant we extend the EventObject class and then make the corresponding Event Source class , Listener class etc.


rgds
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Event handling is nothing magic, just ordinary Java method calls in an interesting decoupled design. The two main parts of the problem are keeping track of who is interested in which events and then notifying them when something happens.

Java comes with the Observable and Observer framework for both parts. I found them a bit clunky and wrote my own more flexible scheme. I use an "intermediary" which is optional in some publish subscribe pattern discussions. See if this looks useful:

I actually hand-code a new intermediary for every new set of related event messages. The base class handles the subscriber lists and the hand made one forwards the event to all listeners. Here's a complete example. It's some years old; I'm sure I'd do it a bit differently nowdays ...

See more background (sales pitch about why I like this) HERE
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!