Java学习之路-Burlap学习

今天我们来学一下Burlap。

Burlap是一种基于XML远程调用技术,但与其他基于XML的远程技术(例如SOAP或者XML-RPC)不同,Burlap的消息结构尽可能的简单,不需要额外的外部定义语言(例如WSDL或IDL)。

Burlap和Hessian很大程度上,它们是一样的,唯一的区别在于Hessian的消息是二进制的,而Burlap的消息是XML。(Burlap和Hessian代码实现上也很相似)

接下来我们看一下代码的实现:

 

一、首先我们先创建一个实体类,这里不需要实现Serializable接口

package entity;

public class Food {

    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;

    }

}

二、我们来定义一个接口

package service;



import java.util.List;



import entity.Food;



public interface FoodService {



    List<Food> getFoodList();



}

 

三、定义一个类,实现步骤二中的接口,并继承BurlapServlet类(这里需要用到Burlap的jar文件,可以到这里下载http://www.findjar.com/jar/burlap/jars/burlap-2.1.7.jar.html)

package service.impl;

import java.util.ArrayList;

import java.util.List;

import service.FoodService;

import com.caucho.burlap.server.BurlapServlet;

import entity.Food;

public class FoodServiceImpl extends BurlapServlet implements FoodService {

    public List<Food> getFoodList() {

        List<Food> list=new ArrayList<Food>();

        Food f1=new Food();    

        f1.setName("酸菜鱼");

        f1.setPrice(25);

        Food f2=new Food();

        f2.setName("糖醋鱼");

        f2.setPrice(23);

        list.add(f1);

        list.add(f2);

        return list;

    }

}

 

四、现在我们可以在WEB-INF下的web.xml中配置一个servlet(Hessian也可以这样配置servlet)

<servlet>

    <servlet-name>food</servlet-name>

    <servlet-class>service.impl.FoodServiceImpl</servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>food</servlet-name>

    <url-pattern>/food</url-pattern>

</servlet-mapping>

 

五、我们来写一下测试代码,看一下结果

package test;

import java.util.List;

import service.FoodService;

import com.caucho.burlap.client.BurlapProxyFactory;

import entity.Food;

public class Test {

    public static void main(String[] args) {

        String url="http://127.0.0.1:8080/test/food";

        BurlapProxyFactory factory=new BurlapProxyFactory();

        try {

            FoodService foodSevice=(FoodService) factory.create(FoodService.class, url);

            List<Food> foodList = foodSevice.getFoodList();

            for (Food food : foodList) {

                System.out.println(food.getName()+":"+food.getPrice()+"元。");

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

 

控制台显示的结果为:

=========控制台=========


酸菜鱼:25.0元。

糖醋鱼:23.0元。


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


接下来我们看一下Spring整合Burlap,这里和Spring整合Hessian基本差不多。

Spring整合Burlap

一、我们来定义一个接口

package service;

import java.util.List;

import entity.Food;

public interface FoodService {

    List<Food> getFoodList();

}

 

二、定义一个类,实现步骤二中的接口

package service.impl;

import java.util.ArrayList;

import java.util.List;

import service.FoodService;

import entity.Food;

public class FoodServiceImpl implements FoodService {

  public List<Food> getFoodList() {

    List<Food> list=new ArrayList<Food>();

    Food f1=new Food();

    f1.setName("酸菜鱼");

    f1.setPrice(25);

    Food f2=new Food();

    f2.setName("糖醋鱼");

    f2.setPrice(23);

    list.add(f1);

    list.add(f2);

    return list;

  }

}

 

三、我们可以在WEB-INF下的web.xml中配置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>

  <load-on-startup>1</load-on-startup>

</servlet>



<servlet-mapping>

  <servlet-name>springMvc</servlet-name>

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

</servlet-mapping>

 

四、在applicationContext.xml中配置需要导出服务的bean信息

<bean id="foodService" class="service.impl.FoodServiceImpl"></bean>
<bean id="FoodService"   class="org.springframework.remoting.caucho.BurlapServiceExporter"   p:serviceInterface="service.FoodService"   p:service-ref="foodService" />

 

五、在WEB-INF下新建springMvc-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"

  xmlns:p="http://www.springframework.org/schema/p"

  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">



  <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

    <property name="mappings">

      <props>

        <prop key="/foodService">FoodService</prop>

      </props>

    </property>

  </bean>

</beans>

 

六、在客户端程序applicationContext.xml中配置获取服务的bean信息

<bean id="getFoodService"

  class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"

  p:serviceInterface="service.FoodService"

  p:serviceUrl="http://127.0.0.1:8080/test/foodService"

/>

 

七、现在我们编写测代码

package test;

import java.util.List;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import entity.Food;

import service.FoodService;

public class Test {

  public static void main(String[] args) {

    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

    FoodService foodService=(FoodService) ctx.getBean("getFoodService");

    List<Food> foodList = foodService.getFoodList();

    for (Food food : foodList) {

      System.out.println(food.getName()+":"+food.getPrice()+"元。");

    }

  }

}

 

接下来我们把项目部署到Tomcat上面,并且启动服务。运行测试代码

======控制台=======


酸菜鱼:25.0元。

糖醋鱼:23.0元。


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

到这里我们已经学习了Spring整合Burlap。

你可能感兴趣的:(Java学习)