妥善处理wildfly组件关闭

时间:2018-10-18 12:34:17

标签: java-ee wildfly shutdown wildfly-11

如何在部署在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就足以正常进行正常关机(不会用kill -9杀死VM,但这超出了范围反正我)。

但是在这种情况下,我不知道如何检测到模块已取消部署。如果我取消部署war文件(在我的情况下,将test.war.deployed文件重命名为test.war.undeployed),它将最终开始在EJB访问中抛出ComponentIsStoppedException

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)
        ...

1 个答案:

答案 0 :(得分:1)

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

@PreDestroy
public void doShutdown() {
   System.out.println("Shutting down. Please wait...");
   for (Timer timer : timerService.getTimers() {
      timer.cancel();
   } 
}