JBoss ESB学习笔记13——第十二个ESB应用Spring AOP

续上篇介绍了第十一个ESB应用,本文介绍第十二个ESB应用——Spring AOP。

 

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

 

 

1 概述
本实例主要演示了Spring AOP在ESB中的应用。

 

2 新建ESB工程
新建ESB工程,在jbossesb-server-4.7中找到以下两个jar包:jbossesb-spring.jar和spring-2.5.6.SEC01.jar,并将这两个jar包复制到esbcontent/lib目录下,同时添加到项目的classpath。Jar包的具体位置不记得了,大家直接搜索就行。

 

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=springaopRequest" 	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=springaopResponse" 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=springaopEsb" 	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,并定义2个消息通道,内容如下:

<jms-provider connection-factory="ConnectionFactory" name="JBossMQ">
	<jms-bus busid="gwChanel">
		<jms-message-filter dest-name="queue/springaopRequest"
			dest-type="QUEUE" />
	</jms-bus>
	<jms-bus busid="esbChanel">
		<jms-message-filter dest-name="queue/springaopEsb"
			dest-type="QUEUE" />
	</jms-bus>
</jms-provider>

 

3.3 定义Service

<service category="SpringAop" description="Spring Aop"
name="SpringAopService">
	<listeners />
	<actions mep="OneWay">
		<action />
	</actions>
</service>

 

3.4 定义Listener

<listeners>
	<jms-listener busidref="gwChanel" is-gateway="true"
		name="gwListener" />
	<jms-listener busidref="esbChanel" name="esbListener" />
</listeners>

 

3.5 定义Java Bean

这是一个普通的JavaBean,为了验证Spring在ESB中的IOC而引入。

/***********************************************************************
 * <p>Project Name: springaop</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.SaySomething.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;

/**
 * <p>Class Name: SaySomething</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-16
 * @version 1.0
 */
public class SaySomething
{
	private String greeting;

	public String getGreeting()
	{
		return greeting;
	}

	public void setGreeting(String greeting)
	{
		this.greeting = greeting;
	}
}

 

3.6 定义Action

/***********************************************************************
 * <p>Project Name: springaop</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.MySpringEnabledAction.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.AbstractSpringAction;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;

/**
 * <p>Class Name: MySpringEnabledAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-16
 * @version 1.0
 */
public class MySpringEnabledAction extends AbstractSpringAction
{
	@SuppressWarnings({"deprecation" })
	public MySpringEnabledAction(final ConfigTree configTree)
	{
		this.configTree = configTree;
	}
	
	public Message sayHelloAopStyle(Message message) throws Exception
	{
		SaySomething saySomething = (SaySomething) getBeanFactory().getBean("saySomething");
		String helloString = (String) message.getBody().get();
		saySomething.setGreeting(helloString);
		message.getBody().add(saySomething.getGreeting());
		
		return message;
	}
}

注意:这里的action 扩展的是AbstractSpringAction类,而不是AbstractActionLifecycle。这里在对调用setGreeting方法进行了拦截,具体配置会在下面的配置中说明。

 

3.7 定义拦截器

/***********************************************************************
 * <p>Project Name: springaop</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.MyInterceptor.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.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/**
 * <p>Class Name: MyInterceptor</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-16
 * @version 1.0
 */
public class MyInterceptor implements AfterReturningAdvice
{
	/* (non-Javadoc)
	 * <p>Title: </p>
	 * <p>Method Name: afterReturning</p>
	 * <p>Description: </p>
	 * @author: Afa
	 * @date: 2010-9-16
	 * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
	 *
	 * @param returnValue
	 * @param method
	 * @param args
	 * @param target
	 * @throws Throwable
	 */
	public void afterReturning(Object returnValue, Method method, Object[] args,
			Object target) throws Throwable
	{
		SaySomething something = (SaySomething) target;
		System.out.println(something.getGreeting());
		
		System.out.println("*****GREETING VALUE PRE-INTERCEPTOR*****");
		System.out.println(something.getGreeting());
		
		something.setGreeting("I used AOP to change the greeting! JMS has got nothing on me!");
		System.out.println("*****GREETING VALUE POST-INTERCEPTOR*****");
		System.out.println(something.getGreeting());
	}

}

说明:这里有三条同样的打印出书语句:System.out.println(something.getGreeting()),其中,第一条和第二条输出的都是在拦截器处理之前的内容,第三条是输出修改内容后的结果。

 

3.8 Spring配置
在src目录下创建spring配置文件:spring-context.xml,该文件将定义由spring容器管理的类以及aop设置,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
	<bean id="saySomething"	class="com.thu.afa.esb.jbossesb.action.SaySomething" />
		<aop:config proxy-target-class="true">
			<aop:advisor pointcut="execution(* setGreeting*(..))"
				advice-ref="myInterceptor" />
		</aop:config>

	<bean id="myInterceptor"
class="com.thu.afa.esb.jbossesb.action.MyInterceptor" />
</beans>

说明:这里定义了一个拦截器,对SaySomething的set分发进行拦截。

 

3.9 配置Action

<actions mep="OneWay">
	<action name="sayHelloAopStyle"
	class="com.thu.afa.esb.jbossesb.action.MySpringEnabledAction"
	process="sayHelloAopStyle">
	<property name="exceptionMethod" value="exceptionHandler" />
	<property name="springContextXml" value="spring-context.xml" />
	</action>
</actions>

说明:

1) 在MySpringEnabledAction中的sayHelloAopStyle方法中,首先获取客户端发来的消息,然后调用SaySomething的set方法,但是由于定义了拦截器的关系,在执行了set方法之后还会执行拦截器的方法,而在拦截器中再一次调用了SaySomething的set方法,所以action中的set将不起作用。下面将会验证这一点。

2) 这里的springContextXml属性指明了spring配置文件的位置。

 

3.10 配置部署文件
部署依赖文件deployment.xml内容如下:

<jbossesb-deployment>
	<depends>jboss.esb:deployment=spring.esb</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=springaopRequest
	</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=springaopResponse
	</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=springaopEsb
	</depends>
</jbossesb-deployment>

 

3.11 部署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.SpringHelloWorldClient.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: SpringHelloWorldClient</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-16
 * @version 1.0
 */
public class SpringAopClient
{
	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/springaopRequest");
		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();
	}
	
	public static void main(String[] args) throws Exception
	{
		SpringAopClient client = new SpringAopClient();
		client.setupConnection();
		client.sendAMessage("Llu, miss you, afa - spring aop");
		client.stop();
	}
}

 

运行客户端程序即可看到以下输出结果:

JBoss ESB学习笔记13——第十二个ESB应用Spring AOP_第1张图片  

 

说明:上述几行打印输出结果都是在拦截器中执行的,其中最后一行是在拦截器中调用set方法后打印输出的结果。

 

-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Apr 3rd, 2011
-----------------------------------------------------

 

你可能感兴趣的:(spring,AOP,jboss,jms,SOA)