ITeye博客

JBoss Enterprise SOA Platform 4.3 编程指南翻译之十二——开发自定义的动作

hsrong · 2012-02-23 11:10 · ESB · 企业架构 · Markdown

12. 开发自定义的动作 只要简单的实现了org.jboss.soa.esb.actions.ActionPipelineProcessor接口,就可以实现一个自定义动作处理器。 这个接口支持具有可管理的生命周期的无状态动作的实现。一个实现了该接口的类的实例是以一个管道为基础实例化的。这就意味着你能够在初始化方法中缓存动作所需资源并在销毁方法中清空这些资源。 实现类应当通过process方法的实现来处理消息。 为了方便,你只需要简单的继承类 [code="java"]public class ActionXXXProcessor extends AbstractActionPipelineProcessor { public void initialise() throws ActionLifecycleException { // 初始化资源... } public Message process(final Message message) throws ActionProcessingException { // 以一种无状态的方式处理消息... } public void destroy() throws ActionLifecycleException { // 清除资源... } }[/code] 12.1 使用属性对动作进行配置 一般的,Actions就像是一个需要外部配置来完成任务的模板。例如,PrintMessage动作使用一个名为“message”的属性来表明打印的内容和属性repeatCount来表明打印的次数。在jboss-esb.xml文件中的动作配置是这样的: [code="java"] [/code] 一个动作实现类加载属性值的默认方法是使用ConfigTree实例。这个ConfigTree提供了一个关于动作的Dom-like视图。默认情况下,动作有一个引用ConfigTree参数的构造函数。例如: [code="java"]public class PrintMessage extends AbstractActionPipelineProcessor { private String information; private Integer repeatCount; public PrintMessage(ConfigTree config) { information = config.getAttribute("information"); repeatCount = new Integer(config.getAttribute("repeatCount")); } public Message process(Message message) throws ActionProcessingException { for (int i=0; i < repeatCount; i++) { System.out.println(information); } } }[/code] 设置动作属性的另一个方式是在动作上添加相关属性的setter方法,以便框架自动完成属性的赋值。为了能使动作beab自动赋值,动作类必须实现 org.jboss.soa.esb.actions.BeanConfiguredAction接口。例如接下来的类和上面的那个有相同的作用: [code="java"]public class PrintMessage extends AbstractActionPipelineProcessor implements BeanConfiguredAction { private String information; private Integer repeatCount; public setInformation(String information) { this.information = information; } public setRepeatCount(Integer repeatCount) { this.repeatCount = repeatCount; } public Message process(Message message) { for (int i=0; i < repeatCount; i++) { System.out.println(information); } } }[/code] 注意:setRepeatCount()中的整形参数是从xml中指定的字符串自动转换的。 BeanConfiguredAction加载属性的方法是动作类获取参数的一个好的选择,然而,当需要直接处理xml形式参数的时候,ConfigTree方法要更好。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。