0

Java EE 7 - Injection into Runnable/Callable object

Continuing this question, i have a situation... I'm using a Producer/Consumer pattern to consuming a list of objects. My Class Consumer is a implementation of Runnable.

public class MYConsumer implements Runnable{

   @EJB
   private MYService myService;    

   private BlockingQueue<> queue;

   public MYConsumer (BlockingQueue<> q){
        this.queue=q;
   }
  @Override
    public void run() {
       /** Error is here**/    
       myService.insert(); 
    }
}

The implementation of the Pattern is working fine, but when i try to use myService, it is Null. The container not injecting the Service. I know it is because the Container knows only instances created by itself.

But how can overcome this problem? Thanks

1 Answers1

1

You should instantiate your runnable object inside a session bean. This session bean will instantiate the service you want and then pass it to your runnable object.