如何在每个任务调用之间保留属性值 [英] How to reserve property value between every task call

查看:19
本文介绍了如何在每个任务调用之间保留属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

得到它

<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 的值发生了变化.

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.

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

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.

推荐答案

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

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

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

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>

这篇关于如何在每个任务调用之间保留属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆