[英]How to check if file exists in Mule esb
I want to use choice flow control which will chose one route when file named #[function:dateStamp:dd-MM-yyyy].xml exists and other route when this file does not exist. 我想使用选择流控制,当名为#[function:dateStamp:dd-MM-yyyy] .xml的文件存在时,将选择一个路由,当此文件不存在时,将选择其他路由。
Is it possible to write 'when' part of choice to check for file existence? 是否可以写'何时'选择部分来检查文件是否存在?
You could use MEL: 您可以使用MEL:
<configuration>
<expression-language>
<import class="java.text.SimpleDateFormat" />
<global-functions><![CDATA[
def xmlFileExists() {
filePath = '/tmp/' + new SimpleDateFormat('dd-MM-yyyy').format(new Date()) + '.xml';
new File(filePath).isFile();
}
]]></global-functions>
</expression-language>
</configuration>
...
<choice>
<when expression="#[xmlFileExists()]">
...
</when>
<otherwise>
...
</otherwise>
</choice>
Not sure what that would look like but, you could always do it in plain Java. 不知道那会是什么样子但是,你总是可以用普通的Java来做。 Place a Java component in front of the choice:
将Java组件放在选择前面:
<component doc:name="CheckFileExists">
<singleton-object class="com.example.CheckFileExist">
</singleton-object>
</component>
Check for the file in the Java code and add a message property to the invocation scope. 检查Java代码中的文件,并将message属性添加到调用范围。
Then do the choice on the invocation property: 然后在调用属性上进行选择:
<choice doc:name="Choice">
<when expression="message.getInvocationProperty('thevariable')" evaluator="groovy">
<processor-chain>
....
</processor-chain>
</when>
<choice>
If you want to check for the files existence at a certain time or interval you can use quartz with the requester module 如果要在特定时间或间隔检查文件是否存在,可以使用quartz和请求者模块
<flow name="filePollQuartzFlow1" doc:name="filePollQuartzFlow1">
<quartz:inbound-endpoint jobName="job" repeatInterval="60000" startDelay="1000" responseTimeout="10000" doc:name="Quartz">
<quartz:event-generator-job/>
</quartz:inbound-endpoint>
<mulerequester:request config-ref="Mule_Requester" resource="file://#[function:dateStamp:dd-MM-yyyy].xml" doc:name="Request a message from a directory"/>
<choice doc:name="Choice">
<when expression="#[message.payload==null]">
<logger message="NO FILE FOUND" level="ERROR" doc:name="Log Null Payload"/>
</when>
<otherwise>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="FILE FOUND: #[message.payload]" level="ERROR" doc:name="Log Payload"/>
</otherwise>
</choice>
</flow>
This is how I ended up doing this: 这就是我最终这样做的方式:
<flow name="stateFileFlow" doc:name="stateFileFlow">
<file:inbound-endpoint connector-ref="input" path="/path" doc:name="File">
<file:filename-regex-filter pattern="(^ABC).xml" caseSensitive="true"/>
</file:inbound-endpoint>
<choice doc:name="Choice">
<when expression="#[message.outboundProperties['filename'] == null]">
<logger level="WARN" doc:name="Logger" message="NO FILE"/>
</when>
<otherwise>
<logger level="WARN" doc:name="Logger" message="FILE EXISTS"/>
<file:outbound-endpoint connector-ref="output" path="/path" doc:name="File"/>
</otherwise>
</choice>
</flow>
And this is yet another which make sit easier to work with an existing message: 这又是另一个让现在更容易使用现有消息:
<sub-flow name="stateFileFlow" doc:name="stateFileFlow">
<message-properties-transformer scope="invocation" overwrite="true" doc:name="Message Properties">
<add-message-property key="path" value="/path" />
<add-message-property key="format" value="dd-MM-yyyy" />
<add-message-property key="extension" value="flag" />
</message-properties-transformer>
<scripting:component doc:name="asdf">
<scripting:script engine="groovy">
<scripting:text>
def format = message.getInvocationProperty('format');
def path = message.getInvocationProperty('path');
def fileName = new Date().format(format) + "." + message.getInvocationProperty('extension');
def filePath = path + File.separator + fileName;
def exists = new File(filePath).isFile();
message.setProperty('exists',exists, org.mule.api.transport.PropertyScope.OUTBOUND);
message.setProperty('filename',fileName, org.mule.api.transport.PropertyScope.OUTBOUND);
return message.payload;
</scripting:text>
</scripting:script>
</scripting:component>
<choice doc:name="Choice">
<when expression="#[message.outboundProperties['exists'].toString() == 'true']">
<logger level="INFO" doc:name="Logger" message="FILE EXISTS"/>
</when>
<otherwise>
<logger level="INFO" doc:name="Logger" message="CREATING FILE #[message]"/>
<file:outbound-endpoint connector-ref="output" path="/path" doc:name="File"/>
</otherwise>
</choice>
</sub-flow>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.