Set jvmRoute in spring boot 2.0.0

若如初见. 提交于 2019-12-06 02:50:01

You can customise Tomcat's Context a little more elegantly by using a context customiser. It's a functional interface so you can use a lambda:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return (tomcat) -> tomcat.addContextCustomizers((context) -> {
        Manager manager = context.getManager();
        if (manager == null) {
            manager = new StandardManager();
            context.setManager(manager);
        }
        manager.getSessionIdGenerator().setJvmRoute(jvmRoute);
    });
}

I'm using Spring Boot 2.0.4. The above answer did not work for me all the way. I had to update it this way:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainer() {
    return (tomcat) -> {

        tomcat.addContextCustomizers((context) -> {
            Manager manager = context.getManager();
            if (manager == null) {
                manager = new StandardManager();
                context.setManager(manager);
            }

            ((ManagerBase) context.getManager()).getEngine().setJvmRoute("tomcatJvmRoute");

        });
    };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!