简体   繁体   English

如何控制在多个端口上侦听的Java服务器应用程序

[英]How can I control a java server app listening on multiple ports

First, forgive me if this has been answered already. 首先,请原谅我是否已经回答。 I spent a few hours searching the Internet (including stackoverflow) for my answer/good tips but what I am finding is not exactly what I am looking for so I decided to post...here goes. 我花了几个小时在Internet(包括stackoverflow)上搜索我的答案/好技巧,但是我发现的内容与我所寻找的不完全相同,因此我决定发布...继续。

I am writing a program to receive a "feed" over sockets on a single (Windows) machine. 我正在编写一个程序,以便通过一台(Windows)计算机上的套接字接收“订阅源”。 I will have 6 feeds coming in so I am listening on 6 ports (7000-7005). 我将有6个供稿,因此我正在6个端口(7000-7005)上监听。 Each "feed" will be certain data I want to read/parse and write to a DB. 每个“提要”将是我要读取/解析并写入数据库的某些数据。 The data over each socket will be sent to me every 15 seconds (eg data is refreshed every 15 seconds). 每个套接字上的数据将每15秒发送一次给我(例如,每15秒刷新一次数据)。

I have written a simple java program (modeled as a server program) that will simply listen on the specified port, receive the updates and write them to the DB. 我编写了一个简单的Java程序(建模为服务器程序),它将仅在指定的端口上侦听,接收更新并将它们写入数据库。 Let's generically call this the "listener" program. 我们通常将其称为“侦听器”程序。

I can change the port at the command line so what I've done in this early stage is to open 6 command prompts and I run java -jar myprogram.jar and I see what I expect to see. 我可以在命令行上更改端口,因此在此早期阶段,我要做的是打开6个命令提示符,然后运行java -jar myprogram.jar,然后看到了希望看到的内容。 My understanding of this setup is that I have 6 JVMs running (basically 6 separate processes). 我对此设置的理解是,我有6个JVM运行(基本上是6个独立的进程)。

My question revolves around controlling each listener. 我的问题围绕着控制每个听众。 I want to create an admin interface for the listeners...I want to be able to control the starting/stopping of each listener/port combo from a CENTRAL script/program. 我想为侦听器创建一个管理界面...我希望能够从CENTRAL脚本/程序控制每个侦听器/端口组合的启动/停止。

I've worked with Threads before and thought to modify my existing app to start and create 6 threads to handle the listening for each port. 我之前与Threads一起工作过,并考虑过修改我现有的应用程序以启动并创建6个线程来处理每个端口的侦听。 From the main program I can then invoke commands to control the child threads by port number or some name. 然后,从主程序中,我可以调用命令以通过端口号或某些名称控制子线程。

My thought/concern for that design is that what if I get an update for any feed where some significant change has occurred AFTER it's thread had "execution focus"? 我对设计的想法/关注的是,如果我对某个提要进行了任何重大更改之后的线程进行了“执行重点”更新,该怎么办? Threads are time-sliced usually right? 线程通常是按时间划分的,对吗? How to I ensure that these child threads are always ready to get the updates?? 我如何确保这些子线程始终准备好获取更新? I know CPUs switch fast :-) but isn't there SOME (albeit small) possibility that I miss an update by virtue of the time-slicing I'd think? 我知道CPU的切换速度很快:-),但是我是否会因为我想的时间限制而错过更新的可能性(虽然很小)?

So then I thought to keep it as-is and just run multiple instances of the program where each one is bound to a specific port. 因此,我当时想保持原样,只运行程序的多个实例,每个实例都绑定到一个特定的端口。 Ok..well if I do that, how am I controlling all instances from a single place? 好吧..如果我这样做了,我如何从一个地方控制所有实例? I've read about RMI a little..is that the answer? 我已经读过一些有关RMI的信息。这是答案吗? So I could call "remote" methods from a control program against the other instances of the server application. 因此,我可以针对服务器应用程序的其他实例从控制程序中调用“远程”方法。

I am looking for some tips/best-practice for this problem scenario. 我正在寻找一些解决此问题的技巧/最佳实践。 Hoping I've explained what the issue is clearly enough. 希望我已经解释了这个问题足够清楚了。 Thanks for taking a read and for posting any thoughts/links, etc. 感谢您阅读并发布任何想法/链接等。

What do you mean by timeslicing? 时间片是什么意思? The whole point of TCP is that data is verified. TCP的全部目的是验证数据。 Data won't just go lost even if the process/thread isn't responding. 即使进程/线程没有响应,数据也不会丢失。 The kernel will hold onto it until the connection times out. 内核将一直保留到连接超时。

I'd recommend just starting the Threads listening on different ports with a callback to the master handler (that launches the Threads), and then handling the messages there. 我建议仅通过侦听主端口处理程序(启动线程)的回调来启动在不同端口上侦听的线程,然后在此处处理消息。

You don't need multiple processes. 您不需要多个过程。

There is absolutely no problem with your scenario. 您的方案绝对没有问题。 Threads waiting on I/O operations (like reading from files, sockets, waiting on blocking locks, etc.) are blocked automatically from scheduling and are put to sleep until an input is detected. 等待I / O操作的线程(例如,从文件,套接字读取,等待阻塞锁等)将自动被调度,并进入睡眠状态,直到检测到输入为止。 Then, the operating system wakes up the sleeping threads so they can read the input coming from the sockets. 然后,操作系统唤醒睡眠线程,以便它们可以读取来自套接字的输入。

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

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