Java学习之路-Hessian学习


  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了。

 

你可能感兴趣的:(hessian)