Reputation: 16430
I would like to know if it is possible to use CDI beans in Quartz jobs in a portable way?
I have already tried to write my own JobFactory and so on, but the problem is, that the BeanManager is not available through JNDI, because the job runs in a non managed thread. My application server is Websphere 8.0.0.1 and I already read that it is against the EE6 spec to make the name "java:" available to non managed threads.
There are two solutions now I think:
I know that, when I use the CDI solution, then I am not able to let the worker threads run in a different JVM, correct me if I am wrong. To stay scaleable I should implement an EJB for that? What do you think, have you ever had that problem? I would also appreciate different solutions or even suggestions on different scheduler libraries!
Upvotes: 3
Views: 4755
Reputation: 24457
Look at DeltaSpike scheduler module.
You can find last version on Maven Central Repository.
Upvotes: 4
Reputation: 708
There seem to be a quick'n'dirty way to have CDI in local Quartz environment: set your own subclass of PropertySettingJobFactory in your scheduler, which has its own injected instance of BeanManager and does the following to every created job:
Job job = super.newJob(bundle, Scheduler);
Class<? extends Job> clazz = job.getClass();
if (beanManager != null) {
CreationalContext<Job> ctx = beanManager
.createCreationalContext(null);
@SuppressWarnings("unchecked")
AnnotatedType<Job> type = (AnnotatedType<Job>) beanManager
.createAnnotatedType(clazz);
InjectionTarget<Job> it = beanManager.createInjectionTarget(type);
it.inject(job, ctx);
}
Then @Injects will be filled in your Jobs on creation.
Upvotes: 1
Reputation: 12039
Have you look at CDIQI its a CDI Quartz implementation that you may be able to model your own after?
https://github.com/symbiont/cdiqi
Or is your problem that you absolutely need to create your own threads? CDIQI has an asynchronous execution of jobs but it sounds like you want to run yours remotely on another JVM?
Upvotes: 0
Reputation: 5378
You can take a look at DeltaSpike and use its BeanManagerProvider, or you can create a PortableExtension and cache a reference to the BeanManager in the extension and use that. There shouldn't be a problem doing things that way.
Upvotes: 1