views:

32

answers:

2

Hi,

I have a background service that runs every 10 min.

This service queries the DB for jobs to execute a will load a class with a different business logic according to the job (using Class.forName()). They implement the same Interface, of course.

I declared those classes to be SEAM components but when I try to use the entityManager or Logger I get a NPE.

Answering my own question, I suppose this is because Class.forName() goes under SEAM's injection mechanism and it won't have a chance to inject the dependencies.

I could probably work around that by injecting those myself, after I load the class. Is there a better way to do that?

Thanks!

+2  A: 

Did you check how to use EJB-Timer Service?

Another resource:

http://stackoverflow.com/questions/2070262/is-it-possible-to-use-seam-in-a-jboss-timed-service

I have to do something similar in a few days.

Please leave a comment what worked for you ;-)

stacker
I'm using the EJB 3 timers. Strangely enough I haven't had the 'No context' problem mentioned in the other question. Loading the component using Component.getInstance(String, boolean) solved the issue for me, no need to call lyfecicle.beginCall()... Anyway, now I'm able to use the EntityManager and Logger. Thanks!
Cleber Goncalves
@Cleber Goncalves Thanks for your feedback
stacker
A: 

You can inject all Seam components in non seam classes by using

Component.getInstance("entityManager") //You can inject whatever you want.

To get the Logger working you can do the following

 private static final LogProvider log = Logging.getLogProvider(MyClass.class);
Shervin