How to access EJB from a Quartz Job

我只是一个虾纸丫 提交于 2019-12-11 02:48:35

问题


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.


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/8909573/how-to-access-ejb-from-a-quartz-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!