1. 程式人生 > >Spring使用遠端服務之Hessian

Spring使用遠端服務之Hessian

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


HessianSeriviceExporter是一個SpringMVC控制器,它可以接收Hessian請求,並將這些請求翻譯成對POJO的呼叫來,從而將POJO匯出為一個Hessian服務

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

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

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

下面我們距離說明

我的專案分層


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實戰(第3版)》

PS:程式碼主體來源於網路