如何在每次任务调用之间保留 属性 值

How to reserve property value between every task call

我使用 WSO2 ESB 计划任务从外部系统获取数据,该任务每 5 秒调用一次我的代理服务。在我的代理服务中,我使用了一个 属性 名称 "startTime" 和 "endTime",这意味着我想从 "startTime" 到 "endTime" 获取数据。 "startTime" 和 "endTime" 应该在每次任务调用时增加 5 秒。 但似乎 ESB 无法在每次任务调用之间存储这些属性(startTime 和 endTime)。我尝试使用脚本编写 "startTime" :

importPackage(Packages.org.apache.synapse.config);  
var id = mc.getProperty("id");
var res = "conf/data_task/"+id ;
var startTimeInReg = mc.getProperty("_endTime");
mc.getConfiguration().getRegistry().updateResource(res+"/startTime", startTimeInReg.toString());

得到它

<property expression="get-property('registry', fn:concat('conf/data_task/',get-property('id'),'/startTime'))"
    name="startTimeInReg" scope="default" type="STRING"/>

我可以得到"startTime",但它保持相同的值,我发现在调度任务调用 2 或 3 次后(可能超过 15 秒),startTime 的值发生变化。

我认为这可能是由 ESB 缓存引起的,我如何在调用 updateResource 方法后立即提交 startTime 值的更改。或者如何解决这个问题。

尝试在治理注册表中保存您的值:

mc.getConfiguration().getRegistry().newResource("gov:/trunk/test/MyCounter.txt",false); // create the resource the 1st time, does nothing the others
mc.getConfiguration().getRegistry().updateResource("gov:/trunk/test/MyCounter.txt", startTimeInReg.toString()); 

另一个解决方案,看看这个创建 "global" 计数器的示例(ESB 重新启动时丢失):

<script language="js"><![CDATA[                         
    var curValue = mc.getEnvironment().getServerContextInformation().getProperty("MyCounter");
    if (curValue == null) {             
        curValue = 0;           
    } else {
        curValue++;
    }
    mc.getEnvironment().getServerContextInformation().addProperty("MyCounter",curValue);
    mc.setProperty("MyCounter",curValue);
]]></script>