This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Event handling !!!  RSS feed

 
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 ]
 
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
 
(instanceof Sidekick)
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
 
Always look on the bright side of life. At least this ad is really tiny:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!