---
title: "hessian应用"
author: "richie.lee"
date: "2010-05-19T13:54:26Z"
tags: []
categories: ["企业架构"]
slug: "hessian"
draft: false
lastmod: "2010-05-19T13:56:19Z"
id: 669804
url: "https://leegic.iteye.com/blog/669804"
---

目前hessian算一个比较热的应用。利用它可以很方便的实现远程服务。目前，开发的系统（项目）多处利用了hessian。一个简单的场景是，应用A通过hessian调用应用B的服务，然后B的服务请求公司对外统一的gateway进行请求转发，把请求分发到外部合作公司。hessian还可以利用多线程实现数据同步（或者说是数据推送）。下面，给出一个简单例子，说明hessian的用法。具体hessian适合用在哪些场景，则看看之前发表的 远程服务 相关的文章。

 

首先，简简单单的利用sevlet就OK。但需要准备好hessian.\*.\*.jar，spring.jar , common.logging和jdk等

 

（1）编写服务端

声明接口和方法：

 

```
public interface RemoteHessianService {

 String sayHello(String name);

｝
```

 

 

实现接口：

 

```
public class RemoteHessianServiceImpl implements RemoteHessianService {

public String sayHello(String name) {
        return "hello " + name

｝

}


```

 

配置web.xml

 

```
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app> 
<!--  hessian 服务 	-->
	<servlet>
		<servlet-name>remote</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
<!--  hessian 服务 	-->
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remote/*</url-pattern>
	</servlet-mapping>
</web-app> 
```

 在WEB－INFO下面新增remote-servlet.xml，配置其内容如下：

```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="remoteHessian"
		class="com.myCompany.crm.remote.RemoteHessianServiceImpl">
	</bean>
	<bean name="/remoteHessianService"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service">
			<ref bean="remoteHessian" />
		</property>
		<property name="serviceInterface">
			<value>
				com.myCompany.service.interfaces.RemoteHessianService
			</value>
		</property>
	</bean>
</beans>
```

 上面的配置主要是完成了servlet的名称与URL的映射配置，以及利用spring配置初始化bean而已。

 

 

到此，启动利用JBOSS启动web应用，服务端已经到此OK。

 

 

（2）编写客户端：

 

纯粹的利用JDK，而不依赖spring的客户端代码如下：

```
public class Say {

    /**
     * @param args
     */
    public static void main(String[] args) {  
com.caucho.hessian.client.HessianProxyFactory proxyFactory = new
         com.caucho.hessian.client.HessianProxyFactory();
        
         RemoteHessianService service = null;
         try {
         service = (RemoteHessianService) proxyFactory.create(RemoteHessianService.class,
         "http://localhost:jobss_port/remote/remoteHessianService");
         } catch (MalformedURLException e) {      
         e.printStackTrace();//no recommended
         }
        
         if (service != null) {
                     System.out.println(service.sayHello("hessian"));
         }
｝
```

 编译运行客户端吧，应该可以看到hell hessian的输出了吧。那就大功告成！

 

再利用 spring集成一下客户端吧。

 

增加一个beans-config.xml的配置在工程目录下：

```
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
 	<bean name = "hessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
     <property name="serviceUrl">  
        <value>http://localhost:jboss_port/remote/remoteHessianService</value>  
    </property>  
    <property name="serviceInterface">  
        <value>com.myCompany.service.interfaces.RemoteHessianService</value>  
    </property>  
    </bean>  

</beans>    

```

 接着,main函数改成如下：

 

```
public class Say {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        ApplicationContext context = new FileSystemXmlApplicationContext("beans-config.xml");
      RemoteHessianService service = (RemoteHessianService) context.getBean("hessianService");

        System.out.println(service.sayHello("richie"));
    }

}
```

 编译运行就OK拉。。。一个简单的例子就到此结束。

 

注意，访问端口一定要使用jboss或者tomcat之类的端口，不可以使用apache等端口。