Hessian是一款基于HTTP协议的RPC框架,采用的是二进制RPC协议,非常轻量级 ,且速度较快。
因为公司的项目远程调用采用的是hessian,故抽时间了解了下hessian,自己也写了一个应用实例,以便加深对hessian的理解。
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
在进行基于Hessian的项目开发时,应当注意以下几点:
JAVA服务器端必须具备以下几点:
·包含Hessian的jar包
·设计一个接口,用来给客户端调用
·实现该接口的功能
·配置web.xml,配好相应的servlet
·对象必须实现Serializable 接口
·对于复杂对象可以使用Map的方法传递
客户端必须具备以下几点:
·java客户端包含Hessian.jar的包。
·具有和服务器端结构一样的接口。包括命名空间都最好一样
·利用HessianProxyFactory调用远程接口。
以下是一个关于hessian应用的示例程序。
示例背景:
由于本人所在公司正在编码直播室方面的项目,故以下源码中用LiveRoom.java类为直播室的类。User.java类为用户类。直播室和用户之间为一对多的关系。即一个直播室里面有多个用户。本示例将实现对直播室的增删改查功能
我们先写服务端
服务端,即提供服务的一端(废话,呵呵),需要定义接口,并提供接口实现。
首先,定义两个类LiveRoom和User,并且两个类都要实现Serializable 接口
package org.study.hessian.domain; import java.io.Serializable;import java.util.ArrayList;import java.util.List; public class LiveRoom implements Serializable,Comparable{ private static final long serialVersionUID = -3219121209937741843L; private int roomId; private String roomName; public List users = new ArrayList (); public LiveRoom(int roomId, String roomName) { super(); this.roomId = roomId; this.roomName = roomName; } public LiveRoom() { super(); } public int getRoomId() { return roomId; } public void setRoomId(int roomId) { this.roomId = roomId; } public String getRoomName() { return roomName; } public void setRoomName(String roomName) { this.roomName = roomName; } public int compareTo(LiveRoom o) { if(this.getRoomId() > o.getRoomId()){ return 1; } else if(this.getRoomId() < o.getRoomId()){ return -1; } else { return 0; } }}
package org.study.hessian.domain; import java.io.Serializable;import java.util.Date; public class User implements Serializable{ private int userId; private String userName; private Date birth; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; }}
然后定义接口
package org.study.hessian.service; import java.util.List;import org.study.hessian.domain.LiveRoom; public interface LiveRoomService { public abstract void addRoom(LiveRoom room); public abstract void delRoom(int roomId); public abstract void updateRoom(LiveRoom room); public abstract LiveRoom getRoomById(int roomId); public abstract ListgetAllRoom();}
接口的实现
package org.study.hessian.service.impl; import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.study.hessian.domain.LiveRoom;import org.study.hessian.service.LiveRoomService; public class LiveRoomServiceImpl implements LiveRoomService { public MaproomMap = new HashMap (); public void addRoom(LiveRoom room) { roomMap.put(room.getRoomId(), room); } public void delRoom(int roomId) { roomMap.remove(roomId); } public List getAllRoom() { List list = new ArrayList (); Collection coll = roomMap.values(); Iterator it = coll.iterator(); while(it.hasNext()){ LiveRoom room = it.next(); list.add(room); } Collections.sort(list); return list; } public LiveRoom getRoomById(int roomId) { return roomMap.get(roomId); } public void updateRoom(LiveRoom room) { roomMap.remove(room.getRoomId()); roomMap.put(room.getRoomId(), room); } }
配置web.xml,配好相应的servlet
index.jsp liveRoom com.caucho.hessian.server.HessianServlet home-class org.study.hessian.service.impl.LiveRoomServiceImpl home-api org.study.hessian.service.LiveRoomService liveRoom /liveRoom
至此,服务端的代码就写完了。
接着,我们写客户端
注:客户端需要服务端定义的两个类和定义的接口(不需要接口的实现)
客户端的调用代码如下:
package org.study.hessian; import java.net.MalformedURLException;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.study.hessian.api.LiveRoomService;import org.study.hessian.domain.LiveRoom;import org.study.hessian.domain.User;import org.study.hessian.utils.PropertyFileUtil; import com.caucho.hessian.client.HessianProxyFactory; public class Test { public static void main(String[] args) throws MalformedURLException, ClassNotFoundException { // 远程调用地址 String url="http://localhost:8080/HessianServer/liveRoom"; System.out.println(url); HessianProxyFactory factory = new HessianProxyFactory(); LiveRoomService liveRoomService = (LiveRoomService) factory.create( LiveRoomService.class, url); for (int i = 1; i < 10; i++) { LiveRoom room = new LiveRoom(); room.setRoomId(i); room.setRoomName("liveRoom" + i); Listlist = new ArrayList (); for (int j = 1; j < 5; j++) { User user = new User(); user.setUserId(j); user.setUserName("xiongyuewen" + j); user.setBirth(new Date()); list.add(user); } room.users = list; liveRoomService.addRoom(room); } liveRoomService.delRoom(3); LiveRoom room = new LiveRoom(); room.setRoomId(4); room.setRoomName("wanglili"); room.users = null; liveRoomService.updateRoom(room); List listRooms = liveRoomService.getAllRoom(); if (listRooms == null) { throw new RuntimeException("listRooms is null"); } for (LiveRoom lr : listRooms) { System.out.println(lr.getRoomName()); List users = lr.users; if (users == null) { continue; } for (User user : users) { System.out.println(user.getUserName() + " " + user.getBirth()); } } }}