以编程方式与工作流交互 interacting-with-workflows-programmatically

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

使用工作流Java API using-the-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脚本中获取工作流对象 obtaining-workflow-objects-in-ecma-scripts

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

ScriptHelper类提供对可用于最终获取WorkflowSession对象的SlingHttpServletRequest的访问权限;例如:

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

使用工作流REST API using-the-workflow-rest-api

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

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

REST API支持以下操作:

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

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

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

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

管理工作流实例 managing-workflow-instances

以下HTTP请求方法适用于:

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

HTTP请求方法
操作
GET
列出可用的工作流实例。
POST
创建新的工作流实例。 参数为:
- model:相应工作流模型的ID (URI)
- payloadType:包含有效负载的类型(例如,JCR_PATH或URL)。
有效负载作为参数payload发送。 使用包含新工作流实例资源的URL的位置标头发送回201 (CREATED)响应。

按状态管理工作流实例 managing-a-workflow-instance-by-its-state

以下HTTP请求方法适用于:

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

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

按其ID管理工作流实例 managing-a-workflow-instance-by-its-id

以下HTTP请求方法适用于:

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

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

管理工作流模型 managing-workflow-models

以下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"}
]

管理特定工作流模型 managing-a-specific-workflow-model

以下HTTP请求方法适用于:

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

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

HTTP请求方法
操作
GET
获取模型的HEAD版本(定义和元数据)。
PUT
更新模型的HEAD版本(创建新版本)。
必须将新版本模型的完整模型定义作为名为model的参数添加。 此外,创建新模型时需要type参数,且需要值为JSON.
POST
与PUT的行为相同。 需要,因为AEM小组件不支持PUT操作。
DELETE
删除模型。 要解决防火墙/代理问题,还将接受包含值为DELETEX-HTTP-Method-Override标头条目的POST作为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":
    {
    }
  }
]}

按版本管理工作流模型 managing-a-workflow-model-by-its-version

以下HTTP请求方法适用于:

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

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

管理(用户)收件箱 managing-user-inboxes

以下HTTP请求方法适用于:

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

HTTP请求方法
操作
GET
列出用户收件箱中的工作项目,由HTTP身份验证标头标识。
POST
完成其URI作为参数item发送的工作项并将相应的工作流实例前进到下一个节点,如果返回一个步骤,则由参数routebackroute定义。
如果发送了参数delegatee,则由参数item标识的工作项将委派给指定的参与者。

按工作项目ID管理(用户)收件箱 managing-a-user-inbox-by-the-workitem-id

以下HTTP请求方法适用于:

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

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

示例 examples

如何获取所有正在运行的工作流的列表及其ID how-to-get-a-list-of-all-running-workflows-with-their-ids

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

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

如何获取所有正在运行的工作流及其ID的列表 — 使用CURL的REST how-to-get-a-list-of-all-running-workflows-with-their-ids-rest-using-curl

使用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"}
]
NOTE
curl命令可与任何工作流状态一起使用,以取代RUNNING

如何更改工作流标题 how-to-change-the-workflow-title

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

  • 收件人:http://localhost:4502/etc/workflow/instances/{id}

  • ,并使用以下参数:

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

如何使用curl更改工作流标题 — REST how-to-change-the-workflow-title-rest-using-curl

使用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

如何列出所有工作流模型 how-to-list-all-workflow-models

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

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

如何列出所有工作流模型 — 使用CURL的REST how-to-list-all-workflow-models-rest-using-curl

使用curl的示例:

curl -u admin:admin http://localhost:4502/etc/workflow/models.json
NOTE
另请参阅管理工作流模型

获取WorkflowSession对象 obtaining-a-workflowsession-object

com.adobe.granite.workflow.WorkflowSession类可从javax.jcr.Session对象或org.apache.sling.api.resource.ResourceResolver对象改写。

获取WorkflowSession对象 — Java obtaining-a-workflowsession-object-java

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

<%
%><%@include file="/libs/foundation/global.jsp"%><%
%><%@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脚本 obtaining-a-workflowsession-object-ecma-script

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

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

创建、读取或删除工作流模型 creating-reading-or-deleting-workflow-models

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

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

使用的示例:

  1. 创建模型(ID为/var/workflow/models/mymodel/jcr:content/model)。
  2. 删除模型。
NOTE
删除模型会将模型的metaData子节点的deleted属性设置为true
删除不会删除模型节点。

创建模型时:

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

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

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

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

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

    note note
    NOTE
    请参阅如何列出所有工作流模型

创建、读取或删除工作流模型 — Java creating-reading-or-deleting-workflow-models-java

<%@include file="/libs/foundation/global.jsp"%><%
%><%@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脚本 creating-reading-or-deleting-workflow-models-ecma-script

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);

删除工作流模型 — REST使用curl deleting-a-workflow-model-rest-using-curl

# deleting the model by its id
curl -u admin:admin -X DELETE http://localhost:4502/etc/workflow/models/{id}
NOTE
由于所需的细节级别,Curl被认为对于创建和/或读取模型不实用。

检查工作流状态时筛选出系统工作流 filtering-out-system-workflows-when-checking-workflow-status

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

各种方法都具有参数:

excludeSystemWorkflows

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

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

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

超时后自动前进参与者步骤 auto-advance-participant-step-after-a-timeout

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

  1. 实施OSGI事件侦听器以侦听任务的创建和修改。

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

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

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

NOTE
必须明确界定要采取的行动,以便能够采用这种方法。

与工作流实例交互 interacting-with-workflow-instances

下面提供了如何与工作流实例交互(以编程方式)的基本示例。

与工作流实例交互 — Java interacting-with-workflow-instances-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脚本 interacting-with-workflow-instances-ecma-script

// 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 interacting-with-workflow-instances-rest-using-curl

  • 启动工作流

    code language-shell
    # 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
    
  • 正在列出实例

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

    这将列出所有实例;例如:

    code language-shell
    [
        {"uri":"/var/workflow/instances/server0/2018-02-26/prototype-01_1"}
        ,{"uri":"/var/workflow/instances/server0/2018-02-26/prototype-01_2"}
    ]
    
    note note
    NOTE
    请参阅如何获取所有正在运行的工作流的列表及其ID,以列出具有特定状态的实例。
  • 暂停工作流

    code language-shell
    # 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
    
  • 恢复工作流

    code language-shell
    # 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
    
  • 正在终止工作流实例

    code language-shell
    # 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
    

与工作项交互 interacting-with-work-items

下面提供了如何与工作项交互(以编程方式)的基本示例。

与工作项交互 — Java interacting-with-work-items-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脚本 interacting-with-work-items-ecma-script

// 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 interacting-with-work-items-rest-using-curl

  • 从收件箱列出工作项

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

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

    code language-shell
    [{
        "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"
      }
    
  • 委托工作项

    code language-xml
    # 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
    
    note note
    NOTE
    delegatee必须为工作流步骤的有效选项。
  • 完成工作项或将工作项提升到下一步

    code language-xml
    # 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
    

侦听工作流事件 listening-for-workflow-events

使用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;
 }
}
recommendation-more-help
19ffd973-7af2-44d0-84b5-d547b0dffee2