Code example for ConcurrentMap

Methods: removeputIfAbsent

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());
        } 
    } 
} 
Stop searching for code, let great code find you!  Add Codota to your java IDE