Code example for ConcurrentMap

Methods: putIfAbsent, remove

0
public class LongRunningServiceInvocationHandler implements RemoteServiceInvocationHandler<LongRunningService> { 
    ConcurrentMap<String, Object> activeClients = new ConcurrentHashMap<String, Object>();
 
    @Override 
    public Object invoke(ClientContext client, ServiceReference reference, Method method, Object[] args) throws Exception {
        if (activeClients.putIfAbsent(client.getHostIPAddress(), Boolean.TRUE) != null)
            throw new RuntimeException("Only 1 concurrent invocation allowed per client.");
 
        try { 
            return method.invoke(new LongRunningServiceImpl(), args);
        } finally { 
            activeClients.remove(client.getHostIPAddress());
        } 
    } 
}