我有一个场景,我需要在配置注册表中存储简单的计数器,并在序列流结束时递增它。我们需要在配置注册表中存储的原因是,如果服务器重新启动,我们将持有最后一个计数器值。有人可以建议如何增加配置注册表中的计数器吗?
答案 0 :(得分:4)
您可以在中介中使用示例javascript来保存注册表中的当前消息:
<script language="js"><![CDATA[
importPackage(Packages.org.apache.synapse.config);
mc.getConfiguration().getRegistry().newResource("gov:/trunk/mypath/MyResource.xml",false);
mc.getConfiguration().getRegistry().updateResource("gov:/trunk/mypath/MyResource.xml",mc.getPayloadXML().toString());
]]></script>
第一次使用newResource来创建资源
答案 1 :(得分:0)
我有适合您的解决方案!
<script language="nashornJs"><![CDATA[
var body = mc.getPayloadXML();
print(body);
var registryPath = "gov:/portales/date.xml";
if(body != null && body != ''){
var existingProperty = mc.getConfiguration().getRegistry().getResource(registryPath);
print(body);
if(existingProperty == null){
print(body);
// Create the registry entry if no such entry exists.
mc.getConfiguration().getRegistry().newResource(registryPath, false);
mc.getConfiguration().getRegistry().updateResource(registryPath, body);
} else {
print(body);
// Update the registry entry if it already exists.
mc.getConfiguration().getRegistry().updateResource(registryPath, body);
}
}]]></script>
答案 2 :(得分:0)
我使用这种方式从 POST 请求中获取 json 负载并以 xml 格式存储在注册表中
<datamapper config="gov:datamapper/conversionToSaveInRegistry.dmc" description="conversionToSaveInRegistry" inputSchema="gov:datamapper/conversionToSaveInRegistry_inputSchema.json" inputType="JSON" outputSchema="gov:datamapper/conversionToSaveInRegistry_outputSchema.json" outputType="XML" xsltStyleSheet="gov:datamapper/conversionToSaveInRegistry_xsltStyleSheet.xml"/>
<property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
<script language="nashornJs"><![CDATA[
var body = mc.getPayloadXML();
var registryPath = "gov:/generated/date.xml";
if(body != null && body != ''){
var existingProperty = mc.getConfiguration().getRegistry().getResource(registryPath);
if(existingProperty == null){
// Create the registry entry if no such entry exists.
mc.getConfiguration().getRegistry().newResource(registryPath, false);
mc.getConfiguration().getRegistry().updateResource(registryPath, body);
} else {
// Update the registry entry if it already exists.
mc.getConfiguration().getRegistry().updateResource(registryPath, body);
}
}]]></script>
<property name="NO_ENTITY_BODY" scope="axis2" type="BOOLEAN" value="true"/>
<property name="HTTP_SC" scope="axis2" type="STRING" value="201"/>