如何在AEM中获取Workflow事件类型和Processstep名称或Arguments到java服务?

时间:2016-08-23 10:37:40

标签: java aem

我创建了一个工作流,无论何时我从/content/dam/MyAsset文件夹上传或删除资产,我都可以触发JavaServices。我用Created Event Type和Removed Event Type创建了两个启动器。 我需要在服务中获取该事件的类型和流程步骤名称或参数 内部触发执行功能

这是我的代码:

public void execute(WorkItem arg0, WorkflowSession arg1, MetaDataMap arg2) 
                    throws WorkflowException 
{
    log.info("Workflow created ::::: ");    
}

有没有办法让Launcher事件类型和进程参数]服务?

2 个答案:

答案 0 :(得分:0)

您无法获取启动器事件类型,因为该信息未传递给工作流程。您可以做的是检查有效负载以确定节点是否存在。被删除的工作流程将包含已删除的节点的路径,如果您尝试解析该路径,它将为您提供异常(是的,糟糕的方法,但这是唯一的可能性)。

对于传递的流程步骤参数,所有这些信息都可在元数据中使用。查看arg0.getWorkflowData().getMetaDataMap().get("MyIDkey",<Type>);,其中arg0是您的WorkItem实例。

答案 1 :(得分:0)

为什么不使用org.osgi.service.event.EventHandler?您将获得所需的所有信息。这是一个等待事件的小代码片段,并将相关数据添加到地图中,然后传递给JobManager以生成由匹配JobConsumer执行的新作业:

import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobConsumer;
// and more imports...
@Service
@Component(immediate = true, policy = ConfigurationPolicy.OPTIONAL, description = "Listen to page modification events.")
@Properties(value = { @Property(name = "event.topics", value = { PageEvent.EVENT_TOPIC, DamEvent.EVENT_TOPIC}, propertyPrivate = true),
                      @Property(name = JobConsumer.PROPERTY_TOPICS, value = ModificationEventHandler.JOB_TOPICS, propertyPrivate = true) })
public class ModificationEventHandler implements EventHandler, JobConsumer {
    /**
     * Modification Job Topics.
     */
    public static final String JOB_TOPICS = "de/rockware/aem/modification";

    @Reference
    private JobManager jobManager;

    @Override 
    public void handleEvent(Event event) {
        logger.trace("Checking event.");
        PageEvent pageEvent = PageEvent.fromEvent(event);
        DamEvent damEvent = DamEvent.fromEvent(event);
        Map<String, Object> properties = new HashMap<>();
        if (damEvent != null) {
          // DamEvent is not serializable, so we cannot add the complete event to the map.
          logger.trace("Event on {} is a dam event ({}).", damEvent.getAssetPath(), damEvent.getType().name());
          properties.put(DAM_EVENT_ASSET_PATH, damEvent.getAssetPath());
        }
        if (pageEvent != null) {
          logger.trace("Event is a page event.");
          properties.put(PAGE_EVENT, pageEvent);
        }
        logger.trace("Adding new job.");
        jobManager.addJob(JOB_TOPICS, properties);
      }

      @Override 
      public JobResult process(Job job) {
          if (job.getProperty(PAGE_EVENT) != null) {
              PageEvent pageEvent = (PageEvent) job.getProperty(PAGE_EVENT);
              if(pageEvent.isLocal()) {
                  Iterator<PageModification> modificationsIterator;
                  modificationsIterator = pageEvent.getModifications();
                  while (modificationsIterator.hasNext()) {
                      PageModification modification = modificationsIterator.next();
                      logger.info("Handling modification {} on path {}.", modification.getType(), modification.getPath());
                      if(isRelevantModification(modification.getType())) {
                          logger.info(modification.getPath());
                      }
                  }
              } else {
                  logger.trace("Page event is not local.");
              }
          } else if (job.getProperty(DAM_EVENT_ASSET_PATH) != null) {
              String assetPath = (String) job.getProperty(DAM_EVENT_ASSET_PATH);
              logger.trace(assetPath);
              backupService.trackModification(assetPath);
          } else {
              logger.trace("Invalid event type. Cannot help.");
          }
          return JobResult.OK;
      }
}