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