- There are multiple IntanceIDs, and SessionIDs for the same CollatorSN (unique physical device). So connections disconnect, leaving an orphaned Instance and then sometimes reconnect with the same SessionID and sometimes with a new SessionID.
I also created a third ThreadsafeMap which mapped the Wesocket connection's SessionID to the Websocket. From what I can see:-
Why do you need three maps? The map you posted above is fine. Do not worry about new instances. New instances are created after a WebSocket disconnects and then reconnects. It is up to you do re-associate you existing SessionID with the new websocket. If you create a new SessionID for a session that already existed, then that is an issue on your end. Workflow:
A) Client connects, creating new WebSocket instance. Client can either be
1) A new client. Generate a new CollatorSN. Generate a new CollatorData object and fill it. Place new CollatorData object into Main.CollatorMap using newly generated CallatorSN as key
2) An existing client. Use this client's CollatorSN and look it up in Main.CollatorMap.
a) If found, retrieve CollatorData and update any relevant information. Especially, assign the new WebSocket instance to CollatorData.ws
b) If not found, then
1) Either keep CollatorSN, create a new CollatorData object and fill it. Place new CollatorData object into Main.CollatorMap
2) Perform steps A.1
On top of that, set up a timer to manage time-out clients
When timer fires, step through Main.CollatorMap and check each client to see if they timed out. If they timed out, then remove from Main.CollatorMap.
With this setup, you should only have one active WebSocket connection per active client. When a client reconnects with an existing CollatorSN and you perform A.2.a, the previous WebSocket instance becomes unassigned (nothing is referencing it anymore) and whenever Java's garbage collector kicks in that instance will be cleaned up. If you keep multiple lists pointing to the WebSocket instance, you have to remove that instance from all lists or the garbage collector will not clean up the instance since it is still referenced by your application. One issue with this setup can be that if the timeout period is high, a bunch of WebSocket connections from new clients could create a bunch of new Main.CollatorMap entries and could exhaust your memory. But that would have to be a lot of new connections where new CollatorSN's would have to be created. If that is a concern, then you have to create another ThreadSafeMap that has the WebSocket instance as the key and the CollatorSN as the value. In your WebSocket Closed event you would have to then
1) Get the CollatorSN from this new ThreadSafeMap.
2) See if CollatorSN exists in Main.CollatorMap and if so remove it
3) Remove the WebSocket instance from the new ThreadSafeMap
I just don't know if it would be worth the effort to keep two maps, nor do I see a need for a third map. If your mapping becomes that convoluted, you may look into using a local SQLite database to store your extra information (creating your own session database). With such a database, no WebSocket instances are referenced and you would not have to worry about having stuck references that the garbage collector cannot clean up.