简体   繁体   English

Tomcat服务器启动后运行一个方法

[英]Run a method after Tomcat server starts

I need to start/run method (3 methods declared inside a class) only after Tomcat Server has started and ran.只有在 Tomcat 服务器启动并运行后,我才需要启动/运行方法(在类中声明的 3 个方法)。 I went over several threads suggesting using Servlet s (listener class) in web.xml .我浏览了几个线程,建议在web.xml使用Servlet s(侦听器类)。 The problem is my class which implements ServletContextListene r runs even before completion of loading Tomcat Server.问题是我的实现ServletContextListene r 的类甚至在加载 Tomcat 服务器完成之前运行。

Note: My web.xml is already tagged to dispatcher servlet looking for any REST API calls.注意:我的web.xml已被标记为调度程序 servlet,以查找任何 REST API 调用。

Ex (Below are few logs to help you understand more):例如(以下是一些日志以帮助您了解更多信息):

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 }}

When I initialize the class under ServletContextListener the below log throws up.当我在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**
.
.

As my method has thread it gets stuck even before Tomcat starts completely (the thread should start only when this displays ' INFO: Server startup in 27236 ms ').由于我的方法有线程,它甚至在 Tomcat 完全启动之前就卡住了(只有当显示“信息:服务器启动时间为 27236 毫秒”时,线程才应启动)。

Note: Let me know if I missed something.注意:如果我错过了什么,请告诉我。

You can write a @Bean which provides a CommandLineRunner .您可以编写一个提供CommandLineRunner@Bean Something 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
    };
}

There are other ways to do this.还有其他方法可以做到这一点。 You can read some related articles here:你可以在这里阅读一些相关的文章:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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