续上篇介绍了第四个ESB应用,本文介绍第五个ESB应用——Custom Action。
说明:本文及后续文章虽非百分百的原创,但毕竟包含本人的努力和付出,所以希望大家转载时务请注明出处:http://yarafa.iteye.com,谢谢合作。
1 概述
本实例主要演示了action类的不同使用方式。
2 新建ESB工程
操作过程略。
3 ESB配置
3.1 创建消息队列
如概述中所描述,这里将创建两个消息队列。在esbcontent文件夹下创建文件jbm-queue-service.xml用于配置消息队列,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <server> <mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.esb.customaction.destination:service=Queue,name=customactionGw" xmbean-dd="xmdesc/Queue-xmbean.xml"> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer </depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean> <mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.esb.customaction.destination:service=Queue,name=customactionEsb" xmbean-dd="xmdesc/Queue-xmbean.xml"> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer </depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean> </server>
3.2 定义Provider
这里将定义一个JMS Provider,同时定义两个消息通道,一个用于监听客户端发来的消息,另一个用作ESB消息通道。内容如下:
<jms-provider connection-factory="ConnectionFactory" name="JBossMQ"> <jms-bus busid="customactionGwChanel"> <jms-message-filter dest-name="queue/customactionGw" dest-type="QUEUE" /> </jms-bus> <jms-bus busid="customactionEsbChanel"> <jms-message-filter dest-name="queue/customactionEsb" dest-type="QUEUE" /> </jms-bus> </jms-provider>
3.3 定义Service
<service category="CustomAction" description="Custom Action" name="CustomActionService"> <listeners /> <actions / </service>
3.4 定义Listener
这里定义两个listener,分别用于监听客户端消息和ESB内部消息。其中,前者用于监听客户端消息,因此需要将其is-gateway属性设置为true。
<jms-listener busidref="customactionGwChanel" is-gateway="true" name="JMSListener" /> <jms-listener busidref="customactionEsbChanel" name="EsbListener" />
3.5 定义Action类
下面将定义四个action,分别是:MyBasicAction,StatefulAction,CustomConfigAction和CustomBeanConfigAction,其中,CustomBeanConfigAction类在扩展了AbstractActionLifecycle类的基础上同时实现了BeanConfiguredAction接口。下面对各个action一一介绍。
3.5.1 MyBasicAction
这是一个基本的action,与之前的实例中的action功能类似,主要用于打印输出消息内容。类定义如下:
/*********************************************************************** * <p>Project Name: customaction</p> * <p>File Name: com.thu.afa.esb.jbossesb.action.MyBasicAction.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.Message; /** * <p>Class Name: MyBasicAction</p> * <p>Description: </p> * @author Afa * @date 2010-9-10 * @version 1.0 */ public class MyBasicAction extends AbstractActionLifecycle { protected ConfigTree configTree; public MyBasicAction(ConfigTree configTree) { this.configTree = configTree; } public Message noOperation(Message message) { return message; } public Message process(Message message) { System.out.println("The default process method called"); return message; } public Message printMessage(Message message) throws Exception { System.out.println("Message Body: " + message.getBody().get().toString()); return message; } public void exceptionHandler(Message message, Throwable exception) { System.out.println("!ERROR!"); System.out.println(exception.getMessage()); System.out.println("For Message: "); System.out.println(message.getBody().get()); } }
3.5.2 StatefulAction
该类主要用于演示在一个action配置中同时调用多个方法时,action的实例个数。简单的说,在一个action配置中,不论调用多少个方法,都只会有一个action类的实例。
/*********************************************************************** * <p>Project Name: customaction</p> * <p>File Name: com.thu.afa.esb.jbossesb.action.StatefulAction.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.Message; /** * <p>Class Name: StatefulAction</p> * <p>Description:</p> * * @author Afa * @date 2010-9-10 * @version 1.0 */ public class StatefulAction extends AbstractActionLifecycle { protected ConfigTree configTree; private int count = 0; public StatefulAction(ConfigTree configTree) { System.out.println("\n\nConstructor - " + this.getClass().getName() + "\n"); count++; this.configTree = configTree; } public Message noOperation(Message message) { return message; } public Message methodOne(Message message) throws Exception { count++; System.out.println("methodOne Called: " + count); return message; } public Message methodTwo(Message message) throws Exception { count++; System.out.println("methodOne Called: " + count); return message; } public Message displayCount(Message message) throws Exception { System.out.println("DisplayCount count = " + count + "\n"); return message; } public void exceptionHandler(Message message, Throwable exception) { System.out.println("!ERROR!"); System.out.println(exception.getMessage()); System.out.println("For Message: "); System.out.println(message.getBody().get()); } }
3.5.3 CustomConfigAction
该类主要用于演示如何访问在jboss-esb.xml配置中的action节点及其子节点的信息。
/*********************************************************************** * <p>Project Name: customaction</p> * <p>File Name: com.thu.afa.esb.jbossesb.action.CustomConfigAction.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 java.util.Set; import org.jboss.soa.esb.actions.AbstractActionLifecycle; import org.jboss.soa.esb.helpers.ConfigTree; import org.jboss.soa.esb.message.Message; /** * <p>Class Name: CustomConfigAction</p> * <p>Description: </p> * @author Afa * @date 2010-9-10 * @version 1.0 */ public class CustomConfigAction extends AbstractActionLifecycle { protected ConfigTree configTree; public CustomConfigAction(ConfigTree configTree) { this.configTree = configTree; } public Message displayConfig(Message message) throws Exception { Set<String> names = configTree.getAttributeNames(); System.out.println("****************************"); for (String attrName : names) { String value = configTree.getAttribute(attrName); System.out.println("Attribute: " + attrName + " Value: " + value); } System.out.println("****************************"); ConfigTree[] subElements = configTree.getAllChildren(); // Note: even a sub-element can have attributes but trying to keep this simple System.out.println("############################"); for (ConfigTree child : subElements) { System.out.println("SubElement: " + child.getName() + " Body: " + child.getWholeText()); } System.out.println("############################"); return message; } }
3.5.4 CustomBeanConfigAction
该类演示了如何在jboss-esb.xml配置中对action类属性的注入,这一点与spring的依赖注入有异曲同工之处。需要注意的是:在该类中不能有ConfigTree类型的属性以及包含该类型参数的构造方法。具体原因有待于进一步研究。
/*********************************************************************** * <p>Project Name: customaction</p> * <p>File Name: com.thu.afa.esb.jbossesb.action.CustomBeanConfigAction.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.actions.BeanConfiguredAction; import org.jboss.soa.esb.message.Message; /** * <p>Class Name: CustomBeanConfigAction</p> * <p>Description: </p> * @author Afa * @date 2010-9-10 * @version 1.0 */ public class CustomBeanConfigAction extends AbstractActionLifecycle implements BeanConfiguredAction { private String information; private Integer repeatCount; private String serviceCategory; private String serviceName; public String getInformation() { return information; } public void setInformation(String information) { this.information = information; } public Integer getRepeatCount() { return repeatCount; } public void setRepeatCount(Integer repeatCount) { this.repeatCount = repeatCount; } public String getServiceCategory() { return serviceCategory; } public void setServiceCategory(String serviceCategory) { this.serviceCategory = serviceCategory; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } public Message process(Message message) throws Exception { System.out.println("[" + serviceCategory + ":" + serviceName + "] Repeat message: " + information + " " + repeatCount + " times:"); for (int i=0; i < repeatCount; i++) { System.out.println(information); } return message; } }
3.6 配置Action
<actions> <action class="com.thu.afa.esb.jbossesb.action.MyBasicAction" name="firstAction" process="printMessage"> <property name="exceptionMethod" value="exceptionHandler" /> </action> <action class="com.thu.afa.esb.jbossesb.action.MyBasicAction" name="secondAction"> <property name="exceptionMethod" value="exceptionHandler" /> </action> <action class="com.thu.afa.esb.jbossesb.action.MyBasicAction" name="thirdAction" process="printMessage"> <property name="exceptionMethod" value="exceptionHandler" /> </action> <action class="com.thu.afa.esb.jbossesb.action.StatefulAction" name="fourthAction" process="methodOne,methodTwo,displayCount"> <property name="exceptionMethod" value="exceptionHandler" /> </action> <action class="com.thu.afa.esb.jbossesb.action.CustomConfigAction" name="fifthAction" process="displayConfig"> <property name="myStuff" value="rocks" /> <property name="moreStuff" value="rocks harder" /> <property name="subElements"> <subElement1>Value of 1</subElement1> <subElement2>Value of 2</subElement2> <subElement3>Value of 3</subElement3> </property> </action> <action class="com.thu.afa.esb.jbossesb.action.MyBasicAction" name="sixthAction" process="printMessage"> <property name="exceptionMethod" value="exceptionHandler" /> </action> <action name="seventhAction" class="com.thu.afa.esb.jbossesb.action.CustomBeanConfigAction> <property name="information" value="Hola Mundo" /> <property name="repeatCount" value="5" /> </action> </actions>
3.7 配置部署文件
部署依赖文件deployment.xml内容如下:
<jbossesb-deployment> <depends>jboss.esb.customaction.destination:service=Queue,name=customactionGw </depends> <depends>jboss.esb.customaction.destination:service=Queue,name=customactionEsb </depends> </jbossesb-deployment>
3.8 部署ESB
将整个工程导出成一个ESB文件,并保存至JBoss ESB Server的部署目录下,启动JBoss ESB Server即可。
4 ESB客户端
4.1 新建Java工程
这里略去操作过程以及添加所需要的Jar包,具体操作过程可参考第一个ESB实例说明。
4.2 发送消息的客户端
/*********************************************************************** * <p>Project Name: helloworldclient</p> * <p>File Name: com.thu.afa.esb.jbossesb.client.CustomActionClient.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.util.Properties; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.naming.Context; import javax.naming.InitialContext; /** * <p>Class Name: CustomActionClient</p> * <p>Description: </p> * @author Afa * @date 2010-9-10 * @version 1.0 */ public class CustomActionClient { private QueueConnection connection; private QueueSession session; private Queue queue; public void setupConnection() throws Exception { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); properties.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099"); InitialContext context = new InitialContext(properties); QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory"); connection = factory.createQueueConnection(); queue = (Queue) context.lookup("queue/customactionGw"); session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); connection.start(); System.out.println("Connection Started"); } public void stop() throws Exception { if(connection != null) connection.stop(); if(session != null) session.close(); if(connection != null) connection.close(); } public void sendAMessage(String text) throws Exception { QueueSender sender = session.createSender(queue); ObjectMessage message = session.createObjectMessage(text); sender.send(message); sender.close(); } /** * <p>Title: </p> * <p>Method Name: main</p> * <p>Description: </p> * @author: Afa * @date: 2010-9-10 * @param args */ public static void main(String[] args) throws Exception { CustomActionClient client = new CustomActionClient(); client.setupConnection(); client.sendAMessage("Llu, miss you, afa, custom action"); client.stop(); } }
运行客户端程序即可看到以下输出结果:
其中,在第一个结果输出图中,
Constructor - com.thu.afa.esb.jbossesb.action. StatefulAction,这一行信息是在部署ESB时实例化StatefulAction类时,由其构造方法打印输出的结果。
上述便是ESB第五个应用实例。如有问题,欢迎指正。
-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Jan 15th, 2011
-----------------------------------------------------