tags:

views:

27

answers:

1

Is there a function which is called every time an EJB is looked up? I need to refresh some data before any method is called.

+1  A: 

Not sure I get exactly what you mean. First you probably need to distinguish between stateless and stateful EJBs.

  • Stateless EJB are well, stateless, and should not contain data. The app. server can decide to destroy or reconstruct it anytime. You don't actually hold a reference to a particular instance which is created when it's looked up. The app. server maintain a pool of EJBs, and one of them is used per invocation.

  • Stateful EJB can contain data. You hold a reference to one specific instance, which is created when the bean is looked up. Callback methods can be specified with @PostConstruct or @PreDestroy (These callbacks also exist for stateless EJBs but make less sense).

That said, if you need to perform something before a method is called, I suggest you use an interceptor (using @Interceptor, works for stateless and stateful EJBs). Just like with AOP, you get a chance to perform something before and after the bean method is actually executed, e.g refresh the cache.

ewernli
Data is client independent so I've created stateless bean (maybe wrongly). I need to read config file before method invocation. This config may change after deployment so I need to update this data everytime someone looks up for my EJB.
Filip
I see now. Configuration data is not business data, neither it is completely static in your case. I fear that realoding the config each time will be bad performance wise. You could store a timestamp and reload the data if a certain period has elapsed. You can store the config, in an instance field, in a static field, or give a try to a `@Singleton` bean if you use EJB 3.1. An instance field means you will have one config per bean instance, a static field means there is only one config but you must make sure you synchronize things correctly.
ewernli