Burlap和Hessian同属于codehaus的RPC调用框架,但是Burlap已经几年不更新,所以Spring在4.0里已经将Burlap的支持置为Deprecated,所以在选择RPC框架时,不应该考虑Burlap了。
这篇文章还是记录下Burlap的用法吧,主要是复制粘贴了Hessian与Spring集成一文,【RPC框架Hessian四】Hessian与Spring集成
Burlap和Hessian的共同点:
- 基于HTTP协议
- 简单易上手,不需要定义复杂的接口描述文件,如WSDL,IDL
- 在RPC框架中,性能优越
- Spring对它们一致的集成,使得在Burlap和Hessian之间切换很容易,只要修改Spring的暴露接口(Exporter)和远程服务代理工厂类,就可以实现切换
Burlap和Hessian的不同点:
- Burlap基于XML,Hessian基于二进制,这使得Burlap传输数据的可读性优于Hessian
- Hessian的性能稍微优于Burlap
在【RPC框架Hessian二】Hessian 对象序列化和反序列化一文中介绍了基于Hessian的RPC服务的实现步骤,在那里使用Hessian提供的API完成基于Hessian的RPC服务开发和客户端调用,本文使用Spring对Hessian的集成来实现Hessian的RPC调用。
定义模型、接口和服务器端代码
|---Model
|--ComplexModel
|--Person
|--Point
|---Interface
|--IComplexModelService
|---Service Implementation
|--ComplexModelService
定义pom.xml
添加对Spring、Hessian和Spring Remoting的支持
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>hessian.project</artifactId> <groupId>com.tom</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>learn.hessian.spring.integration</artifactId> <dependencies> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-remoting</artifactId> <version>2.0.8</version> </dependency> </dependencies> <build> <plugins> <!--jetty plugin to manage embedded jetty--> <!--No goal is specified--> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.7</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8668</port> <maxIdleTime>30000</maxIdleTime> </connector> </connectors> <webAppSourceDirectory>${project.basedir}/web </webAppSourceDirectory> <contextPath>/web</contextPath> </configuration> </plugin> </plugins> </build> </project>
定义web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>hessian</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-hessian-server.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/hessian/*</url-pattern> </servlet-mapping> </web-app>
定义applicationContext-hessian-server.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 服务暴露 --> <!--BurlapServiceExporter代替HessianServiceExporter--> <bean name="/complexModelService" class="org.springframework.remoting.caucho.BurlapServiceExporter"> <!-- 服务类 --> <property name="service"> <bean id="complexModelService" class="com.tom.hessian.server.ComplexModelService"/> </property> <property name="serviceInterface"> <value> com.tom.hessian.common.IComplexModelService </value> </property> </bean> </beans>
定义applicationContext-hessian-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--远程对象代理工厂-->
<!--BurlapProxyFactoryBean代替HessianProxyFactoryBean-->
<bean name="complexModelService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
<!--BurlapProxyFactoryBean没有connectTimeout属性-->
<property name="connectTimeout" value="60000"/>
<property name="serviceUrl" value="http://localhost:8668/web/hessian/complexModelService"/>
<property name="serviceInterface" value="com.tom.hessian.common.IComplexModelService"/>
</bean>
</beans>
定义客户端测试代码
package com.tom.hessian.client; import com.tom.hessian.common.ComplexModel; import com.tom.hessian.common.IComplexModelService; import com.tom.hessian.common.Person; import com.tom.hessian.common.Point; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.ArrayList; import java.util.Date; import java.util.List; public class ComplextModelServiceSpringTest { public static void main(String[] args) throws Exception { ComplexModel<Point> model = new ComplexModel<Point>(); model.setId(1); Person person = new Person(); person.setName("Tom"); person.setAge(86); person.setBirthDay(new Date()); person.setSensitiveInformation("This should be private over the wire"); model.setPerson(person); List<Point> points = new ArrayList<Point>(); Point point = new Point(); point.setX(3); point.setY(4); points.add(point); point = new Point(); point.setX(100); point.setY(100); points.add(point); model.setPoints(points); //远程方法调用 ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext-hessian-client.xml"); IComplexModelService service = cxt.getBean("complexModelService", IComplexModelService.class); service.save(model); model = service.read(model.getId()); List<Point> points1 = model.getPoints(); for(Point elem : points1) { System.out.println(elem.getX() + "\t" + elem.getY()); } } }
运行
启动Jetty Server,然后运行上面的客户端代码,可以正常执行