![](/img/trans.png)
[英]How to implement transaction subprocess using Camunda's fluent builder API?
[英]Camunda BMPN Synchronous Response using Camunda REST API
是否可以使用 Camunda REST API 從 Camunda BPMN 流獲得同步響應?
例如我有 2 個服務任務,
1. Validate Address
,
2. Get ProductList
。
現在我正在使用Camunda 提供的 REST API 啟動進程(不使用 Cockpit 啟動進程)。 地址驗證服務任務被執行, ProductList 任務被成功執行,它的響應, ie List of products
,將在調用 Client 的響應中返回,而不是用進程 id 挖掘 camunda。 我搜索了它,但沒有找到任何這樣的例子或導致它。
我已經閱讀了 REST 文檔,沒有其他方法可以從 REST API 獲取實際響應(即List of Product
)作為其余調用的返回類型,因為返回類型采用以下格式 -
{
id: "95aef406-3a7a-11e5-85b6-dafa20524153",
definitionId: "invoice:1:15e97a1c-312d-11e5-aca3-a0e120524153",
businessKey: null,
caseInstanceId: null,
ended: true,
suspended: false
}
它顯示進程已結束( ended: true
),因為所有調用都是同步的, Camunda Rest Create Process
現在如果用戶想從進程中讀取參數,則需要調用歷史服務,因為進程是同步的並已結束。
GET /history/variable-instance
,更多選項見進程歷史變量。
我沒有看到僅使用其余 api 的方法,即使使用嵌入式流程引擎在沒有額外查詢和流程變量的情況下執行此操作也有點棘手。
解決這個問題的一種方法是引入一個“上下文”對象,它要么由依賴注入框架管理,要么作為一個簡單的ThreadLocal
。 此上下文將包含輸入和輸出參數,並且可以在啟動流程和服務任務時訪問。
class Context {
private String address;
private List<String> products;
public Context(String address) { this.address = address; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public List<String> getProducts() { return products; }
public void setProducts(List<String> products) { this.products = products; }
}
class CurrentContext {
private static final ThreadLocal<Context> value = new ThreadLocal<>();
public static void set(Context context) {
value.set(context);
}
public static Context get() {
return value.get();
}
public static void remove() {
value.remove();
}
}
class GetProductList implements JavaDelegate {
@Override
public void execute(final DelegateExecution execution) {
final Context context = CurrentContext.get();
context.setProducts(getProducts(context.getAddress()));
}
}
在使用 java api 開始進程之前,您將初始化上下文,然后從上下文中檢索結果:
final Context context = new Context("address");
CurrentContext.set(context);
try {
runtimeService.startProcessInstanceById(...);
return context.getProducts();
} finally {
CurrentContext.remove();
}
我們非常成功地使用了這種模式(使用由自定義 spring 范圍管理的上下文)。
在我的理解中,進程應該始終被視為可以異步處理的東西。 出於這個原因,我會檢查替代方案,而不是嘗試從流程開始直接獲得結果。
一種常見的方法是您的應用程序提供充當回調處理程序的 Web 端點(例如 HTTP GET 端點)。 Camunda 進程可以在服務進程完成之前通知您的端點(將您的數據的一些標識符作為回調的一部分和結果)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.