---
title: "JBoss ESB学习笔记5——第四个ESB应用Hello World File Action"
author: "yarafa"
date: "2011-01-08T11:23:34Z"
tags: ["Eclipse", "JBoss", "JMS", "SOA", "XML"]
categories: ["编程语言"]
slug: "jboss-esb-5-esb-hello-world-file-action"
draft: false
lastmod: "2011-01-08T11:23:34Z"
id: 865785
url: "https://yarafa.iteye.com/blog/865785"
---

续上篇介绍了第三个ESB应用，本文介绍第四个ESB应用——Hello World File Action。

 

**说明：本文及后续文章虽非百分百的原创，但毕竟包含本人的努力和付出，所以希望大家转载时务请注明出处：**[**http://yarafa.iteye.com**](http://yarafa.iteye.com/)**，谢谢合作。**

 

 

**1 概述**  
本实例演示了JBoss ESB的File Gateway的使用及特性，当ESB所监听的特定目录下具有特定扩展名的文件发生变化时，将发送一个JMS消息到消息队列，然后由ESB处理该消息。最后根据ESB的配置将输出一个具有特定扩展名的文件到输出目录。

 

**2 新建ESB工程**  
操作过程略。

 

**3 ESB配置**  
**3.1 创建消息队列**  
本例中只用到了一个消息队列，如下所示：

```
<?xml version="1.0" encoding="UTF-8"?>
<server>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.helloworld.destination:service=Queue,name=helloworldfile" xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer
		</depends>
	</mbean>
</server>
```

 

**3.2 定义Provider**  
这里将定义一个fs-provider和一个JMS Provider，分别用于监听文件系统的变化和监听ESB消息。内容如下：

```
<fs-provider name="FSprovider1">
	<fs-bus busid="helloFileChannel">
		<fs-message-filter
directory="F:\Workspace\Eclipse-JEE\helloworldfile\file\input"
error-delete="false"
error-directory="F:\Workspace\Eclipse-JEE\helloworldfile\file\error"
			error-suffix=".IN_ERROR" input-suffix=".txt" post-delete="false"
post-directory="F:\Workspace\Eclipse-JEE\helloworldfile\file\output"
			post-suffix=".sentToEsb"
work-suffix=".esbWorking" />
	</fs-bus>
</fs-provider>
<jms-provider connection-factory="ConnectionFactory"
	name="JBossMessaging">
	<jms-bus busid="quickstartEsbChannel">
		<jms-message-filter dest-name="queue/helloworldfile"
			dest-type="QUEUE"
selector="type='fromHelloworldFileAction'" />
	</jms-bus>
</jms-provider>
```

fs-provider配置说明：

directory：ESB的监听目录  
input-suffix：监听文件的扩展名，不是指定扩展名的文件将被忽略  
error-delete：如果发生内部错误或者ESB无法加载文件，该属性决定是否删除文件  
error-directory：在文件加载或者处理失败时的文件存放目录  
error-suffix：发生错误时的文件扩展名  
post-directory：当post-delete属性设置为false且未发生错误的情况下，该目录用于存放处理后的文件  
post-suffix：处理后的文件扩展名  
work-suffix：正在被处理时的文件扩展名，“被处理”指的是文件已通过gateway并已被传输到ESB listener/service。

 

**3.3 定义Service**

```
<service category="myCategory" description="Hello World File Action
(esb listener)" name="myFileListener">
	<listeners />
	<actions /
</service>
```

 

**3.4 定义Listener**  
这里定义两个listener，分别用于监听文件通道和消息队列，内容如下：

```
<fs-listener busidref="helloFileChannel" is-gateway="true"
	name="FileGateway" poll-frequency-seconds="10" />
<jms-listener busidref="quickstartEsbChannel"
name="helloWorldFileAction" />
```

 

**3.5 定义Action类**  
这里使用自定义的action类，因此需要扩展AbstractActionLifecycle类。类的全部代码如下：

```
/***********************************************************************
 * <p>Project Name: helloworldfile</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.HelloWorldFileAction.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.action;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Body;
import org.jboss.soa.esb.message.Message;

/**
 * <p>Class Name: HelloWorldFileAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-8
 * @version 1.0
 */
public class HelloWorldFileAction extends AbstractActionLifecycle
{
	protected ConfigTree configTree;
	
	public HelloWorldFileAction(ConfigTree configTree)
	{
		this.configTree = configTree;
	}
	
	public Message noOperation(Message message) { return message; }
	
	public Message displayMessage(Message message) throws Exception
	{
		System.out.println("Body: " + new String((byte[])message.getBody().get()));
		
		return message;
	}
	
	public Message playWithMessage(Message message) throws Exception
	{
		Body body = message.getBody();
		String content = new String((byte[])body.get());
		StringBuffer buffer = new StringBuffer();
		buffer.append("Before------------------");
		buffer.append(content);
		buffer.append("After-------------------");
		body.add(buffer.toString());
		
		return message;
	}
}
```

类方法说明：

displayMessage方法用于在控制台打印输出消息的内容

playWithMessage方法用于处理消息

 

**3.6 配置Action**  
由于使用了自定义的action类，因此在说明action配置之前介绍了action类的定义。下面将说明action的配置。

```
<actions mep="OneWay">
	<action
class="com.thu.afa.esb.jbossesb.action.HelloWorldFileAction"
	name="myAction" process="displayMessage,playWithMessage" />
	<action class="org.jboss.soa.esb.actions.SystemPrintln"
name="dump">
		<property name="message" />
		<property name="printfull" value="true" />
	</action>
</actions>
```

配置说明：这里定义了两个action，分别用自定义的action类显示文件内容并做相应的处理后，再由ESB本身提供的SystemPrintln类在控制台输出经处理后的消息内容。 

 

**3.7 配置部署文件**  
部署依赖文件deployment.xml内容如下：

```
<jbossesb-deployment>
	<depends>jboss.esb.helloworld.destination:service=Queue,name=helloworldfile
	</depends>
</jbossesb-deployment>
```

 

**3.8 部署ESB**  
将整个工程导出成一个ESB文件，并保存至JBoss ESB Server的部署目录下，启动JBoss ESB Server即可。

**4 ESB客户端**  
**4.1 新建Java工程**  
这里略去操作过程以及添加所需要的Jar包，具体操作过程可参考第一个ESB实例说明。

**4.2 客户端实现**  
这里的客户端生成一个文本文件，并保存在ESB的监听目录下，之后可看到ESB的处理操作。

```
/***********************************************************************
 * <p>Project Name: helloworldclient</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.client.HelloWorldFileClient.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.client;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 * <p>Class Name: HelloWorldFileClient</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-9
 * @version 1.0
 */
public class HelloWorldFileClient
{

	/**
	 * <p>Title: </p>
	 * <p>Method Name: main</p>
	 * <p>Description: </p>
	 * @author: Afa
	 * @date: 2010-9-9
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		File file = new File("F:/Workspace/Eclipse-JEE/helloworldfile/file/input/test.txt");
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		out.write("Llu, miss you, afa");
		out.close();
	}

}
```

 

运行该程序可看到如下输出结果：

处理中的文件：

![JBoss ESB](http://dl.iteye.com/upload/picture/pic/79277/59f37248-305a-3fcb-9b07-b63b1d3c0bf0.png)  

 

 

处理后的输出文件：

![JBoss ESB](http://dl.iteye.com/upload/picture/pic/79275/485c5612-02c6-302f-af4b-dc67ffd22546.png) 

 

 

ESB的SystemPrintln类在控制台输出结果：

![JBoss ESB](http://dl.iteye.com/upload/picture/pic/79273/de7d7d4b-2d7d-3ea9-b9dc-3c20283c6124.png) 

 

上述便是ESB第四个应用实例。如有问题，欢迎指正。

 

 

**-----------------------------------------------------**  
[**Stay Hungry, Stay Foolish!**](http://yarafa.weebly.com/)  
[**http://yarafa.iteye.com**](http://yarafa.iteye.com)  
**Afa**  
**Jan 8th, 2011**  
**-----------------------------------------------------**