简体   繁体   English

Java-如何将通知从服务器发送到客户端

[英]Java - How to send notification from server to client

I am having a server where my java web application and database server reside. 我有一台Java Web应用程序数据库服务器所在的服务器

Now the clients of my web app are of two types: 现在,我的Web应用程序的客户端有两种类型:

  • ClientType1 ClientType1
  • ClientType2 ClientType2

Both can access the database. 两者都可以访问数据库。

ClientType1 stores the data in the database. ClientType1将数据存储在数据库中。

ClientType2 retrieves the data from the database. ClientType2从数据库检索数据。

But the ClientType1 should start storing the data in the database when the ClientType2 says Start . 但是,当ClientType2说出Start时, ClientType1应该开始将数据存储在数据库中。

Similarily ClientType1 should stop storing the data in the database when the ClientType2 says Stop 相若方式ClientType1应停止存储在数据库中的数据ClientType2停止

=========================================================================== ================================================== ========================

Q1. 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 . 在数据库中创建一个表,该表的一列显示状态“ 开始”或“ 停止”,并且此列的值应由ClientType2设置。 ClientType1 will keep sending the query to the database for getting the status from this table and perform operations according to the status. ClientType1将继续将查询发送到数据库,以从该表中获取状态,并根据状态执行操作。

  • 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 . 应用ServerPush方法,通过该服务器将保持与ClientType1活着的连接,并将请求时,它接收来自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 ClientType1

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: 然后,客户可以:

  • Ask the server to perform a database operation on behalf of it 要求服务器代表它执行数据库操作
  • Request access to the database 请求访问数据库

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 eg, some kind of REST API which the server can poll. 如果存在许多保持打开连接的客户端的问题,则可以在每次需要与服务器通信时启动一个新的连接,或者让服务器提供例如服务器可以轮询的某种REST API。

You should use a kind of Ajax for this since this abstracts the "server can call client" away. 您应该为此使用一种Ajax,因为这样可以抽象“服务器可以调用客户端”。

Choose a library that allows you to keep a single connection open and do multiple things through this connection. 选择一个允许您保持单个连接打开并通过该连接执行多项操作的库。

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

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