jQuery in Action, 2nd edition*
The moose likes EJB and other Java EE Technologies and the fly likes A Newb JMS question - trying to use observer pattern with MDB onMessage? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "A Newb JMS question - trying to use observer pattern with MDB onMessage?" Watch "A Newb JMS question - trying to use observer pattern with MDB onMessage?" New topic
Author

A Newb JMS question - trying to use observer pattern with MDB onMessage?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
I'm a bit confused on how to best implement something that I'm sure is pretty basic.

Take a simple example of where a warehouse sends a message to queue when an item is ready to be shipped.

An MDB receives the message.

Users can go to a JSP page and select that they want to be notified (say by email) when the item is ready to be shipped.

They could also go to the JSP and select to be removed from the notification.

For the sake of this example I don't want to use a DB.

The idea I had was that, using the Observer pattern, users would add themselves to a list of Listeners (and removed themself from this list as well.)

The part where I'm a bit unclear about is the implementation I would use within the MBean. I would think the idea would be ...

onMessage(..) { someobject.notifyListeners(); }

But what kind of bean/resource should I inject into the MDM? At first thought I was thinking I would need some kind of single stateful session bean (as a singleton?) that all users would have access to register/unregister themselves form the listeners list. Seems sort of stupid I think to try to use a stateful session bean like this since I really don't need to maintain conversation for the different clients - I just need one bean that I can access from the web midtier servlet (for registering listeners) and also access from my MDB's onMessage. Maybe I just need some kind of JMX component? I just need 'one' object that is initialized on server start up (or preferably lazy loaded on first access) and then just hangs around in memory that I can access both from my MDB onmessage and from elsewhere in my webapp.

Hopefully I'm missing something really obvious. Thanks for any help
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166

Other than the MDB, i dont see why you would require any other bean. Why not just have a singleton as follows:








Note that the listeners information wont be persisted. Every time you redeploy the application or restart the server, the listeners information will be lost and the ShipmentListener will be created new. You might want to go for database, later, for persisting the listener information.


[My Blog] [JavaRanch Journal]
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
DOH. There I go again try to overly complicate things Thanks Jaikiran - and I really appreciated that you even provided code - very nice.

Yes, I agree the DB would be the way to go for a production/real-life situation - this was going to be more for a quick learning exercise working with pub/sub. I already learned something - don't forget about basic Java when working with jee
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166

Good luck with your exercise
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Newb JMS question - trying to use observer pattern with MDB onMessage?