Java Code Examples for org.camunda.bpm.engine.impl.interceptor.CommandContext#registerCommandContextListener()

The following examples show how to use org.camunda.bpm.engine.impl.interceptor.CommandContext#registerCommandContextListener() . 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: ProgrammaticBeanLookup.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private static void releaseOnContextClose(CreationalContext<?> creationalContext, Bean<?> bean) {
  CommandContext commandContext = Context.getCommandContext();
  if(commandContext != null) {
    commandContext.registerCommandContextListener(new CreationalContextReleaseListener(creationalContext));

  } else {
    LOG.warning("Obtained instance of @Dependent scoped bean "+bean +" outside of process engine command context. "
        + "Bean instance will not be destroyed. This is likely to create a memory leak. Please use a normal scope like @ApplicationScoped for this bean.");

  }
}
 
Example 2
Source File: TaskEntity.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
protected void registerCommandContextCloseListener() {
  CommandContext commandContext = Context.getCommandContext();
  if (commandContext!=null) {
    commandContext.registerCommandContextListener(this);
  }
}
 
Example 3
Source File: ExecuteJobsCmd.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public Void execute(CommandContext commandContext) {
  ensureNotNull("jobId", jobId);

  final JobEntity job = commandContext.getDbEntityManager().selectById(JobEntity.class, jobId);

  final ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
  final IdentityService identityService = processEngineConfiguration.getIdentityService();

  final JobExecutorContext jobExecutorContext = Context.getJobExecutorContext();

  if (job == null) {
    if (jobExecutorContext != null) {
      // CAM-1842
      // Job was acquired but does not exist anymore. This is not a problem.
      // It usually means that the job has been deleted after it was acquired which can happen if the
      // the activity instance corresponding to the job is cancelled.
      LOG.debugAcquiredJobNotFound(jobId);
      return null;

    } else {
      throw LOG.jobNotFoundException(jobId);
    }
  }

  jobFailureCollector.setJob(job);

  if (jobExecutorContext == null) { // if null, then we are not called by the job executor
    for(CommandChecker checker : commandContext.getProcessEngineConfiguration().getCommandCheckers()) {
      checker.checkUpdateJob(job);
    }
    // write a user operation log since we're not called by the job executor
    commandContext.getOperationLogManager().logJobOperation(UserOperationLogEntry.OPERATION_TYPE_EXECUTE,
        jobId, job.getJobDefinitionId(), job.getProcessInstanceId(), job.getProcessDefinitionId(),
        job.getProcessDefinitionKey(), PropertyChange.EMPTY_CHANGE);
  } else {
    jobExecutorContext.setCurrentJob(job);

    // if the job is called by the job executor then set the tenant id of the job
    // as authenticated tenant to enable tenant checks
    String tenantId = job.getTenantId();
    if (tenantId != null) {
      identityService.setAuthentication(null, null, Collections.singletonList(tenantId));
    }
  }

  try {

    // register as command context close lister to intercept exceptions on flush
    commandContext.registerCommandContextListener(jobFailureCollector);

    commandContext.setCurrentJob(job);

    job.execute(commandContext);

  } catch (Throwable t) {
    jobFailureCollector.setFailedActivityId(Context.getCommandInvocationContext().getProcessDataContext().getLatestPropertyValue(ProcessDataContext.PROPERTY_ACTIVITY_ID));
    throw t;
  } finally {
    if (jobExecutorContext != null) {
      jobExecutorContext.setCurrentJob(null);
      identityService.clearAuthentication();
    }
  }

  return null;
}