Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!