Websocket Monitoring in Java Spring Boot - java

From our application we establish multiple websocket client connections to a websocket server, one for each registered user.
I need to monitor all the connections separately, so that a super user can monitor socket stream for each user. Say in case of a failure i.e, one of the user's socket connection is not available, then it can be identified and initiated manually at least. Also for the purpose of maintenance tasks like start, stop and keep alive each individual socket connection from a browser as part of the application we are intended to develop.
Using org.java_websocket.client.WebSocketClient I checked to see if args from onOpen() method gives any value that I can use to track each and every socket connection separately.
I also tried if we can retrieve the local port number assigned to each individual connection. So that it can be used to perform the task.

Related

Exiting of one program, not more

So basically I'm doing a server and client in java.
In order for it to run, I have to run both a client.java and server.java. I need to close one or both depending on the instance
What I currently need is a way such that when the client passes a parameter x, the client should close but the server should remain open. I also have to implement it in such a way that if client passes parameter y instead, both the client and server should close
I thought of doing:
System.exit(0);
But I'm not sure if this would close both server and client, meaning that it would be useful for the second instance. I also thought of just letting it reach the end of the program, but I'm not really sure what result would be the result of that.
If you want the server to keep listening for more requests for a handshake (even when previous connections terminate), what you need is a multi-client server. A server that can handle multiple clients usually runs a thread that keeps looping and listens for a client that wants to connect. Such a thread would generate a new thread for each connection and store it in an array of threads that all keep listening or talking with each client.
You can learn more about multiple client servers here.
With the array of threads listening to all clients, you could assign a condition that terminates all threads and connections in the server. This could be a String that one of the clients can relay to the server, such as "/exit" and the server can check if information sent by any client-thread is "/exit".

How could a server check the availability of a client?

I have classic http client/server application where the server serves the clients data at their will but also performs some kind of call-backs to the list of clients' addresses it has. My two questions are :
1- How would the server know if a client is down (the client did not disconnect but the connection got suddenly interrupted) ?
2- Is there a way to know from the server-side if the process at client-side listening on the call-back port is still up (i.e. client call-back socket is still open) ?
1- How would the server know if a client is down (the client did not disconnect but the connection got suddenly interrupted) ?
Option #1: direct communication
Client tells server "I'm alive" at a periodic interval. You could make your client to ping your server at a configurable interval, and if the server does not receive the signal for a certain time, it'll mark the client as down. Client could even tell server more info(e.g. It's status) in each heartbeat if necessary, this is also the way used in many distributed systems(e.g. Hadoop/Hbase).
Option #2: distributed coordination service
You could treat all clients connected to a server as a group, and use a 3rd party distributed coordination service like Zookeeper to facilitate the membership management. Client registers itself to Zookeeper as a new member of the group right after booting up, and leaves the group if it's down. Zookeeper notifies the server whenever the membership changes.
2- Is there a way to know from the server-side if the process at client-side listening on the call-back port is still up (i.e. client call-back socket is still open) ?
I think this can only be done by the way Option #1 listed above. It could be either the way clients tell server "My callback port is OK" at a fixed interval, or the server asks clients "Are your callback port OK?" and wait its response at a fixed interval
You would have to establish some sort of protocol; and simply spoken: the server keeps track of "messages" that it tried to sent to clients.
If that "send" is acknowledged, fine; if not: then the server might do a limited number of retries; and then regard that client as "gone"; and then drop any other messages for that client.
1- How would the server know if a client is down (the client did not disconnect but the connection got suddenly interrupted) ?
A write to the client will fail.
2- Is there a way to know from the server-side if the process at client-side listening on the call-back port is still up (i.e. client call-back socket is still open
A write to the client will fail.
The write won't necessarily fail immediately, due to TCP buffering, but the write will eventually provoke retries and retry timeouts that will cause a subsequent read or write to fail.
In Java the failure will manifest itself as an IOException: connection reset.

How to use Netty clients within Netty server

I'm going to create an authentication server which itself interacts with
a set of different Oauth2.0 servers.
Netty seems to be a good candidate to implement network part here.
But before start I need to clear some details about netty as I'm new to it.
The routine will be as follows:
The server accepts an HTTPS connection from a client.
Then, not closing this first connection, it makes another connection
via HTTPS to a remote Oauth2.0 server and gets data
After all, the server sends the result back to the client which is supposed to keep the connection alive.
How to implement this scenario with Netty?
Do I have to create a new netty client and/or reconnect it each time I need to connect to a remote Oauth2.0 server?
If so, I'll have to create a separate thread for every
outgoing connection which will drastically reduce performance.
Another scenario is to create a sufficient number of Netty clients
within a server at the beginning (when server starts)
and keep them constantly connected to the Oauth2.0 servers via HTTPS.
That's easily done with Netty. First you set up your Netty server using the ServerBootstrap and then in a ChannelHandler that handles your connection from the client you can use e.g. the client Bootstrap to connect to the OAuth server and fetch the data. You don't need to worry about creating threads or similar. You can do it all in a non-blocking fashion. Take a look at and try to understand how this example works:
https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/proxy/HexDumpProxyFrontendHandler.java#L44.

Two Way Connection

I'm making a java program & I want this to be both as server and a client (using sockets). How is this best achieved?
If you mean that you want to both send and receive data, a single regular socket (on each computer) will do just fine. See Socket.getInputStream and Socket.getOutputStream.
The usual "server" / "client" distinction just boils down which host is listening for incoming connections, and which hosts connect to those hosts. Once the connection is setup, you can both send and receive from both ends.
If you want both hosts to listen for incoming connections, then just set up a ServerSocket and call accept on both hosts.
Related links:
Official trail: The Java™ Tutorials, Lesson: All About Sockets
If you want each station to function as a server and a client, like a p2p chat,
you should implement a thread with a ServerSocket, listening for incoming connections, and once it got a connection, open a new thread to handle it so the current one will keep on listening for new connections.
For it to be able to connect to others, simple use SocketAddress and Socket, in a different thread to try to connect to a specified server address (e.g. by a list of the user's friends)
you can find plenty of chat examples by googling.
cheers.
If you want the program to perform the same operations regardless of whether it is a server or a client for a certain connection, I could imagine handing off both the client Socket and the ServerSocket.accept()-produced socket to the same method for processing.
Have a look at jgroups it's a library that allows the creation of groups of processes whose members can send messages to each other. Another option would be to use hazelcast...
You may also look at this question.
The best way to do this is to run the server on a thread:
You run server.accept() therefore while your program is listening for a connection on that thread you can do whatever you want on the main thread, even connect to another server therefore making the program both a server & a client.

Check number of connections in the queue of a server socket in Java

I have a Java program that creates a server socket and accepts connections from various clients.
I am interested in finding out the number of connections that are in queue (waiting to be processed by the server). I have used the default constructor without specifying the backlog parameter.
At runtime, is it possible to know how many connections are pending for the server?
I want to implement a monitor process, which will check how full the queue is and based on that, trigger clone processes for load balancing (so that the connections are not dropped).
According to this SO answer, you can't.

Categories