如何从 Quartz 作业访问 EJB

发布于 2024-12-27 10:13:05 字数 108 浏览 1 评论 0原文

嗯,我正在使用 Quartz 来安排我的应用程序中需要的一些作业。但是,我需要某种方法来访问我的作业中的 Stateful 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本王不退位尔等都是臣 2025-01-03 10:13:06

我使用 EJB3InvokerJob 来调用 EJB 的方法。然后我创建了扩展 EJB3InvokerJob 的作业,输入它应该调用的 EJB 和方法的参数,然后调用 super.execute()。

EJB3InvokerJob 可以在这里找到: http://jira.opensymphony.com/secure/ Attachment/13356/EJB3InvokerJob.java

我的工作是这样的:

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 是这样的:

@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
    }

我希望能帮助某人。

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().

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);
    }

}

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
    }

I expect to help someone.

尽揽少女心 2025-01-03 10:13:06

一个简单的解决方案是在作业实现中通过 JNDI 查找 EJB。

final Context context = new InitialContext();

myService= (MyService) context
                .lookup("java:global/my-app/myejbmodule-ejb/MyService");

我已经在 Glassfish 3.1 上开发的当前应用程序中完成了此操作。

A simple solution would be to lookup the EJB via JNDI in the Job implementation.

final Context context = new InitialContext();

myService= (MyService) context
                .lookup("java:global/my-app/myejbmodule-ejb/MyService");

I have done this in a current application I am developing on Glassfish 3.1.

再浓的妆也掩不了殇 2025-01-03 10:13:06

您只需在作业实现中通过 JNDI 查找 EJB 即可做到这一点。特别是,JNDI 名称将为:

mappedName#name_of_businessInterface

其中 name_of_businessInterface 是该会话 bean 的业务接口的完全限定名称。例如,如果您指定 mappedName="bank" 并且业务接口的完全限定名称为 com.CheckingAccount,则业务接口的 JNDI 为 银行#com.CheckingAccount

代码示例:

Context context = new InitialContext();
MyService myService= (MyService) context.lookup("MyService#com.test.IMyService");

you can do that simply by lookup the EJB via JNDI in the Job implementation. In particular, the JNDI name will be:

mappedName#name_of_businessInterface

where name_of_businessInterface is the fully qualified name of the business interface of this session bean. For example, if you specify mappedName="bank" and the fully qualified name of the business interface is com.CheckingAccount, then the JNDI of the business interface is bank#com.CheckingAccount.

Code Example:

Context context = new InitialContext();
MyService myService= (MyService) context.lookup("MyService#com.test.IMyService");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文