Hessian使用教程

Hoking發表於2016-06-18

       Hessian是一個輕量級的remotingonhttp工具,使用簡單的方法提供了RMI的功能。相比Webservice,Hessian更簡單、快捷。採用的是二進位制RPC協議,因為採用的是二進位制協議,所以它很適合傳送二進位制資料。

      在進行基於Hessian的專案開發時,應當注意一下幾點:

Java伺服器端必須具備:

■包含Hessian的jar包

■設計一個介面,用來給客戶端呼叫

■實現該介面的功能

■配置web.xml,配置好相應的servlet

■由於使用二進位制RPC協議傳輸資料,物件必須進行序列化,實現Serializable介面

■對於複雜物件可以使用Map的方法傳遞

Java客戶端必須具備:

□Java客戶端包含Hessian的jar包

□具有和伺服器端結構一樣的介面,包括名稱空間都最好一樣

□利用HessianProxyFactory呼叫遠端介面


Hessian的簡單例子

Hessian服務端

第一步:建立Web Project(HessianServer),並將hessian-3.2.1.jar放入WEB-INF/lib資料夾下。

第二步:建立User類,注意:實現Serializable介面

package com.mahc.hessian.domain;

import java.io.Serializable;

public class User implements Serializable{

	private String name;
	private Integer age;

	public User() {
		super();
	}
	
	public User(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}

第三步:建立介面IBasicApi

package com.mahc.hessian;

import com.mahc.hessian.domain.User;

public interface IBasicApi {

	/* 設定使用者名稱 */
	public boolean setUserName(String name);
	
	/* 獲取問候 */
	public String sayHello();
	
	/* 獲取使用者資訊 */
	public User getUser();
	
}

第四步:建立實現IBasicApi的BasicService

package com.mahc.hessian;

import com.mahc.hessian.domain.User;

public class BasicService implements IBasicApi{

	private String name;
	
	@Override
	public boolean setUserName(String name) {
		this.name = name;
		return false;
	}

	@Override
	public String sayHello() {
		return "Hello "+ name+",Welcome to Hessian!";
	}

	@Override
	public User getUser() {
		return new User(name, 23);
	}
}

第五步:配置WEB-INF下的web.xml

<!-- Hessian在web.xml上的配置 -->
  <servlet>
  	<servlet-name>HessianServlet</servlet-name>
  	<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
  	<init-param>
  		<param-name>service-class</param-name>
  		<param-value>com.mahc.hessian.BasicService</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HessianServlet</servlet-name>
  	<url-pattern>/api/service</url-pattern>
  </servlet-mapping>

Hessian客戶端

第一步:建立Java Project(HessianClient),並將hessian-3.2.1.jar放入lib資料夾下

 

第二步:建立與服務端一致的com.mahc.hessian.domain.User和com.mahc.hessian.IBasicApi

 

第三步:建立客戶端測試

public class ClientTest {

	public static void main(String[] args) throws MalformedURLException {
		
		String url = "http://localhost:8080/HessianServer/api/service";
		
		HessianProxyFactory factory = new HessianProxyFactory();
		IBasicApi api = (IBasicApi) factory.create(IBasicApi.class, url);
		api.setUserName("mahc");
		System.out.println(api.sayHello());
		System.out.println(api.getUser().getName());
		System.out.println(api.getUser().getAge());
		
	}
}


第四步:部署HessianServer,啟動Tomcat,執行ClientTest。檢視執行結果。


Hessian與Spring整合的例子

需要新增如下jar包: 下載地址:http://download.csdn.net/download/ma_hoking/8438559

/HessianServer/WebRoot/WEB-INF/lib/aopalliance-1.0.jar

/HessianServer/WebRoot/WEB-INF/lib/commons-logging-1.1.3.jar

/HessianServer/WebRoot/WEB-INF/lib/log4j-1.2.9.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-aop-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-asm-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-beans-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-context-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-core-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-expression-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-jdbc-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-jms-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-web-3.2.0.M1.jar

/HessianServer/WebRoot/WEB-INF/lib/spring-webmvc-3.2.0.M1.jar

第一步:新增所需要的jar包,編輯web.xml

<!-- Hessian與Spring整合時在web.xml上的配置 -->
  <servlet>
  	<servlet-name>HessionRemoteServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:hessianServer-servlet.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HessionRemoteServlet</servlet-name>
  	<url-pattern>/remote/api/*</url-pattern>
  </servlet-mapping>

第二步:在src/建立hessianServer-servlet.xml

       初始化DispatcherServlet 時,該框架在 web 應用程式WEB-INF目錄中尋找一個名為[servlet-名稱]-servlet.xml的檔案,並在那裡定義相關的Beans,重寫在全域性中定義的任何Beans,像下面的web.xml中的程式碼,對應的是dispatcher-servlet.xml;當然也可以使用<init-param>元素,手動指定配置檔案的路徑;

【自定義配置檔案位置】

 	<init-param>
        <description>載入/WEB-INF/spring-mvc/目錄下的所有XML作為Spring MVC的配置檔案</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc/*.xml</param-value>
    </init-param>

<?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="basicService" class="com.mahc.hessian.BasicService" />
	
	<bean name="/basicService"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="basicService" />
		<property name="serviceInterface" value="com.mahc.hessian.IBasicApi" />
	</bean>
</beans>

第三步:在HessianClient中ClientTest的url修改為:

http://localhost:8080/HessianServer/remote/api/basicService

public static void main(String[] args) throws MalformedURLException {
		
		String url = "http://localhost:8080/HessianServer/api/service";
		url = "http://localhost:8080/HessianServer/remote/api/basicService";
		
		HessianProxyFactory factory = new HessianProxyFactory();
		IBasicApi api = (IBasicApi) factory.create(IBasicApi.class, url);
		api.setUserName("mahc");
		System.out.println(api.sayHello());
		System.out.println(api.getUser().getName());
		System.out.println(api.getUser().getAge());
		
	}

Hessian客戶端也是用Spring的配置

第一步:在src下建立hessianClent.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="basicService"
		class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost:8080/HessianServer/remote/api/basicService"></property>
		<property name="serviceInterface" value="com.mahc.hessian.IBasicApi"></property>
	</bean>
</beans>

第二步:修改HessianClient中的ClientTest

public class ClientTest {

	public static void main(String[] args) throws MalformedURLException {
		
		String url = "http://localhost:8080/HessianServer/api/service";
		url = "http://localhost:8080/HessianServer/remote/api/basicService";
		
//		HessianProxyFactory factory = new HessianProxyFactory();
//		IBasicApi api = (IBasicApi) factory.create(IBasicApi.class, url);
		
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"hessianClient.xml");
		IBasicApi api = (IBasicApi) context.getBean("basicService");
		api.setUserName("mahc");
		System.out.println(api.sayHello());
		System.out.println(api.getUser().getName());
		System.out.println(api.getUser().getAge());
		
	}
}






相關文章