/** * Register an active operation with a specific operation id. * * @param id the operation id * @param attachment the shared attachment * @param callback the completed callback * @return the created active operation */ protected ActiveOperation<T, A> registerActiveOperation( final Integer id, A attachment, ActiveOperation.CompletedCallback<T> callback) { lock.lock(); try { // Check that we still allow registration assert !shutdown; final Integer operationId; if (id == null) { // If we did not get a operationId, create a new one operationId = operationIdManager.createBatchId(); } else { // Check that the operationId is not already taken if (!operationIdManager.lockBatchId(id)) { throw ProtocolMessages.MESSAGES.operationIdAlreadyExists(id); } operationId = id; } final ActiveOperationImpl<T, A> request = new ActiveOperationImpl(operationId, attachment, callback); final ActiveOperation<?, ?> existing = activeRequests.putIfAbsent(operationId, request); if (existing != null) { throw ProtocolMessages.MESSAGES.operationIdAlreadyExists(operationId); } activeCount++; // condition.signalAll(); return request; } finally { lock.unlock(); } }
/** * Remove an active operation. * * @param id the operation id * @return the removed active operation, {@code null} if there was no registered operation */ private ActiveOperation<T, A> removeActiveOperation(final Integer id) { lock.lock(); try { final ActiveOperation<T, A> removed = activeRequests.remove(id); if (removed != null) { activeCount--; operationIdManager.freeBatchId(id); condition.signalAll(); } return removed; } finally { lock.unlock(); } }