4

VAVR collections are "immutable".

So, if I have static variable, for example, holding all the WebSocket sessions, how would I use VAVR so that the collection is thread safe?

For example:

@ServerEndpoint("/actions")
public class DeviceWebSocketServer {

    private static Set<Session> sessions = //???; // how should I initialize this?

    @OnOpen
    public void open(Session session) {
        sessions = sessions.add(session); // is this OK???
    }

    @OnClose
    public void close(Session session) {
        sessions = sessions.remove(session); // is this OK??
    }
}    
Rafael Paulino
  • 570
  • 2
  • 9

2 Answers2

11

You can wrap the immutable vavr collection in an atomically updatable AtomicReference, and use one of its update methods to atomically update the reference to the immutable collection.

@ServerEndpoint("/actions")
public class DeviceWebSocketServer {

    private static AtomicReference<Set<Session>> sessionsRef = 
            new AtomicReference<>(HashSet.empty());

    @OnOpen
    public void open(Session session) {
        sessionsRef.updateAndGet(sessions -> sessions.add(session));
    }

    @OnClose
    public void close(Session session) {
        sessionsRef.updateAndGet(sessions -> sessions.remove(session));
    }

}

Make sure you read the javadoc of AtomicReference if you are going to use them in other scenarios, as there are some requirements on the update functions that need to be respected to get correct behavior.

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47
-1

For this use case you could also consider using a concurrent map:

@ServerEndpoint("/actions")
public class DeviceWebSocketServer {

    private static ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<>();

    @OnOpen
    public void open(Session session) {
        sessions = sessions.putIfAbsent(session.getId(), session);
    }

    @OnClose
    public void close(Session session) {
        sessions = sessions.remove(session.getId());
    }

}
Xavier Arias
  • 162
  • 2
  • 10
  • 2
    Keep in mind that ConcurrentHashMap has only save method synchronized. Read methods are not blocked so you can get dirty values from the map. – eL_ Jun 05 '20 at 05:41