hessian实现(客户端服务端在同一个项目中)(转)

Hessian是基于HTTP的轻量级远程服务解决方案,Hessian像Rmi一样,使用二进制消息进行客户端和服务器端交互。但与其他二进制远程调用技术(例如Rmi)不同的是,它的二进制消息可以移植其他非Java的语言中。

通过Servlet绑定服务

一、创建Hessian程序的4个步骤

  1. 定义一个远程接口的接口。
  2. 定义一个实现该接口的类。
  3. 在web.xml中定义导出Hessian服务需要的信息。
  4. 编写客户端访问代码。

二、程序的具体实现
1.首先我们先创建Web项目,并新建一个实体类,这个类需要实现Serializable接口。

package entity; 
import java.io.Serializable; 
public class Book implements Serializable { 
  private String name; 
  private double price; 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public double getPrice() { 
    return price; 
  } 
  public void setPrice(double price) { 
    this.price = price; 
  } 
}

2.创建一个接口

package service; 
import java.util.List; 
import entity.Book; 
public interface BookService { 
  List<Book> getList(); 
}

3.创建一个类实现步骤二中的接口,并且这个类需要继承HessianServlet类(这里需要Hessian的jar文件可以到这个网站下载)

package service.impl; 
import java.util.ArrayList; 
import java.util.List; 
import service.BookService; 
import com.caucho.hessian.server.HessianServlet; 
import entity.Book; 
public class BookServiceImpl extends HessianServlet implements BookService { 
    public List<Book> getList() { 
        List<Book> list=new ArrayList<Book>(); 
        Book b1=new Book(); 
        b1.setName("《信息简史》"); 
        b1.setPrice(56); 
        Book b2=new Book(); 
        b2.setName("《黑客与画家》"); 
        b2.setPrice(48); 
        list.add(b1); 
        list.add(b2); 
        return list; 
    } 

}

4.到WEB-INF下的web.xml中配置信息

<servlet> 
    <!-- 配置 HessianServlet,Servlet的命名任意-->
    <servlet-name>book</servlet-name> 
    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> 
    <!--配置接口,param-name可配置的名称有:home-api、api-class、object-api(可不配置)-->
    <init-param> 
        <param-name>home-api</param-name> 
        <param-value>service.BookService</param-value> 
    </init-param> 
    <!-- 配置接口实现类,此处param-name可配置的名称有:home-class、service-class、object-class--> 
    <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>

5.配置好之后,部署项目到Tomcat服务器,并启动服务

6.编写客户访问代码

package test; 
import java.util.List; 
import service.BookService; 
import com.caucho.hessian.client.HessianProxyFactory; 
import entity.Book; 
public class Test { 
  public static void main(String[] args) { 
    String url="http://127.0.0.1:8080/test/book"; 
    HessianProxyFactory factory=new HessianProxyFactory(); 
    try { 
      BookService bookService=(BookService) factory.create(BookService.class, url); 
      List<Book> list = bookService.getList(); 
      for (Book book : list) { 
        System.out.println(book.getName()+",定价为:"+book.getPrice()+"元。"); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
}

7.运行客户端代码,控制台显示结果为
===========控制台============

《信息简史》,定价为:56.0元。
 《黑客与画家》,定价为:48.0元。

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

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;
import java.util.List;
import entity.Book;
public interface BookService {
    List<Book> getList();
}

2.编写一个类,只需实现步骤一中的接口

package service.impl;
import java.util.ArrayList;
import java.util.List;
import service.BookService;
import entity.Book;
public class BookServiceImpl implements BookService {
    public List<Book> getList() {
        List<Book> list=new ArrayList<Book>(); 
        Book b1=new Book(); 
        b1.setName("《信息简史》"); 
        b1.setPrice(56); 
        Book b2=new Book(); 
        b2.setName("《黑客与画家》"); 
        b2.setPrice(48); 
        list.add(b1); 
        list.add(b2); 
        return list; 
    }
}

3.我们在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>

4.在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"
    />

5.现在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>

6.接下我们应该在客户端程序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"
    />

7.现在我们编写一下测试代码,在运行下面代码之前需要把项目部署到Tomcat中,并运行Tomcat

package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.BookService;
import entity.Book;
public class Test {
    public static void main(String[] args) {
        ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService=(BookService) cxt.getBean("getBookService");
        List<Book> list = bookService.getList();
        for (Book book : list) {
            System.out.println(book.getName()+",定价为:"+book.getPrice()+"元。"); 
        }
    }

}

8.运行代码,控制台显示结果为
===========控制台============

 《信息简史》,定价为:56.0元。
 《黑客与画家》,定价为:48.0元。

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

转自:https://www.cnblogs.com/qujiajun/p/4070182.html

猜你喜欢

转载自www.cnblogs.com/linshuqin/p/10155005.html