简体   繁体   中英

java TCP Server inside Tomcat web app

Folks, i have an java server application running stand alone, this will get tcp connection and do some processing and send back some response as well. and i want to run this inside a Tomcat web server along with a web applcation. i will start the server application by mean of servlet using onload =1 in web.xml. so upon start of tomcat the server application is started as well. i expect both web applcaition and server application to run with the tomcat and share some singleton classes and common classes between them. i also have hibernate inside and would except that no issues with deployment of applications. kindly let me know if there are any issues with this or any suggestions.

Here are a couple of things to consider:

  1. The secondary server you are starting needs to fork and have its own thread. If you don't do this, Tomcat may not be able to complete the startup process. This may not be an issue when starting it from a Servlet, but it might if you follow the advice in #2.
  2. The secondary server needs to be able to receive some type of signal to know for it to shut down. Rather than use a Servelet for startup, I would suggest a ServletContextListener . It is able to receive startup and shutdown events, so you could init and destroy the secondary server from there.

Your ServletContextListener will need to implement the following methods:

public void contextInitialized(ServletContextEvent event)

public void contextDestroyed(ServletContextEvent event)

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