分两部分讲解。
1、Hessian服务端配置和接口定义实现。
2、Hessian客户端配置和接口调用。
注意Hessian客户端不像WebService一样是通过wsdl语言描述的,可以通过工具生成客户端代码。Hessian的客户端接口的实现方式一般有两种。1、是服务端提供接口和相关的实体POJO文档,根据提供的接口自己在客户端定义一样的接口和实体。2、在项目的maven工程中引入服务端的模块,这样就可以直接使用接口和对应的实体。
一、Hessian服务端配置
1. maven工程引入hessian 包
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
2. web.xml配置hessian的servlet
<!-- spring hessian 配置 开始 -->
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
<!-- spring hessian 配置 结束 -->
3. 配置remote-servlet.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" 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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<description>spring hessian集成配置</description>
<bean name="/helloWorldService" class="org.springframework.remoting.caucho.HessianServiceExporter" >
<property name="service" ref="helloWorldService" />
<!--上面这句话指向接口的实现类-->
<property name="serviceInterface" value="com.lancy.service.HelloWorldHessian" />
</bean>
<!--<bean id="helloWorldService" class="com.lancy.service.impl.HelloWorldHessianImpl"></bean>-->
</beans>
4. 定义接口HelloWorldHessian.java
package com.lancy.service;
public interface HelloWorldHessian{
public String sayHello(String name);
}
5. 接口实现HelloWorldHessianImpl.java
package com.lancy.service.impl;
import com.lancy.service.HelloWorldHessian;
@Service("helloWorldService")
public class HelloWorldHessianImpl implements HelloWorldHessian{
@Override
public String sayHello(String name) {
System.out.println("服务端方法被调用!");
return "Hello,"+name+"!";
}
}
二、Hessian客户端配置
1. 配置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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<description>hessian服务消费者配置</description>
<bean id="helloWorldHessian" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!--这里的serviceUrl地址必须和服务端设置的地址一致-->
<property name="serviceUrl" value="${url}/remote/helloWorldService" />
<property name="serviceInterface" value="com.lancy.service.HelloWorldHessian" />
<property name="overloadEnabled" value="true" />
</bean>
</beans>
${url}配置在配置文件public.properties
url=http://localhost:8080/testHessian;
package com.lancy.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lancy.service.HelloWorldHessian;
public class SpringClient {
public static void main(String[] args) {
ApplicationContext contex = new ClassPathXmlApplicationContext(
"hessian-client.xml");
// 获得客户端的Hessian代理工厂bean
HelloWorldHessian i = (HelloWorldHessian) contex.getBean("helloWorldClient");
System.out.println(i.sayHello("lancy"));
}
}
@Autowired
private HelloWorldHessian helloWorldHessian;
2. 第二种调用接口
package com.lancy.test;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.lancy.service.HelloWorldHessian;
public class NormalClient {
public static void main(String[] args) throws MalformedURLException {
//Spring Hessian代理Servelet
String url = "http://localhost:8080/testHessian/remote/helloWorldService";
HessianProxyFactory factory = new HessianProxyFactory();
HelloWorldHessian api = (HelloWorldHessian ) factory.create(HelloWorldHessian .class, url);
System.out.println(api.sayHello("lancy"));
}
}
可参考下面这两篇博客,原理都是差不多的
https://www.cnblogs.com/lilin0719/p/5257821.html
http://blog.csdn.net/chenweitang123/article/details/6334097