0

I'm trying to make CDI work on JBeret SE. This is my code:

SampleBatchlet class

@Named
public class SampleBatchlet extends AbstractBatchlet
{
    @Inject
    @BatchProperty(name = "foo")
    String foo;

    @Inject
    StepContext stepContext;


    @Inject
    Logger logger;

    @Override
    public String process() throws Exception {
        final String say = stepContext.getProperties().getProperty("say");
        System.out.println("hello foolish");
        return null;
    }
}

SampleBatchletTest class

@EnableWeld
class SampleBatchletTest {

    @Inject
    Logger logger;

    @WeldSetup
    public WeldInitiator weld = WeldInitiator
            .from(
                    LoggerProducer.class
            )
            .activate(
                    RequestScoped.class,
                    ApplicationScoped.class
            )
            .build();


    @Test
    void app() throws InterruptedException {

        final JobOperator jobOperator = BatchRuntime.getJobOperator();

        long id = jobOperator.start("simplebatchlet", null);

        final JobExecutionImpl jobExecution = (JobExecutionImpl) jobOperator.getJobExecution(id);
        jobExecution.awaitTermination(5, TimeUnit.SECONDS);
        Assertions.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
    }

}

Server class

@ApplicationScoped
public class Server {

    @Inject
    private Logger logger;

    public void init(@Observes @Initialized(ApplicationScoped.class) Object init) throws InterruptedException {
        logger.info("init");
}

LoggerProducer class

public class LoggerProducer {
    @Produces
    public Logger produceLogger(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }
}

The issue is Logger instance is not injected on SampleBatchlet, whereas is correctly injected either on test and server class above.

Any hints?

LITTLE UPDATE

By reading this reference

https://jberet.gitbooks.io/jberet-user-guide/content/batch_properties/

I discovered java.util.logging.Logger can be injected.

Therefore I added

<batchlet ref="it.infocert.shop.main.SampleBatchlet" >
    <properties>
        <property name="logger" value="java.util.logging.Logger" />
    </properties>
</batchlet>

where value can be actually anything..

On SampleBatchlet I added

@Inject
@BatchProperty
Logger logger;

and now it is injected. I'm a bit perplexed by the way, because I wish to use another logger implementation..

Fabrizio Stellato
  • 1,727
  • 21
  • 52

2 Answers2

0

When injecting via @BatchProperty, JBeret tries to check the type of injection field and match it up with the injection value, and instantiate an instance for injection. That's why the logger created by JBeret, instead of your own logger, is injected. For details, see JBeret BatchBeanProducer.java

To inject your own logger via a producer method, you may need to add a qualifier to disambuiguise it. For example,

public class LoggerProducer {
    @Produces
    @Named("myLogger")
    public Logger produceLogger(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }
}


@Inject
@Named("myLogger")
Logger logger;
cheng
  • 1,076
  • 6
  • 6
  • Tried either from test class and main, but with no success, all I get is a null value :( Where am I failing ? My guess is I have to fix bean resolver from main classes – Fabrizio Stellato Nov 29 '19 at 17:03
  • honestly I can't find an example of weld usage and successful injection under the batchlet domain. Guess it works on EE web app, but never found an example with SE – Fabrizio Stellato Nov 30 '19 at 03:17
0

I changed batchet ref on my xml from:

  <batchlet ref="it.infocert.shop.main.SampleBatchlet">

to:

  <batchlet ref="sampleBatchlet">

now it works

Fabrizio Stellato
  • 1,727
  • 21
  • 52
  • JSR 352 prescribes 3 ways of batch artifact loading: (1) product-specific loader (CDI loader in your case); (2) Archive loader with batch.xml (mapping between ref name and batch artifact class); (3) class loader via fully-qualified class name. See JSR 352 spec section 10.5 Batch Artifact Loading. – cheng Dec 03 '19 at 15:40