문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

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");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top