我们都有这样的需求,在往一个文件夹下放入某些特定的文件后,就希望它能够自动做些什么事。就好比JBoss的热部署,而不是像tomcat那样,部署个什么东西还得再从启一下。
JBossESB就为我们提供了一个这样的监听机制,这篇说一下监听本地的一个文件夹,下篇一下监测ftp服务器上的。
在JBossESB的provider中,有一个fs-provider,这个就是用来对某个文件夹进行监听的。直接上例子,通过例子来对这个进行一下说明。
首先,是最最重要的Jboss-esb.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<jbossesb xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd" parameterReloadSecs="5" >
<providers>
<fs-provider name="FSprovider1">
<fs-bus busid="helloFileChannel" >
<fs-message-filter
directory="D:\JBossESB\jboss-5.1.0.GA\samples\quickstarts\helloworld_file_action\build\dirs\input"
input-suffix=".dat"
work-suffix=".esbWorking"
post-delete="false"
post-directory="D:\JBossESB\jboss-5.1.0.GA\samples\quickstarts\helloworld_file_action\build\dirs\output"
post-suffix=".sentToEsb"
error-delete="false"
error-directory="D:\JBossESB\jboss-5.1.0.GA\samples\quickstarts\helloworld_file_action\build\dirs\error"
error-suffix=".IN_ERROR"
/>
</fs-bus>
</fs-provider>
<jms-provider name="JBossMessaging" connection-factory="ConnectionFactory" >
<jms-bus busid="quickstartEsbChannel">
<jms-message-filter
dest-type="QUEUE"
dest-name="queue/quickstart_helloworld_file_esb"
selector="type='fromHelloworldFileAction'"
/>
</jms-bus>
</jms-provider>
</providers>
<services>
<service category="myCategory" name="myFileListener" description="监听到文件后" >
<listeners>
<fs-listener name="FileGateway" busidref="helloFileChannel" is-gateway="true"
poll-frequency-seconds="10"
/>
<jms-listener name="helloWorldFileAction" busidref="quickstartEsbChannel" />
</listeners>
<actions mep="OneWay">
<action name="testAction" class="org.jboss.soa.esb.actions.SystemPrintln" />
<!-- 依次调用MyAction中的 displayMessage和playWithMessage方法 -->
<action name="action1"
class="org.jboss.soa.esb.samples.quickstart.helloworldfileaction.MyAction"
process="displayMessage,playWithMessage"
/>
<action name="dump" class="org.jboss.soa.esb.actions.SystemPrintln">
<property name="printfull" value="true"/>
</action>
<!-- 这个是用来打印一下Message中的信息 -->
<action name="testStore" class="org.jboss.soa.esb.actions.TestMessageStore" />
</actions>
</service>
</services>
</jbossesb>
MyAction中的代码很简单:
public class MyAction extends AbstractActionLifecycle
{
protected ConfigTree _config;
public MyAction(ConfigTree config) { _config = config; }
public Message noOperation(Message message) { return message; }
//将处理文件进行打印
public Message displayMessage(Message message) throws Exception{
logHeader();
System.out.println("Body: " + new String((byte[])message.getBody().get()));
logFooter();
return message;
}
//对文件进行编辑
public Message playWithMessage(Message message) throws Exception {
Body msgBody = message.getBody();
String contents = new String((byte[])msgBody.get());
StringBuffer sb = new StringBuffer();
sb.append("\nBEFORE\n");
sb.append(contents);
sb.append("\nAFTER\n");
msgBody.add(sb.toString().getBytes());
return message;
}
// This makes it easier to read on the console
private void logHeader() {
System.out.println("\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
}
private void logFooter() {
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
}
}
为了简单,其它的就不再列出了,只是用来声明队列、部署说明什么的。
现在再来看jboss-esb.xml,在这个fs-provider中:
- 对监听文件夹(fs-message-filter)、监听文件类型(input-suffix)、正在处理的文件将后缀修改为(work-suffix);
- 处理完毕后,文件放到哪(post-directory),将文件类型改为(post-suffix);
- 如果文件处理过程中出错了,文件放到哪哪(error-directory),将文件类型改为(error-suffix)
- 以及,处理完成后是否删除(post-delete)、出错后是否删除(error-delete)。
说了这么多,那么这个可以用来干什么呢?我想这个就看你有什么样的需求了,例如,B做事是需要A提交文档后才能够开始做事的,那么B需要一直去问A提交文档或者说A提交后去提示一下B,那么这时就可以使用这个。下篇将说一下怎样监听一个远程的文件夹(Ftp服务器上的~)