Java - 如何从服务器向客户端发送通知
我有一个服务器,我的java 网络应用程序和数据库 服务器驻留。
现在我的网络应用的客户端是 两种类型:
- 客户端类型1
- 客户端类型2
两者都可以访问数据库。
ClientType1 将数据存储在数据库中。
ClientType2 从数据库中检索数据。
但是ClientType1应该启动 将数据存储在数据库中 当 ClientType2 说 开始。
同样ClientType1应该停止将数据存储在数据库中 当ClientType2显示停止
时
=================================== ==============================================
第一季度。< /strong> 这个问题的解决方案是什么?
以下是我想到的方法:
在数据库中创建一个表,其中有一列显示状态开始或停止,该列的值应由ClientType2设置。 ClientType1 将不断向数据库发送查询,以从此表中获取状态,并根据状态执行操作。
应用ServerPush方法通过这种方式,服务器将保持与 ClientType1 的连接,并在收到命令(Start 或 Stop)时向他发送请求来自ClientType2。这种方法的问题是没有。服务器上打开的套接字数量将随着数量的增加而增加。 ClientType1
I am having a server where my java
web application and database
server reside.Now the clients of my web app are
of two types:
- ClientType1
- ClientType2
Both can access the database.
ClientType1 stores the data in the database.
ClientType2 retrieves the data from the database.
But the ClientType1 should start
storing the data in the database
when the ClientType2 says
Start.Similarily ClientType1 should stop storing the data in the database
when the ClientType2 says Stop
===========================================================================
Q1. What are the solutions for this problem?
Here are the approaches I thought of:
Create a table in the database having one column that shows the status Start or Stop and this column's value should be set by ClientType2. ClientType1 will keep sending the query to the database for getting the status from this table and perform operations according to the status.
Apply ServerPush approach by which the server will keep a connection with the ClientType1 alive and will send the request to him whenever it receives the command (Start or Stop) from the ClientType2. Problem with this approach is that the no. of open sockets at the sever will increase as the increase in the no. of ClientType1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您应该使用一种 Ajax,因为这抽象了“服务器可以调用客户端”。
选择一个允许您保持单个连接打开并通过此连接执行多项操作的库。
You should use a kind of Ajax for this since this abstracts the "server can call client" away.
Choose a library that allows you to keep a single connection open and do multiple things through this connection.
我并不认为通过数据库进行进程间通信是一个很好的方法。典型的场景是客户端向服务器注册自身,使其有资格使用基于套接字的机制接收消息。
然后,客户端可以:
如果有很多客户端保持打开连接将成为问题,您可以在每次需要时启动一个新连接与服务器通信或让服务器提供例如服务器可以轮询的某种 REST API。
I don't really consider interprocess communication through a database to be a great approach. The typical scenario is that the client registers itself with the server making it eligible for receiving messages using a socket-based mechanism.
The client can then either:
If there are that many clients that keeping an open connection will be a problem you could either initiate a new connection every time you need to communicate with the server or let the server offer e.g., some kind of REST API which the server can poll.