Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes [MDB &  singleton] pattern 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 "[MDB &  singleton] pattern" Watch "[MDB &  singleton] pattern" New topic
Author

[MDB & singleton] pattern

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Hi there,
I'd like to have some feedback on this pattern which uses a singleton class to pass an ObjectMessage to a Message Driven Bean. What you think and how you would improve it.
The code shown belowe belongs to the client classes based on struts



A Struts plugin which closes the session


The client code which deal with the MDBMessage


Does it make sense?


trying to decode a woman mind....
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

Havent spent much time on the code, but one thing that comes to my mind is why would you make the MDBMessage singleton. Furthermore, the session and other objects like the sender are created in the constructor of this *singleton* class which means that once the ShutDownPlugin closes the session, there's no way to recreate the session and the related objects because the constructor of this singleton MDBMessage will not be called again.


[My Blog] [JavaRanch Journal]
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
As these properties are common used by other classes, I though it was a good idea reduce the code implementing a singleton class.

The idea to close the session on the PlugIn class came out because its destroy method should be called at application server shutdown, therefore no one will be using it.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

Going by this singleton approach, you are going to end up having a single connection and session sending all the messages. This means that the connection will be open throughtout the lifecycle of the application. This would not be a good idea.

As these properties are common used by other classes, I though it was a good idea reduce the code


As far as reducing the code is concerned, you can still achieve that by having this connection/session creation in a utility class which is not a singleton.
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Thanks for your reply,
but why is not a good idea? You mean memory leaks? At least I know that only one instance is using that session and therefore I perhaps minimize that risk.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

With the number of posts in this forum increasing because of the book promotion, lost track of your post. Sorry about that

but why is not a good idea?


Going by the singleton approach, you are keeping one single connection open for the entire lifetime of the application. What if the connection timeouts (or breaks for some reason), you wont be able to recover since with the current design there's no way the connection will be recreated. Just my thought, let us know if you feel otherwise.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [MDB & singleton] pattern
 
Similar Threads
Cant get reference to Queue
MDB @PostConstruct error
MDB onMessage method
Help on Topic in JMS
JMS Help: send a queue message with reply