开发者

Starting Servlet automatically when tomcat starts...? [duplicate]

开发者 https://www.devze.com 2023-02-17 03:51 出处:网络
This question already has an answer here: Using special auto start servlet to initialize on startup and share application data
This question already has an answer here: Using special auto start servlet to initialize on startup and share application data (1 answer) 开发者_开发百科 Closed 7 years ago.

I have a requirement where , I have one table in that columns are phno , name , id. When ever I update the table one servlet need to read that table and gather phno's continuously with out giving any request to that servlet. Then we can get phno's which are new ones and send sms to those only.

Any one have idea on this send sms. I am using java spring's ,tomcat. Is there any other way to do this.


Don't use a HttpServlet if you don't want to fire HTTP requests on it at all. Use a ServletContextListener.

public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Do some stuff during webapp's startup.
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Do some stuff during webapp's shutdown.
    }

}

Register it as <listener> entry in web.xml or when you're already on Servlet 3.0, by @WebListener annotation.

From this listener on, you could use ExecutorService to run background threads. Or, when your servletcontainer/appserver supports it (Tomcat doesn't), rather utilize its job scheduling facilities.


If you want to have a servlet loaded on startup, add the load-on-startup element of the servlet you would like to load on startup inside web.xml:

<servlet>
    <servlet-name>MyStartupServlet</servlet-name>
    <servlet-class>com.package.MyStartupServlet</servlet-class>
    <load-on-startup/>
</servlet> 


Look into SMS Gateway. You might need to subscribe to some SMS Gateway in order to send SMS from your program.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号