簡體   English   中英

構建 Kie 會話線程安全嗎?

[英]Is building Kie sessions thread safe?

我們有一些代碼可以並行創建大約 1500 個 Kie 會話,我的印象是它不是線程安全的。 我檢查了我們所有的無狀態代碼,我的難題中最后遺漏的部分是以下代碼:

public KieSession buildSession(Config configuration) throws InvalidConfigurationException {
    KieFileSystem kfs = this.kieServices.newKieFileSystem();

    addRules(config, kfs); // Stateless

    this.kieServices.newKieBuilder(kfs).buildAll();

    KieBaseConfiguration kieBaseConfiguration = KieServices.Factory.get().newKieBaseConfiguration();
    kieBaseConfiguration.setOption(EventProcessingOption.STREAM);

    KieContainer kieContainer = this.kieServices.newKieContainer(this.kieServices.getRepository().getDefaultReleaseId());
    KieBase kieBase = kieContainer.newKieBase(kieBaseConfiguration);

    return buildSession(configuration.getHouseholdId(), kieBase);
}

public KieSession buildSession(KieBase kieBase) {
    KieSessionConfiguration kieSessionConfiguration = KieServices.Factory.get().newKieSessionConfiguration();
    kieSessionConfiguration.setOption(ClockTypeOption.get("pseudo"));

    KieSession kieSession = kieBase.newKieSession(kieSessionConfiguration, null);

    return kieSession;
}

我們將規則作為字符串添加到文件系統。 文件說:

However, since in this case the KieFileSystem doesn’t contain any pom.xml file (it is possible to add one using the KieFileSystem.writePomXML method though), Kie cannot determine the `ReleaseId` of the KieModule and assign to it a default one.
This default `ReleaseId` can be obtained from the KieRepository and used to identify the KieModule inside the KieRepository itself.

所以我的猜測是,在這種情況下,由於文件系統中沒有 pom 文件,我們將始終使用默認的ReleaseId並具有等效於用於存儲 Kie 模塊的臨時變量。 然后兩個線程可以覆蓋這個變量,這意味着在某些情況下,該方法會為配置返回錯誤的 kie 會話。

這個對嗎? 如果沒有,我該如何解決?

抱歉我遲到的回答。

我的假設實際上是正確的,當您創建一個新的 KieFileSystem 時,它會使用發布 ID 在存儲庫中注冊。 當您不指定發布 ID 時,將使用默認發布 ID,您可以覆蓋另一個線程的文件系統。 它看起來是一個哈希圖,其中所有線程都使用相同的鍵。

所以我們做了什么來創建我們自己的發布 ID,最終代碼看起來像這樣

ReleaseId releaseId = new HouseholdReleaseId(configuration.getHouseholdId()...);

KieFileSystem kfs =
   this.kieServices.newKieFileSystem()
      .generateAndWritePomXML(releaseId); // Set the release id here.

// Do all the stuff with the file system.

KieContainer kieContainer = this.kieServices.newKieContainer(releaseId); // Same release id.
KieBase kieBase = kieContainer.newKieBase(...);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM