• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[MDB & singleton] pattern

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic