Java批处理 - 将ejb注入到批处理中

时间:2018-01-19 21:22:22

标签: ejb cdi java-batch jberet jbatch

我有一个启动bean。我想在此开始一些小程序工作。 我使用@Nemed@Dependent对批处理类进行了注释。我想在batchlet中使用一些像ReportService这样的ejb,但是注入不起作用。如何将EJB注入我的小程序? 我在wildfly 11.0.0.Alpha1上部署了以下示例,并在服务对象中获得了空引用。

BatchletTest:

@Dependent
@Named("BatchletTest")
public class BatchletTest extends AbstractBatchlet{


    public BatchletTest() {
    }

    @Inject
    ReportService service;

    @Override
    public String process() throws Exception {
        System.out.println(service);
        return null;
    }
}

测试job.xml

 <job id="test-job" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
        <step id="testStep">
            <batchlet ref="com.test.BatchletTest" />
        </step>
    </job>

StartupBean:

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public class StartupBean {

    private Logger logger = LoggerFactory.getLogger(StartupBean.class);

    @PostConstruct
    private void startup() throws Exception {
            long executionId = BatchRuntime.getJobOperator().start("test-job", new Properties());
            System.out.println("myJob started, execution ID = " + executionId);

    }

}

ReportService的:

@Stateless
public class ReportService {
.....
}

1 个答案:

答案 0 :(得分:0)

您没有在类ReportService上使用@Local anantotaion实现任何接口。

尝试一下:

@Stateless
@LocalBean
public class ReportService {
.....
}

@Stateless
public class ReportService implements ReportServiceLocal{
.....
}

@Local
public interface ReportServiceLocal {
.....
}

请选中此link