- Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。
相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
Hessian也是将网络传输的对象转换为二进制流通过Http进行传递,不过它是使用自己的序列化机制实现的编组与反编组,其支持的数据类型是有限制的,不支持复杂的对象。Hessian的优点是可以透过防火墙。Hessian是一个轻量级的remoting
on http工具,使用简单的方法提供了RMI的功能.
相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据.
下面我们通过项目代码去了解下Hessian结合Spring的实现。
IDE用的eclipse,JDK是1.7,tomcat是7.
一:新建JavaWeb项目
一直next,最后记得Generate web.xml deployment descriptor(新建项目后会带自带web.xml)
二: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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>TestH</display-name>
<!-- 启动spring配置监听 -->
<!--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Hessian-server.xml</param-value>
<!--
<param-value>classpath:applicationContext*.xml,/WEB-INF/Hessian-server.xml</param-value>
-->
</context-param>
<servlet>
<servlet-name>Hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>Hessian-server</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>
</web-app>
注意:
1. Hessian远程服务需要通过Servlet暴露
2.
namespace
Hessian-server
中Hessian-server跟/WEB-INF/Hessian-server.xml对应,该xml必须在/WEB-INF目录下,原因在DispatcherServlet继承的父类FrameworkServlet文档注释中可以看到,我截取部分
三:Hessian-server.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName">
<!-- 业务类 -->
<bean id="hessianService" class="com.yangwz.HessianServiceImpl"/>
<!-- 远程服务 -->
<bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="hessianService"/>
<property name="serviceInterface">
<value>com.yangwz.HessianService</value>
</property>
</bean>
</beans>
a. /hessianService是暴露给客户端调用的URI,客户端可以http://ip:port/contextroot(项目部署的路径)/(servlet-url-pattern)/hessianService调用,比如http://172.0.0.1:8082/TestH/hessian/hessianService
b.serviceInterface 业务类实现接口,这里必须是接口类。要不然会报org.springframework.beans.MethodInvocationException: Property ‘serviceInterface’ threw exception; nested exception is java.lang.IllegalArgumentException: ‘serviceInterface’ must be an interface 异常
附上接口类和实现类代码:
package com.yangwz;
public interface HessianService {
public String sayHello(String username);
public HessianPojo getHessianPojo(String username, String password);
}
[/code]
[code=java]
package com.yangwz;
public class HessianServiceImpl implements HessianService{
public String sayHello(String username){
return "Hello " + username;
}
public HessianPojo getHessianPojo(String username, String password) {
return new HessianPojo(username, password);
}
public void goTest(){//该方法是我用来测quartz的
System.out.println("hello");
}
}
到这里,服务端的实现已经完成了。接下来写下客户端调用,我是在项目写个测试类测
package com.yangwz;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
public class Test {
public static void main(String[] args) {
try {
String url = "http://localhost:8082/TestH/hessian/hessianService";
HessianProxyFactory factory = new HessianProxyFactory();
HessianService hessianServer;
hessianServer = (HessianService)factory.create(HessianService.class, url);
String ret = hessianServer.sayHello("Simon");
System.out.println(ret);
HessianPojo model = hessianServer.getHessianPojo("yangwz", "666666");
System.out.println("username: " + model.getUsername() + " ,password: "+model.getPassword());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
好了,将项目添加进tomcat,启动,然后运行TestH,执行结果:
在项目启动过程中遇到很多问题,但大部分都是少jar的情况。我把我用到的都截图出来(有几个是spring quartz才用到的)
到此,结束!!!
附上开头web.xml配置文件注释的几行的说明,那些是我测试spring quartz的使用的。
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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>TestH</display-name>
<!-- 启动spring配置监听 -->
<!-- 这个必须加上,要不然spring读取不到applicationContext*.xml的配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml,/WEB-INF/Hessian-server.xml</param-value>
</context-param>
<servlet>
<servlet-name>Hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>Hessian-server</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>
</web-app>
applicationContext_quarz.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 定时上报 -->
<bean id="testClientHessian" class="com.yangwz.HessianServiceImpl">
</bean>
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="testClientHessian" />
</property>
<property name="targetMethod">
<value>goTest</value>
</property>
</bean>
<!-- 配置触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="methodInvokingJobDetail" />
</property>
<property name="cronExpression">
<value>0/2 * * * * ?</value> <!--两秒一次 -->
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean>
</beans>
项目启动运行结果:
如文中有错或者不严谨的地方,望大侠们指出,谢谢!
本文借鉴https://blog.csdn.net/caihaijiang/article/details/5903253
https://blog.csdn.net/ixr_wang/article/details/6632355