Hessian简单实例
一、Hessian概述:
Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用了二进制协议,所以它很适合于发送二进制数据,Hessian主要作面向对象的消息通信。Hessian的初衷就是支持动态类型,格式紧凑,跨语言Hessian是使用自己的序列化机制实现的编组和反编组,其支持的数据类型是有限制的,不支持复杂的对象,可以穿透防火墙,在这里不得不说一下RMI:RMI是一组用户开发分布式应用程序的API。他使用的是java序列化机制实现调用及返回值的编组于反编组。它使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。他可以被看做是RPC的Java版本,因为传统的RPC并不能很好的应用于分布式对象系统。而Java RMI则支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。他也有它的缺点,他只能通过RMI协议来进行访问无法通过HTTP协议访问,无法穿透防火墙。
二、简单实例
服务器端:
jar包(hessian-4.0.37.jar)
设计一个接口,用来给客户端调用
实现该接口的动能
配置web.xml,配置相应的servlet
对象必须实现Serializable接口
对于复杂对象可以使用Map的方法传递
客户端:
jar包(hessian-4.0.37.jar)
具有和服务器端结构一样的接口和实体类。包括命名空间都最好一样。利用HessianProxyFactory调用远程接口
1.编写服务端代码
package com.hessian.simple.entity; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1L; private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.hessian.simple; import java.util.List; import java.util.Map; import com.hessian.simple.entity.User; public interface IHello { public String sayHello(String name); public String getUserList(List<User> users); public String getUserMap(Map<String, User> maps); }
package com.hessian.simple.impl; import java.util.List; import java.util.Map; import com.hessian.simple.IHello; import com.hessian.simple.entity.User; public class IHelloImpl implements IHello { public String sayHello(String name) { return "Hello," + name; } public String getUserList(List<User> users) { StringBuffer stringBuffer = new StringBuffer(); for (User user : users) { stringBuffer.append("["); stringBuffer.append(user.getUserName()); stringBuffer.append("--"); stringBuffer.append(user.getPassword()); stringBuffer.append("]"); } return stringBuffer.toString(); } public String getUserMap(Map<String, User> maps){ StringBuffer stringBuffer = new StringBuffer(); for(String key : maps.keySet()){ stringBuffer.append("["); stringBuffer.append(maps.get(key).getUserName()); stringBuffer.append("--"); stringBuffer.append(maps.get(key).getPassword()); stringBuffer.append("]"); } return stringBuffer.toString(); } }
配制web.xml
<!-- Hessian --> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>home-class</param-name> <param-value>com.hessian.simple.impl.IHelloImpl</param-value> </init-param> <init-param> <param-name>home-api</param-name> <param-value>com.hessian.simple.IHello</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping>
客户端测试类
package com.hessian.test; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.caucho.hessian.client.HessianProxyFactory; import com.hessian.simple.IHello; import com.hessian.simple.entity.User; public class ClientTest { public static String url = "http://127.0.0.1:8080/Hello"; public static void main(String[] args) { HessianProxyFactory factory = new HessianProxyFactory(); try { IHello iHello = (IHello) factory.create(IHello.class, url); System.out.println(iHello.sayHello("tzz")); User user1 = new User(); user1.setUserName("a1"); user1.setPassword("123456"); User user2 = new User(); user2.setUserName("a2"); user2.setPassword("123456"); List<User> users = new ArrayList<User>(); users.add(user1); users.add(user2); System.out.println(iHello.getUserList(users)); Map<String, User> maps = new HashMap<String, User>(); maps.put("user1", user1); maps.put("user2", user2); System.out.println(iHello.getUserMap(maps)); } catch (MalformedURLException e) { e.printStackTrace(); } } }