6.JMS(待实现)。
上一节介绍了HttpInvoker的使用,Burlap、RIM和Hessian的使用与其类似。下面以Burlap为例测试。
Burlap是一个轻量级的XML RPC协议,这使得他可以移植到任何可以解析XML的语言中。比起Hessian,它的可读性更强。和其他基于Xml的远程技术(如SOAP和XML-RPC)不同,它的消息结构尽可能简单,不需要额外的外部定义语言(如WSDL或IDL)。Hessian,Burlap是Caucho提供的两种基于Http的轻量级远程服务, Burlap现在已经集成到hessian.jar中,不作为一个单独的项目了。
代码示例如下:
1.添加依赖jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.9.RELEASE</version> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency>
2.接口、实现类
对象User.java
package cn.slimsmart.burlap.spring.demo; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1L; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "name="+name+",age="+age; } }
接口:UserService.java
package cn.slimsmart.burlap.spring.demo; public interface UserService { User getUser(String name); }
实现类:UserServiceImpl.java
package cn.slimsmart.burlap.spring.demo; public class UserServiceImpl implements UserService{ @Override public User getUser(String name) { User user = new User(); user.setAge(30); user.setName(name); return user; } }
3.服务端配置
applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-lazy-init="true"> <bean id="userServiceImpl" class="cn.slimsmart.burlap.spring.demo.UserServiceImpl"/> </beans>
applicationContext-burlap-exporter.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-lazy-init="true"> <bean name="/userService" class="org.springframework.remoting.caucho.BurlapServiceExporter"> <property name="service" ref="userServiceImpl" /> <property name="serviceInterface" value="cn.slimsmart.burlap.spring.demo.UserService"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ThriftTest</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext-burlap-exporter.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
4.客户端配置
applicationContext-burlap.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-lazy-init="true"> <bean id="userService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"> <property name="serviceUrl" value="http://127.0.0.1:8080/burlap-spring-demo/remote/userService" /> <property name="serviceInterface" value="cn.slimsmart.burlap.spring.demo.UserService" /> </bean> </beans>
5.测试运行类
UserServiceClientTest.java
package cn.slimsmart.burlap.spring.demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceClientTest { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-burlap.xml"); UserService client = context.getBean(UserService.class); System.out.println(client.getUser("lucy")); } }通过http请求抓包可以看见协议定义
请求:
<burlap:call> <method>getUser</method> <string>lucy</string> </burlap:call>
相应:
<burlap:reply> <map> <type>cn.slimsmart.burlap.spring.demo.User</type> <string>name</string> <string>lucy</string> <string>age</string> <int>30</int> </map> </burlap:reply>
代码:http://download.csdn.net/detail/tianwei7518/8468333