Wildfly初始化:时间延迟导致Java-EE应用程序中的IllegalStateException

时间:2018-10-31 11:10:44

标签: java-ee jboss wildfly illegalstateexception

我有一个简单的Java项目,该项目正在获取和计算称为CalculatorService的数据。它通过buildpath包含在部署在wildfly服务器上的JavaEE应用程序中。

当我运行wildfly服务器时,将建立数据库及其表。之后,InitializationBean正在运行,其中通过所谓的CalculatorService获取了一些数据。之后,该数据将被存储在数据库中。让我们看一下JavaEE应用程序的InitializationBean

@Singleton
@Startup
public class InitializationBean {

    @Inject
    private InitService initService;

    @PostConstruct
    private void initialize() {
        // Start CalculatorService and fetch data.
        CalculatorService calcService = new CalculatorService();
        calcService.fetchData();
        System.out.println("DONE <===========");
        initService.storeData();
    }
}

InitService如下:

@Singleton
public class InitService {

    @EJB
    private DataDAO dataDAO;

    public void storeData() {
        // Get fetched data stored within CalculatorService.
        Set <DataCalc> dataSet = CalculatorService.getAllData();
        // Loop through fetched data of CalculatorService and transform to valid entity.
        for (DataCalc d : dataSet) {
            Data data = new Data();
            data.setName(d.getName());
            try { // Store in database
                dataDAO.create(data);
            } catch (Exception e) { e.printStackTrace(); }
    }
    }
}

如果我使用测试数据通过上述设置运行服务器,而不是CalculatorService一切正常运行。但是,一旦使用该服务,我将得到以下输出:

DONE <===========
ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 88) MSC000001: Failed to start service jboss.deployment.subunit."MyProject.ear"."MyProject.jar".component.InitializationBean.START: org.jboss.msc.service.StartException in service jboss.deployment.subunit."MyProject.ear"."MyProject.jar".component.InitializationBean.START: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.ComponentStartService$1.run(ComponentStartService.java:57)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.jboss.threads@2.3.2.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.3.2.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
    at org.jboss.threads@2.3.2.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
    at org.jboss.threads@2.3.2.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
    at java.base/java.lang.Thread.run(Thread.java:844)
    at org.jboss.threads@2.3.2.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)
Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:134)
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:88)
    at org.jboss.as.ejb3@14.0.1.Final//org.jboss.as.ejb3.component.singleton.SingletonComponent.getComponentInstance(SingletonComponent.java:127)
    at org.jboss.as.ejb3@14.0.1.Final//org.jboss.as.ejb3.component.singleton.SingletonComponent.start(SingletonComponent.java:141)
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.ComponentStartService$1.run(ComponentStartService.java:54)
    ... 8 more
Caused by: java.lang.IllegalStateException
    at org.jboss.msc@1.4.3.Final//org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:50)
    at org.jboss.as.ejb3@14.0.1.Final//org.jboss.as.ejb3.deployment.ModuleDeployment$ModuleDeploymentStartService$1.run(ModuleDeployment.java:102)
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.deployers.StartupCountdown.countDown(StartupCountdown.java:27)
    at org.jboss.as.ejb3@14.0.1.Final//org.jboss.as.ejb3.component.singleton.StartupCountDownInterceptor.processInvocation(StartupCountDownInterceptor.java:28)
    at org.jboss.invocation@1.5.1.Final//org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation@1.5.1.Final//org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53)
    at org.jboss.as.ee@14.0.1.Final//org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
    ... 13 more

因为CalculatorService的处理时间很长(超过两个小时),所以我认为它与时间有关。但是由于没有TimeoutException这样的东西,所以我不知道从哪里开始。

更新

CalculatorService必须从数据中取出少量内容时,只需花费几秒钟的时间,就不会引发异常,并且数据存储成功。

1 个答案:

答案 0 :(得分:0)

在您的storeData()方法中添加一个@javax.ejb.Asynchronous注释:

...
@Asynchronous
public void storeData() {
    ...

这将允许您的应用程序在storeData()在后​​台线程中运行时正常启动。

您可能还希望将CalculatorService调用移到该方法中...