天天看點

Spring使用遠端服務之Hessian

Hessian像RMI一樣,使用二進制消息進行用戶端和服務端的互動,它的二進制消息可以移植到其他非Java的語言中包括PHP、Python、C++和C#。因為Hessian是基于HTTP的,是以HessianSeriviceExporter實作為一個Spring MVC控制器。

Spring使用遠端服務之Hessian

HessianSeriviceExporter是一個SpringMVC控制器,它可以接收Hessian請求,并将這些請求翻譯成對POJO的調用來,進而将POJO導出為一個Hessian服務

為了使用導出Hessian服務,我們需要執行兩個額外的配置步驟:

1、在web.xml中配置Spring的DispatcherServlet,并把我們的應用部署為Web應用;

2、在Spring的配置檔案中配置一個URL處理器,将Hessian服務的URL分發給對應的Hessian服務Bean。

下面我們距離說明

我的項目分層

Spring使用遠端服務之Hessian

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
	  <!-- 在此處配置剛剛寫的spring-hessian.xml的位置 -->  
	<context-param>  
	    <param-name>contextConfigLocation</param-name>  
	    <param-value>    
	        classpath:/spring-hessian.xml  
	    </param-value>  
	</context-param> 
	
	
	<servlet>  
    <!-- servlet-name保持與spring-hessian.xml中一緻 -->  
    <servlet-name>HelloServiceExporter</servlet-name>  
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>  
	</servlet>  
	<servlet-mapping>  
	    <servlet-name>HelloServiceExporter</servlet-name>  
	    <url-pattern>/HelloService</url-pattern>  
	</servlet-mapping>  

</web-app>
           

spring-hessian.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	 http://www.springframework.org/schema/tx
	 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	 http://www.springframework.org/schema/jee
	 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
	 http://www.springframework.org/schema/aop
	 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	 http://www.springframework.org/schema/context
	 http://www.springframework.org/schema/context/spring-context-3.0.xsd
	 http://activemq.apache.org/schema/core
	 http://activemq.apache.org/schema/core/activemq-core.xsd"
	default-lazy-init="true"> 
  	
  	<context:annotation-config />
  	<!-- 元件掃描,使用annotation 自動注冊bean,并檢查@Required,@Autowired的屬性已被注入 -->
	<context:component-scan base-package="hessian" />

	<!-- 自動裝配 -->
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	
	
    <!-- Name保持與web.xml中的一緻,web.xml下文中描述 -->  
    <bean name="HelloServiceExporter"  
        class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <!-- service的ref與HelloServiceImpl中@Service中配置的一緻 -->  
        <property name="service" ref="helloService" />  
        <!-- 接口的路徑 -->  
        <property name="serviceInterface"  
            value="hessian.HelloService" />  
    </bean>  
</beans>  
           

java類

package hessian;

public interface HelloService {

	void sayHello(String name);

}
           
package hessian;

import org.springframework.stereotype.Service;

@Service("helloService")  
public class HelloServiceImpl implements HelloService {  
  
    /* (non-Javadoc) 
     * @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String) 
     */  
    @Override  
    public void sayHello(String name) {  
        System.out.println("Hello " + name + "!");  
    }  
  
} 
           

用戶端的調用

首先将Service的打成jar包加入到用戶端程式中

測試代碼

package hessionclient;

import hessian.HelloService;

import com.caucho.hessian.client.HessianProxyFactory;

public class ArticleManager {
	public static void main(String[] args) {  
        try {  
            String url = "http://localhost:8080/SpringTest/HelloService";  
            HessianProxyFactory factory = new HessianProxyFactory();  
            HelloService helloService = (HelloService) factory.create(  
                    HelloService.class, url);  
            helloService.sayHello("張三");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}
           

用戶端調用圖

Spring使用遠端服務之Hessian

參考:

《Spring實戰(第3版)》

PS:代碼主體來源于網絡