• Post Reply Bookmark Topic Watch Topic
  • New Topic

Message Driven Bean question  RSS feed

 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently looking at a piece of work to replace a servlet, which currently kicks off a thread that polls a messaging queue (in this case MQ). I know I know - I hear all of you in shock at the terribleness of this, that is why I have come along to see what is the better alternative.

My solution is to use an MDB instead. However, in the servlet's init() method it carries out a few house keeping jobs that we need to keep in place post-MDB solution. Ideally I would like to dispense with the servlet, but I would need to have something equivalent to the init() method that can act during start up and perform these tasks only once.

Is there anything within an MDB that would carry this out? Or are there any other alternatives that anyone can think of?

Cheers,


Stuart
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using JBoss, then you can go for MBeans. You can deploy them as a service which get invoked on server startup. A couple of references:

ExampleHelloWorldService

CreateAStartupClass
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately we are not using JBoss, instead we are using IBM's WebSphere Application Server.

Any thoughts around that environment?

Cheers,


Stuart
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the application is going to consist of more than this one bean , there should be some place for application initialization code.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I suppose you would still have webapplication running even if you move to MDB. In that case you try using ServletContextListener for those house keeping jobs. If those Jobs have anything to do with what MDB does then it may not work because ServletContextListener will fire events when webapp is deployed and undeployed i.e when context is created and destroyed.
regards,
Amit
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stuart,

Amit brings up a good point. You should be able to use the ServletContextListener for your scenario if you are not using or planning to use clustering.

I am not sure about the nature of your house keeping jobs. It might be a good idea of move them to your MDB if it is not required to be available at startup.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ideally I would like to dispense with the servlet, but I would need to have something equivalent to the init() method that can act during start up and perform these tasks only once.


If its an user action that triggers of the thread, you would still need the servlet.

If it's at application startup, you can use the ServletContextListener to do your house keeping and fire of the message to the MessageQueue.

Finally all app servers have startup and shutdown hooks - you can alternatively use that at the cost of portability.

cheers,
ram.
 
nandkishor rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would suggest whatever code is there in your init() method ,
put in your MDBS ejbCreate(). So whenever the application will startup
it will do the intialization thing for you. efine a static variable in any of your singleton class say count,And in the
ejbCreate(){

if(SingletonClass.StaticVariable ==1){
put the init code here
SingletonClass.StaticVariable++
}
}

----------------------------------------------------------------
If this will not work then at the startup from ejbCreat() you can post a message to the MDB itself which can call any heper class method that can do the housekeeping for you.
 
Jimmy Ho
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just created my first message-driven EJB last week on WAS 5.1, and I agree with using the ejbCreate() method. That's probably the easiest.

Be careful if you're in a clustered environment though. Using that static variable only works if all the beans in your pool come from the same JVM. If the beans are on a WAS cluster, then you're not going to get that behavior.

By the way, if you want an easy way to create MDB's, and you're using Rational Application Developer, don't forget that you can right-click and create an EJB Project, and then right-click in the proper place, and auto-generate a MDB. Good luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!