Search in sources :

Example 1 with LockedExternalTaskDto

use of org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto in project camunda-bpm-platform by camunda.

the class FetchAndLockHandlerImpl method addPendingRequest.

@Override
public void addPendingRequest(FetchExternalTasksExtendedDto dto, AsyncResponse asyncResponse, ProcessEngine processEngine) {
    Long asyncResponseTimeout = dto.getAsyncResponseTimeout();
    if (asyncResponseTimeout != null && asyncResponseTimeout > MAX_TIMEOUT) {
        invalidRequest(asyncResponse, "The asynchronous response timeout cannot be set to a value greater than " + MAX_TIMEOUT + " milliseconds");
        return;
    }
    IdentityService identityService = processEngine.getIdentityService();
    Authentication authentication = identityService.getCurrentAuthentication();
    FetchAndLockRequest incomingRequest = new FetchAndLockRequest().setProcessEngine(processEngine).setAsyncResponse(asyncResponse).setAuthentication(authentication).setDto(dto);
    FetchAndLockResult result = tryFetchAndLock(incomingRequest);
    if (result.wasSuccessful()) {
        List<LockedExternalTaskDto> lockedTasks = result.getTasks();
        if (!lockedTasks.isEmpty() || dto.getAsyncResponseTimeout() == null) {
            // response immediately if tasks available
            asyncResponse.resume(lockedTasks);
        } else {
            addRequest(incomingRequest);
        }
    } else {
        ProcessEngineException processEngineException = result.getProcessEngineException();
        asyncResponse.resume(processEngineException);
    }
}
Also used : IdentityService(org.camunda.bpm.engine.IdentityService) Authentication(org.camunda.bpm.engine.impl.identity.Authentication) LockedExternalTaskDto(org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 2 with LockedExternalTaskDto

use of org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto in project camunda-bpm-platform by camunda.

the class FetchAndLockHandlerImpl method acquire.

protected void acquire() {
    queue.drainTo(pendingRequests);
    // timestamp
    long backoffTime = MAX_BACK_OFF_TIME;
    Iterator<FetchAndLockRequest> iterator = pendingRequests.iterator();
    while (iterator.hasNext()) {
        FetchAndLockRequest pendingRequest = iterator.next();
        long currentTime = ClockUtil.getCurrentTime().getTime();
        FetchAndLockResult result = tryFetchAndLock(pendingRequest);
        if (result.wasSuccessful()) {
            List<LockedExternalTaskDto> lockedTasks = result.getTasks();
            long timeout = pendingRequest.getTimeoutTimestamp();
            if (!lockedTasks.isEmpty() || timeout <= currentTime) {
                AsyncResponse asyncResponse = pendingRequest.getAsyncResponse();
                iterator.remove();
                asyncResponse.resume(lockedTasks);
            } else {
                if (timeout < backoffTime) {
                    backoffTime = timeout;
                }
            }
        } else {
            AsyncResponse asyncResponse = pendingRequest.getAsyncResponse();
            iterator.remove();
            ProcessEngineException processEngineException = result.getProcessEngineException();
            asyncResponse.resume(processEngineException);
        }
    }
    suspend(Math.max(0, backoffTime - ClockUtil.getCurrentTime().getTime()));
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) LockedExternalTaskDto(org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 3 with LockedExternalTaskDto

use of org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto in project camunda-bpm-platform by camunda.

the class FetchAndLockHandlerImpl method tryFetchAndLock.

protected FetchAndLockResult tryFetchAndLock(FetchAndLockRequest request) {
    ProcessEngine processEngine = request.getProcessEngine();
    IdentityService identityService = processEngine.getIdentityService();
    FetchAndLockResult result;
    try {
        identityService.setAuthentication(request.getAuthentication());
        FetchExternalTasksExtendedDto fetchingDto = request.getDto();
        List<LockedExternalTaskDto> lockedTasks = executeFetchAndLock(fetchingDto, processEngine);
        result = FetchAndLockResult.successful(lockedTasks);
    } catch (ProcessEngineException e) {
        result = FetchAndLockResult.failed(e);
    } finally {
        identityService.clearAuthentication();
    }
    return result;
}
Also used : IdentityService(org.camunda.bpm.engine.IdentityService) FetchExternalTasksExtendedDto(org.camunda.bpm.engine.rest.dto.externaltask.FetchExternalTasksExtendedDto) LockedExternalTaskDto(org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) ProcessEngine(org.camunda.bpm.engine.ProcessEngine)

Aggregations

ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)3 LockedExternalTaskDto (org.camunda.bpm.engine.rest.dto.externaltask.LockedExternalTaskDto)3 IdentityService (org.camunda.bpm.engine.IdentityService)2 AsyncResponse (javax.ws.rs.container.AsyncResponse)1 ProcessEngine (org.camunda.bpm.engine.ProcessEngine)1 Authentication (org.camunda.bpm.engine.impl.identity.Authentication)1 FetchExternalTasksExtendedDto (org.camunda.bpm.engine.rest.dto.externaltask.FetchExternalTasksExtendedDto)1