protected MessageCorrelationResult tryCorrelateMessageToProcessDefinition(
      CommandContext commandContext, String messageName, CorrelationSet correlationSet) {

    MessageEventSubscriptionEntity messageEventSubscription =
        commandContext
            .getEventSubscriptionManager()
            .findMessageStartEventSubscriptionByName(messageName);
    if (messageEventSubscription == null || messageEventSubscription.getConfiguration() == null) {
      return null;

    } else {
      DeploymentCache deploymentCache =
          Context.getProcessEngineConfiguration().getDeploymentCache();

      String processDefinitionId = messageEventSubscription.getConfiguration();
      ProcessDefinitionEntity processDefinition =
          deploymentCache.findDeployedProcessDefinitionById(processDefinitionId);
      if (processDefinition == null) {
        LOGGER.log(
            Level.FINE,
            "Found event subscription with {0} but process definition {1} could not be found.",
            new Object[] {messageEventSubscription, processDefinitionId});
        return null;

      } else {
        return MessageCorrelationResult.matchedProcessDefinition(
            processDefinition, messageEventSubscription.getActivityId());
      }
    }
  }
  protected MessageCorrelationResult tryCorrelateMessageToExecution(
      CommandContext commandContext, String messageName, CorrelationSet correlationSet) {
    ExecutionQueryImpl query = new ExecutionQueryImpl();

    Map<String, Object> correlationKeys = correlationSet.getCorrelationKeys();
    if (correlationKeys != null) {
      for (Map.Entry<String, Object> correlationKey : correlationKeys.entrySet()) {
        query.processVariableValueEquals(correlationKey.getKey(), correlationKey.getValue());
      }
    }

    String businessKey = correlationSet.getBusinessKey();
    if (businessKey != null) {
      query.processInstanceBusinessKey(businessKey);
    }

    query.messageEventSubscriptionName(messageName);
    List<Execution> matchingExecutions = query.executeList(commandContext, null);

    if (matchingExecutions.size() > 1) {
      throw new MismatchingMessageCorrelationException(
          messageName,
          businessKey,
          correlationKeys,
          String.valueOf(matchingExecutions.size())
              + " executions match the correlation keys. Should be one or zero.");

    } else if (!matchingExecutions.isEmpty()) {
      Execution matchingExecution = matchingExecutions.get(0);
      return MessageCorrelationResult.matchedExecution((ExecutionEntity) matchingExecution);

    } else {
      return null;
    }
  }