简体   繁体   English

从 Quartz 作业调用 EJB

[英]Invoke an EJB from a Quartz job

I have a Java EE application that runs on Wildfly and I'd like to integrate it with the Quartz Scheduler.我有一个在 Wildfly 上运行的 Java EE 应用程序,我想将它与 Quartz Scheduler 集成。 This is how I envision to invoke an EJB from a Quartz job (since I don't know the name of the EJB class at compile time, I use a lookup):这就是我设想从 Quartz 作业调用 EJB 的方式(因为我在编译时不知道 EJB 类的名称,所以我使用查找):

public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {        

        InitialContext ic = new InitialContext();
        MyInterface bean = null;
        try {
            String beanClassName  = getItFromSomewhere();
            bean = (MyInterface) ic.lookup("java:module/" + beanClassName );
        } 
        catch (NamingException e) {
            e.printStackTrace();
        }

        bean.myMethod();
     }
}

Is this approach correct?这种方法是否正确? The container wouldn't know about the Quartz job when it's initiated, is that an issue?容器在启动时不知道 Quartz 作业,这是一个问题吗?

Imho a cleaner alternative is passing the EJB instance via the Job 's JobExecutionContext恕我直言,更清洁的替代方案是经过EJB通过实例JobJobExecutionContext

When preparing the Job准备Job

final JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put(MY_INTERFACE, myInterface);

final Job myJob =
    JobBuilder.newJob(MyJob.class)
              .setJobData(jobDataMap)
              .build();

Inside Job#execute内部Job#execute

final JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
final MyInterface myInterface = (MyInterface) jobDataMap.get(MY_INTERFACE);

A Quartz Job implementation shouldn't be aware at all of the JavaEE container in which it is operating. Quartz Job实现不应该知道它在其中运行的所有 JavaEE 容器。 This will will ease the process of updating your code/architecture in the long term.从长远来看,这将简化更新代码/架构的过程。
Also, your Job should only care about its only duty, not about getting the required dependencies.此外,您的Job应该只关心它的唯一职责,而不是获取所需的依赖项。
Think about the JobDataMap as a strange kind of Dependency Injection .JobDataMap视为一种奇怪的依赖注入

Here is your answer provided by the Quart'z developers on GitHub:以下是 Quart'z 开发人员在 GitHub 上提供的答案:

https://github.com/quartz-scheduler/quartz/blob/master/quartz-jobs/src/main/java/org/quartz/jobs/ee/ejb/EJBInvokerJob.java https://github.com/quartz-scheduler/quartz/blob/master/quartz-jobs/src/main/java/org/quartz/jobs/ee/ejb/EJBInvokerJob.java

To summarize: Your approach is right, in your MyJob class you correctly create an InitialContext() to search of the EJB instance you would like to invoke.总结一下:您的方法是正确的,在您的 MyJob 类中,您正确地创建了一个 InitialContext() 来搜索您想要调用的 EJB 实例。 You cannot put an instance of the EJB in the JobContext, as suggested by the previous answer, because in case there is a server restart between the scheduling of the Job and its invocation, you have no guarantee that an instance of the EJB you want will be "injected" in the JobContext.正如上一个答案所建议的,您不能将 EJB 的实例放在 JobContext 中,因为如果在 Job 的调度和它的调用之间有服务器重新启动,您不能保证您想要的 EJB 的实例将在 JobContext 中被“注入”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM