如何从Quartz作业访问EJB [英] How to access EJB from a Quartz Job
问题描述
好吧,我正在使用Quartz计划我的应用程序中需要的一些工作。但是,我需要某种方式来访问我的Job上的有状态SessionBean。我知道我无法使用@EJB注入它。谁能帮我?
谢谢。
Well, I'm using Quartz to schedule some jobs that I need in my application. But, I need some way to access a Stateful SessionBean on my Job. I knew that I can't inject it with @EJB. Can anyone help me? Thanks.
推荐答案
我使用EJB3InvokerJob来调用EJB的方法。然后,我创建了扩展EJB3InvokerJob的作业,放置了应调用的EJB和方法的参数,然后调用了super.execute()。
I used the EJB3InvokerJob to invoke the methods of my EJB. Then I created my jobs that extends the EJB3InvokerJob, put the parameters of what EJB and method it should call and then call the super.execute().
EJB3InvokerJob可以是在此处找到: http://jira.opensymphony.com/secure/attachment/13356/ EJB3InvokerJob.java
The EJB3InvokerJob can be found here: http://jira.opensymphony.com/secure/attachment/13356/EJB3InvokerJob.java
我的工作看起来像这样:
My Job is looking like this:
public class BuscaSistecJob extends EJB3InvokerJob implements Job{
private final Logger logger = Logger.getLogger(this.getClass());
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap dataMap = jobExecutionContext.getMergedJobDataMap();
dataMap.put(EJB_JNDI_NAME_KEY, "java:app/JobService");
dataMap.put(EJB_INTERFACE_NAME_KEY, "br.org.cni.pronatec.controller.service.JobServiceLocal");
dataMap.put(EJB_METHOD_KEY, "buscaSistec");
Object[] arguments = new Object[1];
arguments[0] = jobExecutionContext.getTrigger().getStartTime();
dataMap.put(EJB_ARGS_KEY, arguments);
Class[] argumentTypes = new Class[1];
argumentTypes[0] = Date.class;
dataMap.put(EJB_ARG_TYPES_KEY, argumentTypes);
super.execute(jobExecutionContext);
}
}
我的EJB是这样的:
And my EJB is like this:
@Stateless
@EJB(name="java:app/JobService", beanInterface=JobServiceLocal.class)
public class JobService implements JobServiceLocal {
@PersistenceContext
private EntityManager entityManager;
@Resource
private UserTransaction userTransaction;
@Override
public void buscaSistec(Date dataAgendamento) {
// Do something
}
我希望可以帮助某人。
这篇关于如何从Quartz作业访问EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!