JSR 352 - Java batch - Cannot inject service bean or DAO in listener artifacts

192 views Asked by At

In a batch project, I need to load some reference data from the database before processing. I am trying to do so by injecting a service bean in the JobListener and loading the data in the "beforeJob" method but the serviceBean is always null.

public interface ServiceBean extends Serializable { ... }

@Stateless
@Named
public class ServiceBeanImpl implements ServiceBean { ... }

@Named
//@Dependent <- Tried adding this
public class CollectJobListener extends AbstractJobListener {

   @Inject JobContext jobContext;

   @Inject 
   private ServiceBean serviceBean; // Always NULL

   @Override
   public void beforeJob() throws Exception {...}

}

I tried with multiple types of listeners (chunk, process, write listeners) but the result is always the same.

The injection works fine though if I do it in the "main" artifacts (reader, proccesor, writer).

@Named
@Dependent
public class MyItemWriter extends AbstractItemWriter {

    @Inject 
    private ServiceBean serviceBean; // Works !

}

Any ideas on how I might get this to work ?

Or should I just do my setup job in one of the main artifacts ?

Thanks

1

There are 1 answers

0
cheng On

If you're working with WildFly (which includes JBeret as its batch processing container), it should work. Injecting beans to any batch artifacts (including batch listeners) shoudl work the same way. If the problem persists, please file an WildFly JIRA issue