hessian簡單原理及實現例子–學習

來源:互聯網
上載者:User
 Hessian是一個輕量級的remoting on http工具,使用簡單的方法提供了RMI(Remote Method Invocation,遠程方法調用)的功能。採用的是二進位RPC(Remote Procedure Call Protocol,遠端程序呼叫協議)協議,因為採用的是二進位協議,所以它很適合於發送位元據。

  在進行基於Hessian的項目開發時,應當注意以下幾點:

  ▲JAVA伺服器端必須具備以下幾點:

  ·包含Hessian的jar包。

  ·設計一個介面,用來給用戶端調用。

  ·實現該介面的功能。

  ·配置web.xml,配好相應的servlet。

  ·對象必須實現Serializable 介面。

  ·對於複雜對像可以使用Map的方法傳遞。

  ▲用戶端必須具備以下幾點:

  ·java用戶端包含Hessian.jar的包。

  ·具有和伺服器端結構一樣的介面。

·利用HessianProxyFactory調用遠程介面。

 

 

下面是一個hessian的簡單例子

 

情境1:服務端 用戶端都不使用spring

 

Java伺服器端:

環境:j2sdk1.4.2、Tomcat6.0

依賴的包:hessian-3.1.6.jar

 

建立一個名為HessianServer的web project。將hessian-3.2.0.jar放入WEB-INF/lib檔案夾中。

建立介面

package com.mooing.hessian;

 

public interface DemoApi {

       public void setName(String name);

       public String sayHello();

       public User getUser();

}

實現介面

package com.mooing.hessian;

 

public class DemoService  implements DemoApi{

       private String name;

 

       @Override

       public void setName(String name) {

              this.name=name;

       }

 

       @Override

       public String sayHello() {

              System.out.println("hello,world!");

              return "hello:"+name;

       }

 

       @Override

       public User getUser() {

              return new User("ming","m123");

       }

}

 

建立User,注意實現序列化

package com.mooing.hessian;

 

import java.io.Serializable;

 

public class User implements Serializable {

 

       private static final long serialVersionUID = 1L;

 

       private String name = "kitty";

       private String psw = "nopww";

 

       public User() {

       }

 

       public User(String name, String psw) {

              super();

              this.name = name;

              this.psw = psw;

       }

 

       public String getName() {

              return name;

       }

 

       public void setName(String name) {

              this.name = name;

       }

 

       public String getPsw() {

              return psw;

       }

 

       public void setPsw(String psw) {

              this.psw = psw;

       }

}

 

配置web.xml

<servlet>

              <servlet-name>hessian</servlet-name>

              <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

              <init-param>

                     <param-name>service-class</param-name>

                     <param-value>com.mooing.hessian.DemoService</param-value>

              </init-param>

       </servlet>

 

       <servlet-mapping>

              <servlet-name>hessian</servlet-name>

              <url-pattern>/helloworld</url-pattern>

       </servlet-mapping>

 

用戶端代碼

 

注意如果建立用戶端工程,需引入上述jar,還需要服務端匯出DemoService和User兩個類的jar,放入lib。或建立兩個一摸一樣的類。

package com.mooing.hessian;

 

import java.net.MalformedURLException;

 

import com.caucho.hessian.client.HessianProxyFactory;

 

public class HessianClient {

       public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

              String url = "http://localhost:8080/jms1/helloworld";

              HessianProxyFactory pf = new HessianProxyFactory();

              DemoApi da = (DemoApi) pf.create(url);

              da.setName("test");

              System.out.println(da.sayHello());

              System.out.println(da.getUser());

       }

}

 

tomcat啟動服務端,運行用戶端,結果如下

hello:test

ming

m123

 

情境2:服務端 用戶端都使用spring(在上述基礎上)

依賴的包:

1.  Hessian包:hessian-3.1.6.jar

2.  spring-framework-2.0.2包:

a)   aopalliance.jar

b)   commons-logging.jar

c)   log4j-1.2.14.jar

d)   spring.jar

e)   spring-aop.jar

f)   spring-beans.jar

g)   spring-context.jar

h)   spring-core.jar

i)   spring-jdbc.jar

j)   spring-jms.jar

k)   spring-web.jar

l)   spring-webmvc.jar

 

配置web.xml,web.xml中增加:

<servlet>

              <servlet-name>remote</servlet-name>

              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

              <init-param>

                     <param-name>namespace</param-name>

                     <param-value>classes/remote-servlet</param-value>

              </init-param>

       </servlet>

 

       <servlet-mapping>

              <servlet-name>remote</servlet-name>

              <url-pattern>/remote/*</url-pattern>

       </servlet-mapping>

以下是remote-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.5.xsd">

       <!-- 注入內部實現介面 -->

       <bean id="demoService" class="com.mooing.hessian.DemoService" />

           <!-- 注入hessian發布介面  name為調用名稱-->

       <bean name="/helloDemo" class="org.springframework.remoting.caucho.HessianServiceExporter">

              <!-- 關聯結口實現 -->

              <property name="service" ref="demoService" />

              <!-- 關聯結口 -->

              <property name="serviceInterface" value="com.mooing.hessian.DemoApi" />

       </bean>

</beans>

1)     如果用戶端不使用spring,用戶端代碼如下

import java.net.MalformedURLException;

 

import com.caucho.hessian.client.HessianProxyFactory;

import com.mooing.hessian.DemoApi;

 

 

public class ClientSpring {

       public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

              String url = "http://localhost:8080/jms1/remote/helloDemo";

              HessianProxyFactory pf = new HessianProxyFactory();

              DemoApi da = (DemoApi) pf.create(url);

              da.setName("test");

              System.out.println(da.sayHello());

              System.out.println(da.getUser());

       }

}

 

2)    如果用戶端使用spring,需要匯入上述jar,在用戶端工程src目錄下,建立一個remote-hessian.xml,這個檔案可隨意命名,內容為:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

       <bean id="helloDemo" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

              <property name="serviceUrl">

                     <value>

                            http://localhost:8080/jms1/remote/helloDemo

                     </value>

              </property>

              <property name="serviceInterface">

                     <value>com.mooing.hessian.DemoApi</value>

              </property>

       </bean>

</beans>

 

用戶端代碼

 

import java.net.MalformedURLException;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.caucho.hessian.client.HessianProxyFactory;

import com.mooing.hessian.DemoApi;

 

public class ClientSpring {

 

       public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

              // String url = "http://localhost:8080/jms1/remote/helloDemo";

              // HessianProxyFactory pf = new HessianProxyFactory();

              // DemoApi da = (DemoApi) pf.create(url);

              // da.setName("test");

              // System.out.println(da.sayHello());

              // System.out.println(da.getUser());

              ApplicationContext context = new ClassPathXmlApplicationContext("remote-hessian.xml");

              DemoApi da = (DemoApi) context.getBean("helloDemo");

              da.setName("test spring");

              System.out.println(da.sayHello());

              System.out.println(da.getUser());

       }

}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.