Java Code Examples for com.hazelcast.map.IMap#lock()

The following examples show how to use com.hazelcast.map.IMap#lock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HazelcastLockProvider.java    From ShedLock with Apache License 2.0 6 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    log.trace("lock - Attempt : {}", lockConfiguration);
    final Instant now = ClockProvider.now();
    final String lockName = lockConfiguration.getName();
    final IMap<String, HazelcastLock> store = getStore();
    try {
        // lock the map key entry
        store.lock(lockName, keyLockTime(lockConfiguration), TimeUnit.MILLISECONDS);
        // just one thread at a time, in the cluster, can run this code
        // each thread waits until the lock to be unlock
        if (tryLock(lockConfiguration, now)) {
            return Optional.of(new HazelcastSimpleLock(this, lockConfiguration));
        }
    } finally {
        // released the map lock for the others threads
        store.unlock(lockName);
    }
    return Optional.empty();
}
 
Example 2
Source File: ClientManager.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
/**
 * This method will return count of users in room _after_ adding
 *
 * @param c - client to be added to the room
 * @return count of users in room _after_ adding
 */
public int addToRoom(Client c) {
	Room r = c.getRoom();
	Long roomId = r.getId();
	confLogDao.add(
			ConferenceLog.Type.ROOM_ENTER
			, c.getUserId(), "0", roomId
			, c.getRemoteAddress()
			, String.valueOf(roomId));
	log.debug("Adding online room client: {}, room: {}", c.getUid(), roomId);
	IMap<Long, Set<String>> rooms = rooms();
	rooms.lock(roomId);
	rooms.putIfAbsent(roomId, ConcurrentHashMap.newKeySet());
	Set<String> set = rooms.get(roomId);
	set.add(c.getUid());
	final int count = set.size();
	rooms.put(roomId, set);
	onlineRooms.put(roomId, set);
	rooms.unlock(roomId);
	String serverId = c.getServerId();
	addRoomToServer(serverId, r);
	update(c);
	return count;
}
 
Example 3
Source File: ClientManager.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
public void exitRoom(Client c, boolean update) {
	Long roomId = c.getRoomId();
	log.debug("Removing online room client: {}, room: {}", c.getUid(), roomId);
	if (roomId != null) {
		IMap<Long, Set<String>> rooms = rooms();
		rooms.lock(roomId);
		Set<String> clients = rooms.get(roomId);
		if (clients != null) {
			clients.remove(c.getUid());
			rooms.put(roomId, clients);
			onlineRooms.put(roomId, clients);
		}
		rooms.unlock(roomId);
		if (clients == null || clients.isEmpty()) {
			String serverId = c.getServerId();
			IMap<String, ServerInfo> servers = servers();
			servers.lock(serverId);
			ServerInfo si = servers.get(serverId);
			si.remove(c.getRoom());
			servers.put(serverId, si);
			onlineServers.put(serverId, si);
			servers.unlock(serverId);
		}
		kHandler.leaveRoom(c);
		c.setRoom(null);
		c.clear();
		if (update) {
			update(c);
		}

		sendRoom(new TextRoomMessage(roomId, c, RoomMessage.Type.ROOM_EXIT, c.getUid()));
		confLogDao.add(
				ConferenceLog.Type.ROOM_LEAVE
				, c.getUserId(), "0", roomId
				, c.getRemoteAddress()
				, String.valueOf(roomId));
	}
}
 
Example 4
Source File: ClientManager.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
private void addRoomToServer(String serverId, Room r) {
	if (!onlineServers.get(serverId).getRooms().contains(r.getId())) {
		log.debug("Cluster:: room {} was not found for server '{}', adding ...", r.getId(), serverId);
		IMap<String, ServerInfo> servers = servers();
		servers.lock(serverId);
		ServerInfo si = servers.get(serverId);
		si.add(r);
		servers.put(serverId, si);
		onlineServers.put(serverId, si);
		servers.unlock(serverId);
	}
}
 
Example 5
Source File: QuickPollManager.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
public void start(Client c) {
	Long roomId = c.getRoomId();
	if (!c.hasRight(Room.Right.PRESENTER) || isStarted(roomId)) {
		return;
	}
	log.debug("Starting quick poll, room: {}", roomId);
	IMap<Long, Map<Long, Boolean>> polls = map();
	polls.lock(roomId);
	polls.putIfAbsent(roomId, new ConcurrentHashMap<Long, Boolean>());
	polls.unlock(roomId);
	WebSocketHelper.sendRoom(new TextRoomMessage(roomId, c, Type.QUICK_POLL_UPDATED, c.getUid()));
}