Java 如何检查Mule esb中是否存在文件
我想使用choice flow control,当文件名为#[function:dateStamp:dd-MM-yyyy].xml存在时,它将选择一个路由,当该文件不存在时,它将选择其他路由Java 如何检查Mule esb中是否存在文件,java,mule,esb,Java,Mule,Esb,我想使用choice flow control,当文件名为#[function:dateStamp:dd-MM-yyyy].xml存在时,它将选择一个路由,当该文件不存在时,它将选择其他路由 是否可以选择编写“when”部分来检查文件是否存在?不确定这样做会是什么样子,但是,您始终可以用普通Java来完成。将Java组件放在选项前面: <component doc:name="CheckFileExists"> <singleton-object class="
是否可以选择编写“when”部分来检查文件是否存在?不确定这样做会是什么样子,但是,您始终可以用普通Java来完成。将Java组件放在选项前面:
<component doc:name="CheckFileExists">
<singleton-object class="com.example.CheckFileExist">
</singleton-object>
</component>
检查Java代码中的文件,并向调用范围添加消息属性
然后在调用属性上进行选择:
<choice doc:name="Choice">
<when expression="message.getInvocationProperty('thevariable')" evaluator="groovy">
<processor-chain>
....
</processor-chain>
</when>
<choice>
....
您可以使用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>
...
...
...
如果您想在特定时间或间隔检查文件是否存在,您可以将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>
我就是这样做的:
<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>
这是另一个使sit更容易处理现有消息的方法:
<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>
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=新文件(filePath).isFile();
message.setProperty('exists',exists,org.mule.api.transport.PropertyScope.OUTBOUND);
message.setProperty('filename',filename,org.mule.api.transport.PropertyScope.OUTBOUND);
返回消息.payload;
您能解释一下如何将消息属性添加到调用范围吗?只需使用“Callable”实现javaclass并获取mulemessage即可。您将得到一个onCall(muleeventcontexteventcontext)方法。然后可以执行并返回消息eventContext.getMessage()。setInvocationProperty(arg0,arg1)是否有人遇到过此解决方案的问题?对我来说,它在重启后工作一次,然后给出错误,说我的fileExists函数不存在(讽刺的是!我知道,对吧?)-谢谢你使用DataMapper吗?如果是,则存在DataMapper清除表达式全局配置的已知问题: