简体   繁体   中英

Websockets on Tomcat: how to shutdown the server cleanly?

如何拦截服务器上运行的端点的关闭事件(比如servlet的destroy方法,可以这么说)用于日志记录或其他目的?

Spring 4 Java Config:

Implement SmartLifecycle (when using Java based configuration, or translate the Java code to XML):

@Configuration
public class CycleBean implements SmartLifecycle {

    public CycleBean() {
    }

    @Override
    public boolean isRunning() {
        return true;
    }

    @Override
    public void start() {
    }

    @Override
    public void stop() {
        //when stopped call 
    }

    @Override
    public int getPhase() {
        return 0;
    }

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable arg0) {
        //
    }
}

See http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html paragraph 4.6 Customizing the nature of a bean, like:

<bean id="lifecycleProcessor" class="org.springframework.context.support.DefaultLifecycleProcessor">
  <!-- timeout value in milliseconds -->
  <property name="timeoutPerShutdownPhase" value="10000"/>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM