以编程方式与工作流交互

自定义和扩展工作流时,您可以访问工作流对象:

使用Workflow Java API

工作流Java API由com.adobe.granite.workflow包和多个子包组成。 API中最重要的成员是com.adobe.granite.workflow.WorkflowSession类。 WorkflowSession类提供对设计时和运行时工作流对象的访问:

  • 工作流模型
  • 工作项
  • 工作流实例
  • 工作流数据
  • 收件箱项目

该类还提供了几种用于干预工作流生命周期的方法。

下表提供了与工作流进行有序交互时要使用的几个关键Java对象的参考文档的链接。 下面的示例演示如何获取和使用代码中的类对象。

功能 对象
访问工作流 WorkflowSession
执行和查询工作流实例 Workflow
WorkItem
WorkflowData
管理工作流模型 WorkflowModel
WorkflowNode
WorkflowTransition
工作流中(或不是)节点的信息 WorkflowStatus

在ECMA脚本中获取工作流对象

查找脚本中所述,AEM(通过Apache Sling)提供了执行服务器端ECMA脚本的ECMA脚本引擎。 org.apache.sling.scripting.core.ScriptHelper类作为sling变量可立即用于脚本。

ScriptHelper类提供对SlingHttpServletRequest的访问,您可以使用它最终获取WorkflowSession对象;例如:

var wfsession = sling.getRequest().getResource().getResourceResolver().adaptTo(Packages.com.adobe.granite.workflow.WorkflowSession);

使用Workflow REST API

“工作流”控制台大量使用REST API;因此,本页介绍了工作流的REST API。

注意

curl命令行工具允许您使用Workflow REST API访问工作流对象和管理实例生命周期。 本页中的示例演示了如何通过curl命令行工具使用REST API。

REST API支持以下操作:

注意

通过使用Firebug(用于Web开发的Firefox扩展),可以在控制台操作时跟踪HTTP流量。 例如,您可以检查发送到AEM服务器的参数和值(请求POST)。

在此页中,假定AEM在本地主机上的端口4502运行,并且安装上下文为“ /”(root)。 如果安装时不适用,则需要相应地调整HTTP请求所适用的URI。

GET请求支持的渲染是JSON渲染。 GET的URL应具有.json扩展名,例如:

http://localhost:4502/etc/workflow.json

管理工作流实例

以下HTTP请求方法适用于:

http://localhost:4502/etc/workflow/instances

HTTP请求方法 操作
GET 列表可用的工作流实例。
POST

创建新的工作流实例。 参数为:
- model:相应工作流模型
的ID(URI)- payloadType:包含有效负荷的类型(例如JCR_PATH或URL)。
有效负荷作为参数发送 payload。将回发201(CREATED)响应,其中包含新工作流实例资源的URL的位置标头。

按状态管理工作流实例

以下HTTP请求方法适用于:

http://localhost:4502/etc/workflow/instances.{state}

HTTP请求方法 操作
GET 列表可用工作流实例及其状态(RUNNINGSUSPENDEDABORTEDCOMPLETED

按ID管理工作流实例

以下HTTP请求方法适用于:

http://localhost:4502/etc/workflow/instances/{id}

HTTP请求方法 操作
GET 获取实例数据(定义和元数据),包括指向相应工作流模型的链接。
POST 更改实例的状态。 新状态将作为参数state发送,且必须具有以下值之一:RUNNINGSUSPENDEDABORTED
如果新状态不可访问(例如,当挂起终止的实例时), 409 则(CONFLICT)响应将发回到客户端。

管理工作流模型

以下HTTP请求方法适用于:

http://localhost:4502/etc/workflow/models

HTTP请求方法 操作
GET 列表可用的工作流模型。
POST 创建新工作流模型. 如果发送参数title,则使用指定的标题创建新模型。 将JSON模型定义作为参数model附加后,将根据提供的定义创建新的工作流模型。
201 应(CREATED)会与包含新工作流模型资源的URL的位置标题一起发送。
当模型定义作为名为的文件参数附加时,也会发生同样的情况 modelfile
在参数和参数的情 modelmodelfile 下,定义序列化格 type 式时都需要另外一个名为的参数。新的序列化格式可以使用OSGI API进行集成。 标准JSON序列化器随工作流引擎一起提供。 其类型为JSON。 有关格式的示例,请参见下文。

示例:在浏览器中,对http://localhost:4502/etc/workflow/models.json的请求将生成与以下内容类似的json响应:

[
    {"uri":"/var/workflow/models/activationmodel"}
    ,{"uri":"/var/workflow/models/dam/adddamsize"}
    ,{"uri":"/var/workflow/models/cloudconfigs/dtm-reactor/library-download"}
    ,{"uri":"/var/workflow/models/ac-newsletter-workflow-simple"}
    ,{"uri":"/var/workflow/models/dam/dam-create-language-copy"}
    ,{"uri":"/var/workflow/models/dam/dam-create-and-translate-language-copy"}
    ,{"uri":"/var/workflow/models/dam-indesign-proxy"}
    ,{"uri":"/var/workflow/models/dam-xmp-writeback"}
    ,{"uri":"/var/workflow/models/dam-parse-word-documents"}
    ,{"uri":"/var/workflow/models/dam/process_subasset"}
    ,{"uri":"/var/workflow/models/dam/dam_set_last_modified"}
    ,{"uri":"/var/workflow/models/dam/dam-autotag-assets"}
    ,{"uri":"/var/workflow/models/dam/update_asset"}
    ,{"uri":"/var/workflow/models/dam/update_asset_offloading"}
    ,{"uri":"/var/workflow/models/dam/dam-update-language-copy"}
    ,{"uri":"/var/workflow/models/dam/update_from_lightbox"}
    ,{"uri":"/var/workflow/models/cloudservices/DTM_bundle_download"}
    ,{"uri":"/var/workflow/models/dam/dam_download_asset"}
    ,{"uri":"/var/workflow/models/dam/dynamic-media-encode-video"}
    ,{"uri":"/var/workflow/models/dam/dynamic-media-video-thumbnail-replacement"}
    ,{"uri":"/var/workflow/models/dam/dynamic-media-video-user-uploaded-thumbnail"}
    ,{"uri":"/var/workflow/models/newsletter_bounce_check"}
    ,{"uri":"/var/workflow/models/projects/photo_shoot_submission"}
    ,{"uri":"/var/workflow/models/projects/product_photo_shoot"}
    ,{"uri":"/var/workflow/models/projects/approval_workflow"}
    ,{"uri":"/var/workflow/models/prototype-01"}
    ,{"uri":"/var/workflow/models/publish_example"}
    ,{"uri":"/var/workflow/models/publish_to_campaign"}
    ,{"uri":"/var/workflow/models/screens/publish_to_author_bin"}
    ,{"uri":"/var/workflow/models/s7dam/request_to_publish_to_youtube"}
    ,{"uri":"/var/workflow/models/projects/request_copy"}
    ,{"uri":"/var/workflow/models/projects/request_email"}
    ,{"uri":"/var/workflow/models/projects/request_landing_page"}
    ,{"uri":"/var/workflow/models/projects/request_launch"}
    ,{"uri":"/var/workflow/models/request_for_activation"}
    ,{"uri":"/var/workflow/models/request_for_deactivation"}
    ,{"uri":"/var/workflow/models/request_for_deletion"}
    ,{"uri":"/var/workflow/models/request_for_deletion_without_deactivation"}
    ,{"uri":"/var/workflow/models/request_to_complete_move_operation"}
    ,{"uri":"/var/workflow/models/reverse_replication"}
    ,{"uri":"/var/workflow/models/salesforce-com-export"}
    ,{"uri":"/var/workflow/models/scene7"}
    ,{"uri":"/var/workflow/models/scheduled_activation"}
    ,{"uri":"/var/workflow/models/scheduled_deactivation"}
    ,{"uri":"/var/workflow/models/screens/screens-update-asset"}
    ,{"uri":"/var/workflow/models/translation"}
    ,{"uri":"/var/workflow/models/s7dam/request_to_remove_from_youtube"}
    ,{"uri":"/var/workflow/models/wcm-translation/create_language_copy"}
    ,{"uri":"/var/workflow/models/wcm-translation/prepare_translation_project"}
    ,{"uri":"/var/workflow/models/wcm-translation/translate-i18n-dictionary"}
    ,{"uri":"/var/workflow/models/wcm-translation/sync_translation_job"}
    ,{"uri":"/var/workflow/models/wcm-translation/translate-language-copy"}
    ,{"uri":"/var/workflow/models/wcm-translation/update_language_copy"}
]

管理特定工作流模型

以下HTTP请求方法适用于:

http://localhost:4502*{uri}*

其中*{uri}*是存储库中模型节点的路径。

HTTP请求方法 操作
GET 获取模型的HEAD版本(定义和元数据)。
PUT 更新HEAD版本的模型(创建新版本)。
必须将新版本的模型的完整模型定义添加为名为的参数 model。此外,创建新模型时需要type参数,并且需要具有值JSON
POST 行为与PUT相同。 需要,因为AEM构件不支持PUT操作。
DELETE 删除模型。 为了解决防火墙/代理问题,POST中包含值DELETEX-HTTP-Method-Override头条目的也将作为DELETE请求接受。

示例:在浏览器中,对http://localhost:4502/var/workflow/models/publish_example.json的请求返回与以下代码类似的json响应:

{
  "id":"/var/workflow/models/publish_example",
  "title":"Publish Example",
  "version":"1.0",
  "description":"This example shows a simple review and publish process.",
  "metaData": 
  {
    "multiResourceSupport":"true",
    "tags":"wcm,publish"
  },
  "nodes":
  [{
    "id":"node0",
    "type":"START",
    "title":"Start",
    "description":"The start node of the workflow.",
    "metaData":
    {
    }
  },
  {
    "id":"node1",
    "type":"PARTICIPANT",
    "title":"Validate Content",
    "description":"Validate the modified content.",
    "metaData":
    {
      "PARTICIPANT":"admin"
    }
  },
  {
    "id":"node2",
    "type":"PROCESS",
    "title":"Publish Content",
    "description":"Publish the modified content.",
    "metaData":
    {
      "PROCESS_AUTO_ADVANCE":"true",
      "PROCESS":"com.day.cq.wcm.workflow.process.ActivatePageProcess"
    }
  },
  {
    "id":"node3",
    "type":"END",
    "title":"End",
    "description":"The end node of the workflow.",
    "metaData":
    {
    }
  }],
  "transitions":
  [{
    "from":"node0",
    "to":"node1",
    "metaData":
    {
    }
  },
  {
    "from":"node1",
    "to":"node2",
    "metaData":
    {
    }
  },
  {
    "from":"node2",
    "to":"node3",
    "metaData":
    {
    }
  }
]}

按版本管理工作流模型

以下HTTP请求方法适用于:

http://localhost:4502/etc/workflow/models/{id}.{version}

HTTP请求方法 操作
GET 获取给定版本中的模型数据(如果存在)。

管理(用户)收件箱

以下HTTP请求方法适用于:

http://localhost:4502/bin/workflow/inbox

HTTP请求方法 操作
GET 列表用户收件箱中的工作项,HTTP身份验证头会识别该用户。
POST 完成其URI作为参数item发送的工作项,并将根据工作流实例前进到下一个节点,该节点由参数routebackroute定义,以便返回。
如果发送参 delegatee 数,则由参数标识的工作项将 item 委派给指定的参加者。

使用WorkItem ID管理(用户)收件箱

以下HTTP请求方法适用于:

http://localhost:4502/bin/workflow/inbox/{id}

HTTP请求方法 操作
GET 获取由收件箱ID标识的收件箱WorkItem的数据(定义和元数据)。

示例

如何获取所有正在运行的工作流的列表,其ID

要获取所有正在运行的工作流的列表,请执行GET以:

http://localhost:4502/etc/workflow/instances.RUNNING.json

如何获取所有正在运行的工作流的列表及其ID —— 使用curl的REST

使用curl的示例:

curl -u admin:admin http://localhost:4502/etc/workflow/instances.RUNNING.json

结果中显示的uri可以作为其他命令中的实例id;例如:

[
    {"uri":"/etc/workflow/instances/server0/2017-03-08/request_for_activation_1"}
]
注意

curl命令可以与任何工作流状态一起使用,而不是RUNNING

如何更改工作流标题

要更改在工作流控制台的​实例​选项卡中显示的​工作流标题,请发送POST命令:

  • 到: http://localhost:4502/etc/workflow/instances/{id}

  • 使用以下参数:

    • action:其价值必须是: UPDATE
    • workflowTitle:工作流标题

如何使用curl更改工作流标题- REST

使用curl的示例:

curl -u admin:admin -d "action=UPDATE&workflowTitle=myWorkflowTitle" http://localhost:4502/etc/workflow/instances/{id}

# for example
curl -u admin:admin -d "action=UPDATE&workflowTitle=myWorkflowTitle" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1

如何列表所有工作流模型

要获取所有可用工作流模型的列表,请执行GET以:

http://localhost:4502/etc/workflow/models.json

如何列表所有工作流模型——使用curl的REST

使用curl的示例:

curl -u admin:admin http://localhost:4502/etc/workflow/models.json
注意

另请参阅管理工作流模型

获取WorkflowSession对象

com.adobe.granite.workflow.WorkflowSession类可从javax.jcr.Session对象或org.apache.sling.api.resource.ResourceResolver对象进行调整。

获取WorkflowSession对象- Java

在JSP脚本(或Servlet类的Java代码)中,使用HTTP请求对象获取SlingHttpServletRequest对象,该对象提供对ResourceResolver对象的访问。 将ResourceResolver对象调整为WorkflowSession

<%
%><%@include file="/libs/foundation/global.jsp?lang=zh-Hans"%><%
%><%@page session="false" 
    import="com.adobe.granite.workflow.WorkflowSession,
  org.apache.sling.api.SlingHttpServletRequest"%><%

SlingHttpServletRequest slingReq = (SlingHttpServletRequest)request;
WorkflowSession wfSession = slingReq.getResourceResolver().adaptTo(WorkflowSession.class);
%>

获取WorkflowSession对象- ECMA脚本

使用sling变量获取用于获取ResourceResolver对象的SlingHttpServletRequest对象。 将ResourceResolver对象调整为WorkflowSession对象。

var wfsession = sling.getRequest().getResource().getResourceResolver().adaptTo(Packages.com.adobe.granite.workflow.WorkflowSession);

创建、读取或删除工作流模型

以下示例显示如何访问工作流模型:

  • Java和ECMA脚本的代码使用WorkflowSession.createNewModel方法。
  • curl命令直接使用其URL访问模型。

使用的示例:

  1. 创建模型(ID为/var/workflow/models/mymodel/jcr:content/model)。
  2. 删除模型。
注意

删除模型会将模型的metaData子节点的deleted属性设置为true

删除不会删除模型节点。

创建新模型时:

  • 工作流模型编辑器要求模型使用/var/workflow/models下的特定节点结构。 模型的父节点必须为具有以下属性值的jcr:content节点的类型cq:Page:

    • sling:resourceType: cq/workflow/components/pages/model
    • cq:template: /libs/cq/workflow/templates/model

    创建模型时,必须先创建此cq:Page节点,并使用其jcr:content节点作为模型节点的父节点。

  • 某些方法识别模型所需的id参数是存储库中模型节点的绝对路径:

    /var/workflow/models/<*model_name>*/jcr:content/model

创建、读取或删除工作流模型- Java

<%@include file="/libs/foundation/global.jsp?lang=zh-Hans"%><%
%><%@page session="false" import="com.adobe.granite.workflow.WorkflowSession,
                 com.adobe.granite.workflow.model.WorkflowModel,
             org.apache.sling.api.SlingHttpServletRequest"%><%

SlingHttpServletRequest slingReq = (SlingHttpServletRequest)request;
WorkflowSession wfSession = slingReq.getResourceResolver().adaptTo(WorkflowSession.class);
/* Create the parent page */
String modelRepo = new String("/var/workflow/models");
String modelTemplate = new String ("/libs/cq/workflow/templates/model");
String modelName = new String("mymodel");
Page modelParent = pageManager.create(modelRepo, modelName, modelTemplate, "My workflow model");

/* create the model */
String modelId = new String(modelParent.getPath()+"/jcr:content/model")
WorkflowModel model = wfSession.createNewModel("Made using Java",modelId);

/* delete the model */
wfSession.deleteModel(modelId);
%>

创建、读取或删除工作流模型- ECMA脚本

var resolver = sling.getRequest().getResource().getResourceResolver();
var wfSession = resolver.adaptTo(Packages.com.adobe.granite.workflow.WorkflowSession);
var pageManager = resolver.adaptTo(Packages.com.day.cq.wcm.api.PageManager);

//create the parent page node
var workflowPage = pageManager.create("/var/workflow/models", "mymodel", "/libs/cq/workflow/templates/model", "Created via ECMA Script");
var modelId = workflowPage.getPath()+ "/jcr:content/model";
//create the model
var model = wfSession.createNewModel("My Model", modelId);
//delete the model
var model = wfSession.deleteModel(modelId);

删除工作流模型——使用curl进行REST

# deleting the model by its id
curl -u admin:admin -X DELETE http://localhost:4502/etc/workflow/models/{id}
注意

由于所需的详细程度,在创建和/或读取模型时,curl不被认为是实用的。

检查工作流状态时过滤掉系统工作流

您可以使用WorkflowStatus API检索有关节点工作流状态的信息。

各种方法都具有参数:

excludeSystemWorkflows

此参数可设置为true以指示应从相关结果中排除系统工作流。

可以更新OSGi配置 AdobeGranite工作流PayloadMapCache,该配置指定要视为系统工作流的工作流Models。 默认(运行时)工作流模型为:

  • /var/workflow/models/scheduled_activation/jcr:content/model
  • /var/workflow/models/scheduled_deactivation/jcr:content/model

超时后的自动前进参与者步骤

如果您需要自动前进在预定义时间内尚未完成的​参与者​步骤,您可以:

  1. 实现OSGI事件监听器以监听任务创建和修改。

  2. 指定超时(截止日期),然后创建计划的sling作业以在该时间触发。

  3. 编写一个作业处理程序,该处理程序在超时过期时通知并触发作业。

    如果任务尚未完成,此处理程序将对任务执行所需的操作

注意

必须明确界定要采取的行动才能采用这种方法。

与工作流实例交互

下面提供了如何与工作流实例进行交互(progamaly)的基本示例。

与工作流实例交互- Java

// starting a workflow
WorkflowModel model = wfSession.getModel(workflowId);
WorkflowData wfData = wfSession.newWorkflowData("JCR_PATH", repoPath);
wfSession.startWorkflow(model, wfData);

// querying and managing a workflow
Workflow[] workflows workflows = wfSession.getAllWorkflows();
Workflow workflow= wfSession.getWorkflow(id);
wfSession.suspendWorkflow(workflow);
wfSession.resumeWorkflow(workflow);
wfSession.terminateWorkflow(workflow);

与工作流实例交互- ECMA脚本

// starting a workflow
var model = wfSession.getModel(workflowId);
var wfData = wfSession.newWorkflowData("JCR_PATH", repoPath);
wfSession.startWorkflow(model, wfData);

// querying and managing a workflow
var workflows = wfSession.getWorkflows(“RUNNING“);
var workflow= wfSession.getWorkflow(id);
wfSession.suspendWorkflow(workflow);
wfSession.resumeWorkflow(workflow);
wfSession.terminateWorkflow(workflow);

与工作流实例交互——使用curl的REST

  • 启动工作流

    # starting a workflow
    curl -d "model={id}&payloadType={type}&payload={payload}" http://localhost:4502/etc/workflow/instances
    
    # for example:
    curl -u admin:admin -d "model=/var/workflow/models/request_for_activation&payloadType=JCR_PATH&payload=/content/we-retail/us/en/products" http://localhost:4502/etc/workflow/instances
    
  • 列出实例

    # listing the instances
    curl -u admin:admin http://localhost:4502/etc/workflow/instances.json
    

    这将列表所有情况;例如:

    [
        {"uri":"/var/workflow/instances/server0/2018-02-26/prototype-01_1"}
        ,{"uri":"/var/workflow/instances/server0/2018-02-26/prototype-01_2"}
    ]
    
    注意

    请参阅如何获取所有正在运行的列表及其ID,以列出具有特定状态的实例。

  • 暂停工作流

    # suspending a workflow
    curl -d "state=SUSPENDED" http://localhost:4502/etc/workflow/instances/{id}
    
    # for example:
    curl -u admin:admin -d "state=SUSPENDED" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1
    
  • 恢复工作流

    # resuming a workflow
    curl -d "state=RUNNING" http://localhost:4502/etc/workflow/instances/{id}
    
    # for example:
    curl -u admin:admin -d "state=RUNNING" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1
    
  • 终止工作流实例

    # terminating a workflow
    curl -d "state=ABORTED" http://localhost:4502/etc/workflow/instances/{id}
    
    # for example:
    curl -u admin:admin -d "state=ABORTED" http://localhost:4502/etc/workflow/instances/server0/2017-03-08/request_for_activation_1
    

与工作项交互

下面提供了如何与工作项进行交互(按程序)的基本示例。

与工作项交互- Java

// querying work items
WorkItem[] workItems = wfSession.getActiveWorkItems();
WorkItem workItem = wfSession.getWorkItem(id);

// getting routes
List<Route> routes = wfSession.getRoutes(workItem);

// delegating
Iterator<Participant> delegatees = wfSession.getDelegatees(workItem);
wfSession.delegateWorkItem(workItem, delegatees.get(0));

// completing or advancing to the next step
wfSession.complete(workItem, routes.get(0));

与工作项交互- ECMA脚本

// querying work items
var workItems = wfSession.getActiveWorkItems();
var workItem = wfSession.getWorkItem(id);

// getting routes
var routes = wfSession.getRoutes(workItem);

// delegating
var delegatees = wfSession.getDelegatees(workItem);
wfSession.delegateWorkItem(workItem, delegatees.get(0));

// completing or advancing to the next step
wfSession.complete(workItem, routes.get(0));

与工作项交互——使用curl的REST

  • 从收件箱中列出工作项

    # listing the work items
    curl -u admin:admin http://localhost:4502/bin/workflow/inbox
    

    将列出收件箱中当前工作项的详细信息;例如:

    [{
        "uri_xss": "/var/workflow/instances/server0/2018-02-26/prototype-01_2/workItems/node2_var_workflow_instances_server0_2018-02-26_prototype-01_2",
        "uri": "/var/workflow/instances/server0/2018-02-26/prototype-01_2/workItems/node2_var_workflow_instances_server0_2018-02-26_prototype-01_2",
        "currentAssignee_xss": "workflow-administrators",
        "currentAssignee": "workflow-administrators",
        "startTime": 1519656289274,
        "payloadType_xss": "JCR_PATH",
        "payloadType": "JCR_PATH",
        "payload_xss": "/content/we-retail/es/es",
        "payload": "/content/we-retail/es/es",
        "comment_xss": "Process resource is null",
        "comment": "Process resource is null",
        "type_xss": "WorkItem",
        "type": "WorkItem"
      },{
        "uri_xss": "configuration/configure_analyticstargeting",
        "uri": "configuration/configure_analyticstargeting",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      },{
        "uri_xss": "configuration/securitychecklist",
        "uri": "configuration/securitychecklist",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      },{
        "uri_xss": "configuration/enable_collectionofanonymoususagedata",
        "uri": "configuration/enable_collectionofanonymoususagedata",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      },{
        "uri_xss": "configuration/configuressl",
        "uri": "configuration/configuressl",
        "currentAssignee_xss": "administrators",
        "currentAssignee": "administrators",
        "type_xss": "Task",
        "type": "Task"
      }
    
  • 委派工作项

    # delegating
    curl -d "item={item}&delegatee={delegatee}" http://localhost:4502/bin/workflow/inbox
    
    # for example: 
    curl -u admin:admin -d "item=/etc/workflow/instances/server0/2017-03-08/request_for_activation_1/workItems/node1_etc_workflow_instances_server0_2017-03-08_request_for_act_1&delegatee=administrators" http://localhost:4502/bin/workflow/inbox
    
    注意

    delegatee必须是工作流步骤的有效选项。

  • 完成或将工作项推向下一步

    # retrieve the list of routes; the results will be similar to {"results":1,"routes":[{"rid":"233123169","label":"End","label_xss":"End"}]}
    http://localhost:4502/etc/workflow/instances/<path-to-the-workitem>.routes.json
    
    # completing or advancing to the next step; use the appropriate route ID (rid value) from the above list
    curl -d "item={item}&route={route}" http://localhost:4502/bin/workflow/inbox
    
    # for example:
    curl -u admin:admin -d "item=/etc/workflow/instances/server0/2017-03-08/request_for_activation_1/workItems/node1_etc_workflow_instances_server0_2017-03-08_request_for_activation_1&route=233123169" http://localhost:4502/bin/workflow/inbox
    

侦听工作流事件

使用OSGi事件框架监听 com.adobe.granite.workflow.event.WorkflowEvent类定义的事件。 此类还提供几种有用的方法,用于获取有关事件主题的信息。 例如,getWorkItem方法返回与事件相关的工作项的WorkItem对象。

以下示例代码定义一个服务,它监听工作流事件并根据事件类型执行任务。

package com.adobe.example.workflow.listeners;

import org.apache.sling.event.jobs.JobProcessor;
import org.apache.sling.event.jobs.JobUtil;

import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;

import com.adobe.granite.workflow.event.WorkflowEvent;
import com.adobe.granite.workflow.exec.WorkItem;

/**
 * The <code>WorkflowEventCatcher</code> class listens to workflow events. 
 */
@Component(metatype=false, immediate=true)
@Service(value=org.osgi.service.event.EventHandler.class)
public class WorkflowEventCatcher implements EventHandler, JobProcessor {

 @Property(value=com.adobe.granite.workflow.event.WorkflowEvent.EVENT_TOPIC)
 static final String EVENT_TOPICS = "event.topics";

 private static final Logger logger = LoggerFactory.getLogger(WorkflowEventCatcher.class);

 public void handleEvent(Event event) {
  JobUtil.processJob(event, this);
 }

 public boolean process(Event event) {
  logger.info("Received event of topic: " + event.getTopic());
  String topic = event.getTopic();

  try {
   if (topic.equals(WorkflowEvent.EVENT_TOPIC)) {
    WorkflowEvent wfevent = (WorkflowEvent)event;
    String eventType = wfevent.getEventType();
    String instanceId = wfevent.getWorkflowInstanceId();

    if (instanceId != null) {
     //workflow instance events
     if (eventType.equals(WorkflowEvent.WORKFLOW_STARTED_EVENT) ||
       eventType.equals(WorkflowEvent.WORKFLOW_RESUMED_EVENT) ||
       eventType.equals(WorkflowEvent.WORKFLOW_SUSPENDED_EVENT)) {
      // your code comes here...
     } else if (
       eventType.equals(WorkflowEvent.WORKFLOW_ABORTED_EVENT) ||
       eventType.equals(WorkflowEvent.WORKFLOW_COMPLETED_EVENT)) {
      // your code comes here...
     }
     // workflow node event
     if (eventType.equals(WorkflowEvent.NODE_TRANSITION_EVENT)) {
      WorkItem currentItem = (WorkItem) event.getProperty(WorkflowEvent.WORK_ITEM);
      // your code comes here...
     }
    }
   }
  } catch(Exception e){
   logger.debug(e.getMessage());
   e.printStackTrace();
  }
  return true;
 }
}

在此页面上

Adobe Summit Banner

A virtual event April 27-28.

Expand your skills and get inspired.

Register for free
Adobe Summit Banner

A virtual event April 27-28.

Expand your skills and get inspired.

Register for free
Adobe Maker Awards Banner

Time to shine!

Apply now for the 2021 Adobe Experience Maker Awards.

Apply now
Adobe Maker Awards Banner

Time to shine!

Apply now for the 2021 Adobe Experience Maker Awards.

Apply now