How to listen on a socket in Tomcat (servlet container)?

I have to let the web application listen on the socket (ServerSocket) and handle the socket stream.

But the application is simply deployed to Tomcat, which is just a servlet container, it does not have JCA support. And creating a server socket in a servlet thread is unreasonable.

solution 1:

An ugly solution is to write a stand-alone Java application to listen on a socket, and then contact the web server. but this is not a sound solution.

solution 2:

I wrote a ServletContextListener that starts listening on a socket when contextInitialized (), having received the socket, will be streamed. This means that the thread pool is in the ServletContext. Of course, I destroy the thread pool when contextDestroyed.

What do you think of the solutions?

+2
source share
1 answer

A "stand-alone Java application" can be performed just as well internally ServletContextListener. You can manage threadpool using ExecutorService.

+3
source

All Articles