bahadir
bahadir

Reputation: 719

How to InitialContext.lookup instance of EJB by Class<?>

I am trying to create an EJB class using it's class type. My function gets this class type as parameter. This class has it's own dependencies and i want those dependencies to be created as well.

addToUploadQueue(Class<? extends ICallback> callbackClazz)

how can i create an ejb instance of this class?

Edit: i am uploading videos to youtube on a ManagedExecutorService thread.This is a generic module. i want this to run on every project. Http requests call my function, i create another thread and return immediately. When upload finishes i need to inform this callback function to update statusses of those videos on db (or do whatever they want).

Upvotes: 0

Views: 425

Answers (2)

Steve C
Steve C

Reputation: 19445

Given the extra information that you have provided, it looks a little like you're trying to reinvent the Observer Pattern.

Fortunately, Java EE 6 and newer provides us with a CDI framework that implements this for us.

Adam Bien shows an example at JAVA EE 6 OBSERVER PATTERN / EVENTS WITH CDI (JSR-299/JSR-330) AND EJB 3.1.

Upvotes: 1

Rafael Zeffa
Rafael Zeffa

Reputation: 2414

you have to get a ejb instance using the lookup method, you can do something like that

addToUploadQueue(Class<? extends ICallback> callbackClazz) {
  InitialContext ic = new InitialContext();
  MyEjb myEjb = (MyEjb)ic.lookup("java:comp/env/ejb/" + callbackClazz.getName());
}

Upvotes: 0

Related Questions