如何在Tomcat(servlet容器)中监听套接字? [英] How to listen a socket in Tomcat(servlet container)?

查看:348
本文介绍了如何在Tomcat(servlet容器)中监听套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须让web应用程序监听套接字( ServerSocket ),并处理套接字流。

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

但是应用程序是刚刚部署在Tomcat中,它只是一个servlet容器,它没有JCA支持。在servlet线程中建立服务器套接字是不合理的。

But the application is just deployed in Tomcat which is just a servlet container, it doesn't have JCA support. And establishing a server socket in servlet thread is unreasonable.

解决方案1:

丑陋的解决方案是写一个独立的java应用程序来监听套接字,然后与Web服务器通信。但它不是一个合理的解决方案。

The ugly solution is to write a standalone java application to listen socket, then communicate with web server. but it is not a sound solution.

解决方案2:

我写了一篇 ServletContextListener 当contextInitialized()开始监听套接字时,一旦收到套接字,它就会被一个线程传递。这意味着线程池位于ServletContext中。当然,我在contextDestroyed时销毁线程池。

I wrote a ServletContextListener which starts to listen socket when contextInitialized(), once receiving a socket, it will be handed by a thread. That means a thread pool is located in ServletContext. Of course, I destroy the thread pool when contextDestroyed.

您如何看待解决方案?

推荐答案

独立Java应用程序可以在 ServletContextListener 中执行。您可以使用 <$ c $管理线程池c> ExecutorService

The "standalone Java application" can be as good executed from inside a ServletContextListener. You can manage a threadpool using ExecutorService.

这篇关于如何在Tomcat(servlet容器)中监听套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆