1.核心对象People、Person、Address、Boyfriend1、Boyfriend2。通过Hessian传输的对象都要实现java.io.Serializable接口。
2.接口PeopleService 接口实现PeopleServiceImpl
PeopleService.java
public interface PeopleService { public People getPeopleByName(String name); }
PeopleServiceImpl.java
public class PeopleServiceImpl implements PeopleService { private People people; public void setPeople(People people) { this.people = people; } public People getPeopleByName(String name) { return people; } }
3.核心配置文件
applicationContext-hessianrpc-hessian.xml
<bean id="peopleServiceRemote" name="/peopleServiceRemote" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="peopleService"></property> <property name="serviceInterface"> <value>com.mangocity.hessianrpc.service.PeopleService</value> </property> </bean>applicationContext-hessianrpc-services.xml
<bean id="peopleService" class="com.mangocity.hessianrpc.service.impl.PeopleServiceImpl"> <property name="people" ref="people"> </property> </bean> <bean id="people" class="com.mangocity.hessianrpc.pojo.People" > <property name="name"> <value>fenglin</value></property> <property name="boyfriend" ref="boyfriend"></property> <property name="boyfriend1" ref="boyfriend1"></property> <property name="boyfriends"> <list> <ref bean="boyfriend"/> <ref bean="boyfriend1"/> </list> </property> <property name="boyfriendMap"> <map> <entry> <key><value>fengluan</value></key> <ref bean="boyfriend"/> </entry> <entry> <key><value>fengluan1</value></key> <ref bean="boyfriend1"/> </entry> </map> </property> <property name="boyfriendSet"> <set> <ref bean="boyfriend"/> <ref bean="boyfriend1"/> </set> </property> </bean> <bean id="boyfriend" class="com.mangocity.hessianrpc.pojo.Boyfriend"> <property name="address"> <ref bean="address"/> </property> </bean> <bean id="boyfriend1" class="com.mangocity.hessianrpc.pojo.Boyfriend1"> <property name="address"> <ref bean="address"/> </property> </bean> <bean id="address" class="com.mangocity.hessianrpc.pojo.Address"> <property name="zipCode"><value>518000</value> </property> <property name="url"><value>深圳市芒果网有限公司</value> </property> </bean>web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/applicationContext-hessianrpc-beans.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <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:spring/applicationContext-hessianrpc-hessian.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>
4.hessian访问客户端
HessianRPCClient.java
public class HessianRPCClient { String url; /** * */ public HessianRPCClient(String url) { this.url = url; } public People getPeopleFromPeopleService(String name){ HessianProxyFactory factory = new HessianProxyFactory(); try { PeopleService proxy = (PeopleService)factory.create(PeopleService.class, url); return proxy.getPeopleByName(name); } catch (MalformedURLException e) { e.printStackTrace(); } return new People(); } /** * @param args */ public static void main(String[] args) { HessianRPCClient client = new HessianRPCClient("http://127.0.0.1:8088/hessianRPC/hessian/peopleServiceRemote"); People people = client.getPeopleFromPeopleService("people"); System.out.println(people.getName()); System.out.println(people.getBoyfriend().getAddress() == people.getBoyfriend1().getAddress()); System.out.println(people.getBoyfriend() == people.getBoyfriends().get(0)); System.out.println(people.getBoyfriendMap().get("fengluan") == people.getBoyfriendSet().iterator().next()); System.out.println(people.getBoyfriend() == people.getBoyfriendMap().get("fengluan")); } }
6.将项目发布到tomcat上,启动客户访问,测试结果:
Hessian-rpc支持在不同的JVM版本之间传递复杂对象,并且还支持引用参数传递。速度的话,在传输小对象比RMI还快,传输大对象则不如RMI。