简体   繁体   中英

Java - How to send notification from server to client

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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