【问题标题】:Run a method after Tomcat server startsTomcat服务器启动后运行一个方法
【发布时间】:2018-10-26 13:23:09
【问题描述】:

只有在 Tomcat 服务器启动并运行后,我才需要启动/运行方法(在一个类中声明的 3 个方法)。我浏览了几个线程,建议在web.xml 中使用Servlets(侦听器类)。问题是我的实现 ServletContextListener 的类甚至在加载 Tomcat 服务器完成之前运行。

注意:我的 web.xml 已标记为调度程序 servlet,以查找任何 REST API 调用。

Ex(以下是一些日志以帮助您了解更多信息):

INFO: No Spring WebApplicationInitializer types detected on classpath
Oct 26, 2018 6:20:43 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext   
.
{{ Connect with MYSQL }} 
.
Oct 26, 2018 6:20:50 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
.
18:20:51.394 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/user/someMethod],methods=[POST],produces=[application/xml || application/json]}" onto public java.util.ArrayList<com.x.y.z.User> com.z.y.controllers.UserController.deleteUser(com.x.y.z.User)
    18:20:52.090 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Oct 26 18:20:50 IST 2018]; parent: Root WebApplicationContext
    18:20:52.181 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Oct 26 18:20:50 IST 2018]; parent: Root WebApplicationContext
    18:20:52.394 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 2041 ms
    Oct 26, 2018 6:20:52 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8081"]
    Oct 26, 2018 6:20:52 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-bio-8010"]
    Oct 26, 2018 6:20:52 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 27236 ms

{{ I want to call methods after Tomcat starts }}

当我在ServletContextListener 下初始化类时,会抛出以下日志。

INFO: No Spring WebApplicationInitializer types detected on classpath
Oct 26, 2018 6:22:09 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
.
.
{{ Connect with MYSQL }}
.
18:22:16.461 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 6635 ms
**MyClass - myFileWatcher()
The above thread has started**
.
.

由于我的方法有线程,它甚至在 Tomcat 完全启动之前就卡住了(线程应该仅在显示“INFO: Server startup in 27236 ms”时启动)。

注意:如果我遗漏了什么,请告诉我。

【问题讨论】:

  • 您是否尝试在 web.xml 中使用 1 配置 servlet(包含您的初始化代码)?
  • @LeandroLuque 是的,这也显示了与 Listener 相同的输出。
  • Rishi,@lealceldeiro 的回答能解决问题吗?

标签: java spring multithreading tomcat


【解决方案1】:

你可以写一个@Bean,它提供一个CommandLineRunner。东西like this:

@Bean
public CommandLineRunner commandLineRunner(SomeAppService someAppService) {
    return strings -> {
        // do something with someAppService
        // this code will run just after the application has fully started
    };
}

还有其他方法可以做到这一点。你可以在这里阅读一些相关文章:

【讨论】:

  • 抱歉,我无法早点回复。非常感谢您的回答。我尝试实现但失败了,因为我需要更多地通过 Spring boot。实现这一点可能会解决问题,但是是否可以通过更经典的方式(使用线程左右)来解决它?感谢任何建议。
  • 这是怎么失败的?你能给我提供更多细节吗?如果你有我可以查看的 github 存储库,那就太好了!
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
相关资源
最近更新 更多