How to reserve property value between every task call

孤者浪人 提交于 2019-12-11 10:55:04

问题


I used WSO2 ESB schedule task to fetch data from external system, the task call my proxy service every 5 seconds. In my proxy service, I used a property name "startTime" and "endTime", it means I want to fetch data from "startTime" to "endTime". "startTime" and "endTime" should be increase 5 seconds every task call. But it seems ESB cannot store these properties(startTime and endTime) between every task call. I try to use the script to write the "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());

and get it

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

I can get the "startTime", but it remain the same value , and I found that after 2 or 3 times schedule task call ( maybe elaps more than 15s ), the value of startTime change.

I think this maybe caused by ESB caching , how I can commit the changing of the startTime value immediately after updateResource method called. Or how can solve this issue.


回答1:


Try to save your value in the governance registry :

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

An other solution, have a look at this sample that create a "global" counter (lost when the ESB is restarted) :

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


来源:https://stackoverflow.com/questions/36565969/how-to-reserve-property-value-between-every-task-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!