Hessian的使用
程序员文章站
2022-05-28 16:26:21
...
Hessian是一种高效简洁的远程调用框架,它采用的是二进制RPC协议(Binary),具有轻量、传输量小、平台无关的特点,特别适合于目前网络带宽比较小的手机网络应用项目。
Hessian类似于WebService,不过不使用SOAP协议,而是用Binary RPC协议,相比webservice而言更简单、快捷。
它把协议报文封装到http封包中,通过HTTP信道传输。Hessian是通过servlet提供远程服务,完全使用动态代理来实现的,推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。
下面是一个简单的Hessian服务搭建的配置详情:
前提说明:
项目A:hessian-server 服务项目
项目B:hessian-client 客户端项目
- 注意:若传输信息为实体对象,则实体类对象必须实现Serializable接口
hessian-server项目操作:
1、添加hessian-4.0.37.jar
2、创建业务接口:
public interface HelloService {
public String helloWorld(String message);
public String byteToString(byte[] data);
}
3、实现接口方法:
public class HelloServiceImpl implements HelloService {
public String helloWorld(String message) {
return "hello," + message;
}
public String byteToString(byte[] data) {
for(byte b : data) {
System.out.println(b + " ");
}
try {
return new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
}
4、web.xml中通过Spring进行请求的配置:
<servlet>
<servlet-name>hessianServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/hessian-server.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessianServlet</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
5、hessian-server.xml 配置:
<!--接口实现类型-->
<bean id="helloServiceImpl" class="com.slient.hessian.service.impl.HelloServiceImpl" />
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
<bean name="/service" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloServiceImpl" />
<!-- Hessian服务的接口 -->
<property name="serviceInterface" value="com.slient.hessian.service.HelloService" />
</bean>
6、将接口输出为.jar文件,并拷贝至客户端项目
hessian-client项目操作:
- 添加hessian-4.0.37.jar
- 添加 接口文件.jar
1、hessian-client.xml(spring配置文件):
<bean id="hessianClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8090/hessian-server/hessian/service</value>
</property>
<property name="serviceInterface">
<value>com.slient.hessian.service.HelloService</value>
</property>
</bean>
2、调用测试代码:
public class HessianTest {
@Test
public void testHessianService() throws MalformedURLException {
String url = "http://localhost:8090/hessian-server/hessian";
System.out.println(url);
HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(HelloService.class, url);
System.out.println(helloService.helloWorld("tanjin1"));
}
@Test
public void testHessianSpringService() throws UnsupportedEncodingException {
ApplicationContext context = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/conf/hessian-client.xml");
HelloService service = (HelloService) context.getBean("hessianClient");
String str = "我爱北京*";
byte[] data = str.getBytes("UTF-8");
for(byte b : data) {
System.out.println(b + " ");
}
System.out.println(service.byteToString(data));
}
}