0

如何在能够正常关闭的 Wildfly 上部署的战争容器内实现服务?我有以下示例代码:

SomeService.java:

@Stateless
public class SomeService {
    public void doThing() {
        // this will fail once the component is shut down.
        System.out.println("Doing some work...");
    }
}

SomeWorker.java:

@Singleton
@Startup
public class SomeWorker {

    @Inject
    private SomeService service;

    @Resource
    private TimerService timerService;

    @PostConstruct
    public void doWork() {
        System.out.println("Started startup bean...");
        timerService.createSingleActionTimer(10, new TimerConfig());
    }

    @Timeout
    public void doLater() {
        while (!Thread.interrupted()) {
            service.doThing();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        System.out.println("graceful shutdown");
    }
}

这段代码只是开始循环调用 EJB 的方法。如果这是一个纯 Java SE 应用程序(用相应的替换替换 EJB 注释),只需捕获InterruptedException就足以定期正常关闭(不是用 杀死 VM kill -9,但无论如何这超出了我的范围)。

但是在这种情况下,我不知道如何检测未部署的模块。如果我取消部署 war 文件(在我的情况下,通过将test.war.deployed文件重命名为test.war.undeployed),它只会开始最终引发ComponentIsStoppedExceptionEJB 访问:

14:14:17,250 INFO  [stdout] (EJB default - 1) Doing some work...
14:14:18,252 INFO  [stdout] (EJB default - 1) Doing some work...
14:14:19,253 INFO  [stdout] (EJB default - 1) Doing some work...
14:14:19,808 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) WFLYDS0033: Deployment test.war was previously undeployed by this scanner but has been redeployed by another management tool. Marker file D:\<path>\wildfly\standalone\deployments\test.war.undeployed is being removed to record this fact.
14:14:19,831 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 67) WFLYUT0022: Unregistered web context: '/test' from server 'default-server'
14:14:20,256 ERROR [org.jboss.as.ejb3.timer] (EJB default - 1) WFLYEJB0020: Error invoking timeout for timer: [id=4de4a666-5a6e-4830-aa89-21d2040d04f0 timedObjectId=test.test.SomeWorker auto-timer?:false persistent?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@526b0669 initialExpiration=Thu Oct 18 14:14:09 CEST 2018 intervalDuration(in milli sec)=0 nextExpiration=null timerState=IN_TIMEOUT info=null]: javax.ejb.EJBException: org.jboss.as.ee.component.ComponentIsStoppedException: WFLYEE0043: Component is stopped
        ...
Caused by: org.jboss.as.ee.component.ComponentIsStoppedException: WFLYEE0043: Component is stopped
        at org.jboss.as.ee.component.BasicComponent.waitForComponentStart(BasicComponent.java:110)
        at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:194)
        at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:185)
        at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:81)
        at test.SomeService$$$view1.doThing(Unknown Source)
        ...
        at test.SomeService$Proxy$_$$_Weld$EnterpriseProxy$.doThing(Unknown Source)
        at test.SomeWorker.doLater(SomeWorker.java:31)
        ...
4

1 回答 1

1

检查正在取消部署的模块是否正在通过带@PreDestroy注释的方法:

@PreDestroy
public void doShutdown() {
   System.out.println("Shutting down. Please wait...");
   for (Timer timer : timerService.getTimers() {
      timer.cancel();
   } 
}
于 2018-10-19T20:15:10.533 回答