Java學習之路-Hessian學習

來源:互聯網
上載者:User

標籤:bar   book   copy   main   etl   影響   private   code   hand   

Hessian是基於HTTP的輕量級遠程服務解決方案,Hessian像Rmi一樣,使用二進位訊息進行用戶端和伺服器端互動。但與其他二進位遠程調用技術(例如Rmi)不同的是,它的二進位訊息可以移植其他非Java的語言中。 
  一、建立Hessian程式的4個步驟 
    1、定義一個遠程介面的介面。 
    2、定義一個實現該介面的類。 
    3、在web.xml中定義匯出Hessian服務需要的資訊。 
    4、編寫用戶端存取碼。 
  二、程式的具體實現 
    一、首先我們先建立Web項目,並建立一個實體類,這個類需要實現Serializable介面。

 1 package entity;  2 import java.io.Serializable;  3 public class Book implements Serializable {  4   private String name;  5   private double price;  6   public String getName() {  7     return name;  8   }  9   public void setName(String name) { 10     this.name = name; 11   } 12   public double getPrice() { 13     return price; 14   } 15   public void setPrice(double price) { 16     this.price = price; 17   } 18 } 

    二、建立一個介面 

1 package service; 2 import java.util.List; 3 import entity.Book; 4 public interface BookService { 5   List<Book> getList(); 6 } 

    三、建立一個類實現步驟二中的介面,並且這個類需要繼承HessianServlet類(這裡需要Hessian的jar檔案可以到這個網站下載http://hessian.caucho.com/#Java)

 1 package service.impl;  2 import java.util.ArrayList;  3 import java.util.List;  4 import service.BookService;  5 import com.caucho.hessian.server.HessianServlet;  6 import entity.Book;  7 public class BookServiceImpl extends HessianServlet implements BookService {  8     public List<Book> getList() {  9         List<Book> list=new ArrayList<Book>(); 10         Book b1=new Book(); 11         b1.setName("《資訊簡史》"); 12         b1.setPrice(56); 13         Book b2=new Book(); 14         b2.setName("《駭客與畫家》"); 15         b2.setPrice(48); 16         list.add(b1); 17         list.add(b2); 18         return list; 19     } 20 21 } 

    四、到WEB-INF下的web.xml中配置資訊

<servlet>     <servlet-name>book</servlet-name>     <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>     <init-param>         <param-name>home-api</param-name>         <param-value>service.BookService</param-value>     </init-param>     <init-param>         <param-name>home-class</param-name>         <param-value>service.impl.BookServiceImpl</param-value>     </init-param> </servlet> <servlet-mapping>     <servlet-name>book</servlet-name>     <url-pattern>/book</url-pattern> </servlet-mapping>                     

 


      配置好之後,部署項目到Tomcat伺服器,並啟動服務。

 
    五、編寫客戶存取碼 

 1 package test;  2 import java.util.List;  3 import service.BookService;  4 import com.caucho.hessian.client.HessianProxyFactory;  5 import entity.Book;  6 public class Test {  7   public static void main(String[] args) {  8     String url="http://127.0.0.1:8080/test/book";  9     HessianProxyFactory factory=new HessianProxyFactory(); 10     try { 11       BookService bookService=(BookService) factory.create(BookService.class, url); 12       List<Book> list = bookService.getList(); 13       for (Book book : list) { 14         System.out.println(book.getName()+",定價為:"+book.getPrice()+"元。"); 15       } 16     } catch (Exception e) { 17       e.printStackTrace(); 18     } 19   } 20 } 

 


      運行代碼,控制台顯示結果為 
      ===========控制台============ 

      《資訊簡史》,定價為:56.0元。 
      《駭客與畫家》,定價為:48.0元。 

      ============================= 

接下來我們來講一下Spring整合Hessian

Spring整合Hessian

  注意事項:

      Hassian 3.2.0採用了新的Hassian 2協議,而Spring2.5.6 只支援Hassian 1協議,所以spring 2.5.6所能支援的最大版本為Hassian 3.1.6,最好使用spring 2.5.6附帶的版本Hassian 3.1.3,而對Hassian 2的支援,需要 Spring 3.0。

  一、首先我們建立一個介面

1 package service;2 import java.util.List;3 import entity.Book;4 public interface BookService {5     List<Book> getList();6 }

  二、編寫一個類,只需實現步驟一中的介面

 1 package service.impl; 2 import java.util.ArrayList; 3 import java.util.List; 4 import service.BookService; 5 import entity.Book; 6 public class BookServiceImpl implements BookService { 7     public List<Book> getList() { 8         List<Book> list=new ArrayList<Book>();  9         Book b1=new Book(); 10         b1.setName("《資訊簡史》"); 11         b1.setPrice(56); 12         Book b2=new Book(); 13         b2.setName("《駭客與畫家》"); 14         b2.setPrice(48); 15         list.add(b1); 16         list.add(b2); 17         return list; 18     }19 }

  三、我們在WEB-INF下的web.xml中配置SpringMVC需要的資訊(spring整合hessian需要用到SpringMVC)

  <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>   <servlet>      <servlet-name>springmvc</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  </servlet>  <servlet-mapping>      <servlet-name>springmvc</servlet-name>      <url-pattern>/</url-pattern>  </servlet-mapping>

  四、在applicationContext.xml配置bean資訊

    <bean id="bookService" class="service.impl.BookServiceImpl"></bean>        <bean id="BookService"        class="org.springframework.remoting.caucho.HessianServiceExporter"        p:service-ref="bookService"        p:serviceInterface="service.BookService"    />

  五、現在WEB-INF目錄下建立springmvc-servlet.xml檔案,並配置一下。(可以把applicationContext.xml拷到目錄下改一下名字)

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        <property name="mappings">            <props>                <prop key="/book">BookService</prop>            </props>        </property>    </bean>

 

  六、接下我們應該在用戶端程式applicationContext.xml中配置擷取服務的bean資訊(我這裡是在同一個applicationContext.xml檔案中編寫,但不影響測試功能)

    <bean id="getBookService"        class="org.springframework.remoting.caucho.HessianProxyFactoryBean"        p:serviceUrl="http://127.0.0.1:8080/test/book"        p:serviceInterface="service.BookService"    />

  七、現在我們編寫一下測試代碼,在運行下面代碼之前需要把項目部署到Tomcat中,並運行Tomcat

 1 package test; 2 import java.util.List; 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import service.BookService; 6 import entity.Book; 7 public class Test { 8     public static void main(String[] args) { 9         ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");10         BookService bookService=(BookService) cxt.getBean("getBookService");11         List<Book> list = bookService.getList();12         for (Book book : list) {13             System.out.println(book.getName()+",定價為:"+book.getPrice()+"元。"); 14         }15     }16 17 }

 

      運行代碼,控制台顯示結果為 
      ===========控制台============ 

      《資訊簡史》,定價為:56.0元。 
      《駭客與畫家》,定價為:48.0元。 

      ============================= 

到這裡我們已經學習spring和怎麼整合hessian了。

 

Java學習之路-Hessian學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.