续上篇介绍了第九个ESB应用,本文介绍第十个ESB应用——Message Filters。
说明:本文及后续文章虽非百分百的原创,但毕竟包含本人的努力和付出,所以希望大家转载时务请注明出处:http://yarafa.iteye.com,谢谢合作。
1 概述
本实例主要演示了如何设置基于内容的过滤器。这里通过客户端发送Order对象到ESB,在处理消息之前,过滤器将根据Order对象的quantity属性进行过滤,当该属性的值大于10时,消息将被传送到NextService服务进行处理,否则,该消息将会被过滤掉不做任何处理。
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.quickstart.destination:service=Queue,name=messagefilterRequestEsb" 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.quickstart.destination:service=Queue,name=messagefilterRequestGw" 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.quickstart.destination:service=Queue,name=messagefilterNextRequestEsb" 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,并定义3个消息通道,内容如下:
<jms-provider connection-factory="ConnectionFactory" name="JBossMessaging"> <jms-bus busid="gwChanel"> <jms-message-filter dest-name="queue/messagefilterRequestGw" dest-type="QUEUE" /> </jms-bus> <jms-bus busid="esbChanel"> <jms-message-filter dest-name="queue/messagefilterRequestEsb" dest-type="QUEUE" /> </jms-bus> <jms-bus busid="nextEsbChanel"> <jms-message-filter dest-type="QUEUE" dest-name="queue/messagefilterNextRequestEsb" /> </jms-bus> </jms-provider>
3.3 定义第一个Service
<service category="MessageFilter" description="Message Filter" name="MessageFilterService"> <listeners /> <actions mep="OneWay"> <action /> </actions> </service>
3.3.1 定义Listener
<listeners> <jms-listener busidref="gwChanel" is-gateway="true" name="gwListener" /> <jms-listener busidref="esbChanel" name="esbListener" /> </listeners>
3.3.2 定义Action
<actions mep="OneWay"> <action class="org.jboss.soa.esb.actions.MessageFilter" name="MessageFilter"> <property name="ruleSet" value="FilterRules.drl" /> <property name="ruleReload" value="true" /> <property name="destinations"> <route-to destination-name="DemoDestination" service-category="Test" service-name="NextService" /> </property> <property name="object-paths"> <object-path esb="body.'org.jboss.soa.esb.message.defaultEntry'" /> </property> </action> </actions>
配置说明:这里定义了一个消息过滤器,过滤规则在文件FilterRules.drl中进行定义。当满足规则的消息到来时,将会通过过滤器而被传送到NextService服务进行处理,否则,消息将会被直接过滤掉而不做任何处理。
3.3.3 定义过滤规则
在src目录下创建FilterRules.drl文件,并定义如下规则脚本:
#created on: Oct 30, 2006 package com.jboss.soa.esb.routing.cbr #list any import classes here. import org.jboss.soa.esb.message.Message; import org.jboss.soa.esb.message.format.MessageType; import com.thu.afa.esb.jbossesb.client.bean.Order; #declare any global variables here global java.util.List destinations; rule "Pricing Rule - over threshold" when m : Message () order : Order( quantity >= 10) then System.out.println(order.getQuantity() + " Makes the threshold"); #we can modify the object order.setDiscount(10); #demonstrate that we can add a new object to the message m.getBody().add("DiscountObject","10%"); destinations.add("DemoDestination"); end rule "Pricing Rule - under threshold" #adding this for debug output, the important part is to *NOT* set a destination when m : Message () order : Order( quantity < 10) then System.out.println(order.getQuantity() + " Does not make the threshold"); order.setDiscount(0); end
3.3.4 定义Order类
package com.thu.afa.esb.jbossesb.client.bean; import java.io.Serializable; import java.math.BigDecimal; public class Order implements Serializable { private static final long serialVersionUID = 1L; private int quantity; private BigDecimal unitPrice; private double discount; private BigDecimal totalPrice; // setters and getters }
3.5 定义第二个Service
<service category="Test" description="Demo Destination" name="NextService"> <listeners> <jms-listener busidref="nextEsbChanel" name="esbChanel" /> </listeners> <actions mep="OneWay"> <action class="org.jboss.soa.esb.actions.Notifier" name="notificationAction"> <property name="okMethod" value="notifyOK" /> <property name="destinations"> <NotificationList type="OK"> <target class="NotifyConsole" /> </NotificationList> </property> </action> </actions> </service>
3.6 配置部署文件
部署依赖文件deployment.xml内容如下:
<jbossesb-deployment> <depends>jboss.esb:deployment=jbrules.esb</depends> <depends>jboss.esb.quickstart.destination:service=Queue,name=messagefilterRequestEsb </depends> <depends>jboss.esb.quickstart.destination:service=Queue,name=messagefilterRequestGw </depends> <depends>jboss.esb.quickstart.destination:service=Queue,name=messagefilterNextRequestEsb </depends> </jbossesb-deployment>
3.7 部署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.MessageFilterClient.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.Serializable; 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; import com.thu.afa.esb.jbossesb.client.bean.Order; /** * <p>Class Name: MessageFilterClient</p> * <p>Description: </p> * @author Afa * @date 2010-9-14 * @version 1.0 */ public class MessageFilterClient { 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/messagefilterRequestGw"); 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(Serializable object) throws Exception { QueueSender sender = session.createSender(queue); ObjectMessage message = session.createObjectMessage(object); sender.send(message); sender.close(); } /** * <p>Title: </p> * <p>Method Name: main</p> * <p>Description: </p> * @author: Afa * @date: 2010-9-14 * @param args */ public static void main(String[] args) throws Exception { Order order = new Order(); order.setQuantity(5); MessageFilterClient client = new MessageFilterClient(); client.setupConnection(); client.sendAMessage(order); client.stop(); } }
运行客户端程序即可看到以下输出结果,下图是分别将quantity属性设置为15和5的输出结果。
4.3 注意事项
在客户端和服务端的classpath中都必须能找到Order类,其中,类名和包路径也都必须完全一致,而且需要与规则文件中的导入信息保持一致,否则,将会出现如下异常:
No ClassLoaders found for: com.thu.afa.esb.jbossesb.client.bean.Order
-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Mar 29th, 2011
-----------------------------------------------------