This discussion is archived
6 Replies Latest reply: Jan 15, 2013 8:12 AM by jtahlborn RSS

Re-initialize EJB

user2695214 Newbie
Currently Being Moderated
I have an implementation of an EJB that executes some code during the @PostConstruct. While my application is running, there are many instances of the EJB. Is there any way to force the application to call the code in @postConstruct (maybe with a different annotation) for each instance of the bean?

This is what I would like to see happen.

- bean gets initialized and calls @PostConstruct method during startup
- application runs and uses bean for some time
- application gets a trigger (from a timer or webservice) to call bean.reinitialize()
- bean.reinitialize() tells the application to call the bean method with @PostConstruct (or a different annotation) once for each instance of the bean

Does something like this exist with EJBs?
  • 1. Re: Re-initialize EJB
    jtahlborn Expert
    Currently Being Moderated
    while you don't give the details of that this data is and how you are using, this scenario smells bad. it sounds like you have implemented a cache inside your EJB which you need to periodically flush. that is not how you should be using a standard EJB. if this is what you are doing, then you should move the cached data to a proper cache which you can manage using whatever cache semantics you might need. the EJBs should grab the data when they need it (i.e. during execution), not at creation time.
  • 2. Re: Re-initialize EJB
    user2695214 Newbie
    Currently Being Moderated
    Let me try to clarify,

    I have a method, call it executeStuff(). It needs to use some parameters that are stored in a database table. Instead of reading from the database everytime I call executeStuff(), I initialize the data in the postConstruct.

    However, I would like to make the parameters configurable without restarting the deployment. So what I would like is this:

    - when I update the database table entries, I call a bean method that says "please reload the configuration"
    - then the application will tell every bean instance to finish the current processing, then stop any new processing on the bean, and then call the postConstruct(or some other annotation) method
    - after the postConstruct is complete, the bean can start processing again.
  • 3. Re: Re-initialize EJB
    jtahlborn Expert
    Currently Being Moderated
    yep, sounds like pretty much what i expected. my original answer definitely applies.
  • 4. Re: Re-initialize EJB
    user2695214 Newbie
    Currently Being Moderated
    Wow, thanks for the reply. I was hoping it would not need to be overly complicated.

    In your first response you mentioned a "proper cache". What did you mean by this? Is there a some EJB construct for this or was there something more simple that you were referring to?
  • 5. Re: Re-initialize EJB
    r035198x Pro
    Currently Being Moderated
    user2695214 wrote:
    Wow, thanks for the reply. I was hoping it would not need to be overly complicated.

    In your first response you mentioned a "proper cache". What did you mean by this? Is there a some EJB construct for this or was there something more simple that you were referring to?
    If you are using JPA for persistence then your (persistence) provider would already come with some form of second cache implementation that you would need to configure or you can use existing caching APIs yourself like ehchache.
  • 6. Re: Re-initialize EJB
    jtahlborn Expert
    Currently Being Moderated
    r035198x wrote:
    user2695214 wrote:
    Wow, thanks for the reply. I was hoping it would not need to be overly complicated.

    In your first response you mentioned a "proper cache". What did you mean by this? Is there a some EJB construct for this or was there something more simple that you were referring to?
    If you are using JPA for persistence then your (persistence) provider would already come with some form of second cache implementation that you would need to configure or you can use existing caching APIs yourself like ehchache.
    Yep, a second-level cache would definitely be my first thought as well. that fits very well into the existing ejb/jpa infrastructure.

    that said, if you are working with some sort of non-jpa data, then you are looking at a custom cache situation. i would implement something like that using a JMX management bean (although initializing these in the ejb world is not as easy as it should be). you could probably also implement this using an ejb 3.1 singleton ejb.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points