Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
客户端/服务端
客户端具备以下几点:
- java客户端包含Hessian.jar的包。
- 具有和服务器端结构一样的接口。
- 利用HessianProxyFactory调用远程接口。
- 使用spring方式需要配置HessianProxyFactoryBean
JAVA服务器端具备以下几点:
- 包含Hessian的jar包。
- 设计一个接口,用来给客户端调用。
- 实现该接口的功能。
- 配置web.xml,配好相应的servlet。
- 对象必须实现Serializable 接口。
- 对于spring方式DispatcherServlet拦截url,HessianServiceExporter提供Bean服务
案例
服务端
实体类
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7136225729690191616L;
private String name ;
private String password ;
public Person(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Hessian服务的接口
/**
* Hessian服务的接口
*/
public interface MyHessian {
public void say(String str);
public String hello();
public Person getPerson();
}
Hessian服务的实现
/**
* Hessian服务的实现
*/
public class MyHessianImpl implements MyHessian {
private String str = "Hello Hessian";
@Override
public void say(String str) {
this.str = str;
System.out.println("setting say sucess:" + str);
}
@Override
public String hello() {
return str;
}
@Override
public Person getPerson() {
return new Person("James","123");
}
}
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>HessianService</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置hessian -->
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>cn.aric.hessian.service.impl.MyHessianImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
<!-- 配置spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- hessian -->
<servlet>
<servlet-name>hessianServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-context.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>
</web-app>
配置文件(hessian-context.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="myHessianImpl" class="cn.aric.hessian.service.impl.MyHessianImpl" />
<!-- 使用HessianServiceExporter将普通bean导出成Hessian服务 -->
<bean name="serviceImpl" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="myHessianImpl" />
<!-- Hessian服务接口 -->
<property name="serviceInterface" value="cn.aric.hessian.service.MyHessian" />
</bean>
</beans>
将dto以及服务接口对应的包打jar包,提供客户端使用。
http://127.0.0.1:8080/HessianService/hessian 发布hessian服务
客户端 ###
没与spring整合
public class MyHessianClient {
public static void main(String[] args) throws Exception {
String url = "http://127.0.0.1:8080/HessianService/hessian";
HessianProxyFactory factory = new HessianProxyFactory();
MyHessian helloService = (MyHessian) factory.create(MyHessian.class, url);
System.out.println("hello:"+mh.hello());
System.out.println("name:"+mh.getPerson().getName());
System.out.println("password:"+mh.getPerson().getPassword());
mh.say("shuai");
System.out.println("say:"+mh.hello());
}
}
与spring整合
配置文件(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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="hessianClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" >
<value>http://127.0.0.1:8080/HessianService/hessian</value>
</property>
<property name="serviceInterface">
<value>cn.aric.hessian.service.MyHessian</value>
</property>
</bean>
</beans>
测试代码:
public class MyHessianClient {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
MyHessian mh = (MyHessian) context.getBean("hessianClient");
System.out.println("hello:"+mh.hello());
System.out.println("name:"+mh.getPerson().getName());
System.out.println("password:"+mh.getPerson().getPassword());
mh.say("shuai");
System.out.println("say:"+mh.hello());
}
}