以程式設計方式與工作流程互動

自訂並擴充您的工作流程時,您可以存取工作流程物件:

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

Workflow Console大量使用REST API;因此,本頁面說明了工作流程的REST API。

注意

使用curl命令行工具,可以使用Workflow REST API訪問工作流對象並管理實例生命週期。 本頁中的示例演示了如何通過捲曲命令行工具使用REST API。

REST API支援下列動作:

注意

使用Firefox擴充功能Firebug進行網頁開發,可在控制台運作時追蹤HTTP流量。 例如,您可以檢查參數和以POST請求傳送至AEM伺服器的值。

在本頁中,假設AEM在連接埠4502的localhost上執行,且安裝內容為" /"(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
在和參數兩種情況 model 下, modelfile 都需要另一個名為的 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 Widget不支援PUT作業。
DELETE 刪除模型。 為瞭解決防火牆/代理問題,POST包含X-HTTP-Method-Override標題項(其值為DELETE)也將被接受為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

若要取得所有執行中工作流程的清單,請執行下列動作:

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

如何取得所有執行中工作流程的清單及其ID —— 使用curl進行REST

使用捲曲的範例:

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

如何列出所有工作流模型

若要取得所有可用工作流程模型的清單,請執行下列動作:

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

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

使用捲曲的範例:

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-Hant"%><%
%><%@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下方的特定節點結構。 模型的父節點必須為cq:Page類型,該類型具有jcr:content節點,其屬性值如下:

    • 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-Hant"%><%
%><%@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}
注意

由於需要詳細程度,所以在建立和/或讀取模型時不認為捲曲是可行的。

檢查工作流狀態時過濾掉系統工作流

您可以使用WorkflowStatus API來檢索有關節點工作流狀態的資訊。

各種方法都有參數:

excludeSystemWorkflows

此參數可設為true,以指出應將系統工作流程排除在相關結果之外。

可以更新OSGi組態 Adobe Granite Workflow PayloadMapCache,此組態指定要視為系統工作流程的工作流程Models。 預設(執行時期)工作流程模型為:

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

逾時後的自動進階參與者步驟

如果您需要自動前進​參與者​步驟,但該步驟尚未在預先定義的時間內完成,您可以:

  1. 實作OSGI事件偵聽器,以監聽任務建立和修改。

  2. 指定逾時(截止日期),然後建立要在當時觸發的排程sling工作。

  3. 編寫逾時到期時會通知的工作處理常式,並觸發工作。

    如果任務尚未完成,此處理程式將對任務執行所需的操作

注意

必須明確界定要採取的行動才能採用這一辦法。

與工作流實例交互

以下提供如何與工作流程例項互動(程式化)的基本範例。

與工作流實例交互- 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"}
    ]
    
  • 暫停工作流

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

與工作項目互動——使用捲曲進行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