[英]How to create a global variable in WSO2 ESB - must be updatable
我编写了一个ESB资源,其中包含一系列测试案例,例如以下示例:
{ "tests": [ { "type": "DSS", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "ESB", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "GREG", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "GW", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "MB", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "ID", "url": "http://localhost:8280/testsuite/general/test" }, { "type": "BOGUS", "url": "http://localhost:8280/testsuite/general/test" } ] }
资源将其作为JSON数组并使用XPATH表达式//tests/type
遍历元素,并生成状态消息(如果可以执行任何操作,则返回200
否则,则返回ERR
)。
理想情况下,我希望能够将类型递增地添加到某种全局数组属性中,该属性最终将变为[DSS, ESB, GREG, GW, MB, ID, BOGUS]
因此我可以编写脚本来运行这并产生有效载荷以返回如下所示的内容:
{ "results": [ { "TYPE": "DSS", "STATUS": "200" }, { "TYPE": "ESB", "STATUS": "200" }, { "TYPE": "GREG", "STATUS": "200" }, { "TYPE": "GW", "STATUS": "200" }, { "TYPE": "MB", "STATUS": "200" }, { "TYPE": "ID", "STATUS": "200" }, { "TYPE": "BOGUS", "STATUS": "ERR" } ] }
我一直在寻找一种方法来这样做,但无济于事,因此,如果根本不可能,或者如果我只是在寻找错误的东西,我不知道,但我真的会希望避免产生静态资源,而对我来说却可以以最少的参与来实现将来的扩展。
您可以使用以下脚本将有效负载保存在gov注册表中:
<script language="js"><![CDATA[
importPackage(Packages.org.apache.synapse.config);
mc.getConfiguration().getRegistry().newResource("gov:/trunk/Test/TestTypes",false);
mc.getConfiguration().getRegistry().updateResource("gov:/trunk/Test/TestTypes",mc.getPayloadXML().toString());
]]></script>
具有资源“ TESTSOF”的样本如下:
<root>
<value><child>1</child></value>
<value><child>2</child></value>
<value><child>3</child></value>
</root>
您可以使用以下命令迭代所有“值”:
<property name="TESTSOF" expression="get-property('registry','gov:/trunk/TESTSOF')" type="OM"/>
<iterate continueParent="true" sequential="false" preservePayload="false" expression="$ctx:TESTSOF//value">
<target>
<sequence>
<log level="full"/>
</sequence>
</target>
</iterate>
带有JSON的示例:加载JSON作为当前消息:
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('registry','gov:/trunk/Test/TestTypes')"/>
</args>
</payloadFactory>
重复:
<iterate continueParent="true" sequential="false" preservePayload="false" expression="//tests">
<target>
<sequence>
<log level="full"/>
</sequence>
</target>
</iterate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.