由于日志没有图片功能,可以访问内部论坛:
host配置: 192.168.5.245 forum.xxt.cn
文章连接: http://forum.xxt.cn/posts/list/0/276.page
spring 整合hessian进行远程通讯
Hessian是使用HTTP将对象以中性的二进制消息进行传送,而不像RMI使用Java的序列化格式,由于二进制消息是中性的,因此不受限于某种程序语言所实现的客户端或服务器端,二进制数据在传输是所需要的带宽较小是其优点。
由于Hessian是通过HTTP传送的,所在使用时需要WEB框架,在使用spring 时,即就是需要使用DispatcherServlet,下面通过一个实例来展示hessian与spring整合使用。
一.首先介绍一下此实例的环境
1、tocmat: tomcat5.5
2、java: jdk1.5.0_12
3、eclipse: eclipse 3.2.0
4、spring: 2.55
二.服务器端程序
1.先看程序的目录结构:(如图1)
2.新建tomcat工程:
3.添加jar包,路径构建:
备注:spring的版本,请从下载的官网上下载。
spring.jar :spring-framework-2.5.5\dist
hessian.jar :spring-framework-2.5.5\lib\caucho
4.文件详单:
(1)cn.xxt.service.IuserService.java
package cn.xxt.service;
import java.util.List;
/**
* hessian 服务接口,取出用户姓名接口
* @author zhaoguoli
* @version V1.0 2010-09-30
*/
public interface IUserService {
public List getUsernames();
}
(2)cn.xxt.service.impl.UserService.java
package cn.xxt.service.impl;
import java.util.ArrayList;
import java.util.List;
import cn.xxt.service.IUserService;
/**
* hessian 服务实现,取出用户姓名列表
* @author zhaoguoli
* @version V1.0 2010-09-30
*/
public class UserService implements IUserService {
public List getUsernames() {
List<String> usernames = new ArrayList<String>();
usernames.add("zhaoguoli");
usernames.add("wanglei");
usernames.add("yinzhuangzhuang");
return usernames;
}
}
(3)WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(4)WEB-INF/applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="userservicetarget" class="cn.xxt.service.impl.UserService"/>
</beans>
(5)WEB-INF/ remoting-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean name="/Userservice-hessina" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service">
<ref bean="userservicetarget"/>
</property>
<property name="serviceInterface">
<value>cn.xxt.service.IUserService</value>
</property>
</bean>
</beans>
至此,服务器代码已经完毕。
三. 客户端代码
1. 先查看程序的目录结构:(如图2)
2. 新建java工程
3. 添加jar包,路径构建
4. 文件详单
(6)cn.xxt.service.IuserService.java
package cn.xxt.service;
import java.util.List;
/**
* hessian 服务接口,取出用户姓名接口
* @author zhaoguoli
* @version V1.0 2010-09-30
*/
public interface IUserService {
public List getUsernames();
}
(7)cn.xxt.client.HessinaClient.java
package cn.xxt.client;
import java.util.List;
import cn.xxt.service.IUserService;
/**
* hessian 测试 客户端
* @author zhaoguoli
* @version V1.0 2010-09-30
*/
public class HessinaClient {
private IUserService userservice;
public IUserService getUserservice() {
return userservice;
}
public void setUserservice(IUserService userservice) {
this.userservice = userservice;
}
public List getUsernames() {
return this.userservice.getUsernames();
}
}
(8)cn.xxt.client.HessianTest.java
package cn.xxt.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* hessian 客户端 调用
* @author zhaoguoli
* @version V1.0 2010-09-30
*/
public class HessianTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HessinaClient hessina = (HessinaClient) ctx.getBean("hessinaclient");
System.out.println("hessinaclient:" + hessina.getUsernames());
}
}
(9)src/applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>client.properties</value></property>
</bean>
<bean id="hessinaclient" class="cn.xxt.client.HessinaClient">
<property name="userservice">
<ref