精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
作者 | 正文 | ||||||||||||
发表时间:2009-06-30
最后修改:2009-10-08
/** * 转载请注明作者longdick http://longdick.iteye.com * */
远程服务调用大致可以分为固定语言环境和语言中立两种;前者如基于rmi的EJB、基于java序列化的Spring Http Invoker等,后者有基于soap协议的Web Service、使用二进制流传输对象的Hessian、基于xml rpc协议的Burlap等。由于EJB的使用和开发比较麻烦,故一般较少使用。相较而言,web service、Hessian等轻量级的解决方案更受欢迎。Spring已经集成了包括Hessian、Burlap、Http Invoker等几种服务调用方式供我们选择,可以帮助我们简化开发,提高效率,而且几种调用方式之间可以平滑切换,只需要更改配置文件,不需要更改具体的代码。
服务端todo list: package org.spring.test.service.remoting; public interface PhoneNumManager { public void setName(String name); public String getName(); public String getNum(); public PhoneBook getPhoneBook(); }
实现类:
package org.spring.test.service.remoting; public class PhoneNumManagerImpl implements PhoneNumManager{ private String name="default"; private String num="000000"; @Override public String getName() { return "the name is :"+name; } @Override public String getNum() { return name+":"+num; } @Override public void setName(String name) { this.name=name; } @Override public void setNum(String num) { this.num=num; } @Override public PhoneBook getPhoneBook() { PhoneBook book=new PhoneBook(); book.setName(name); book.setNum(num); return book; } }
public class PhoneBook implements Serializable { private static final long serialVersionUID = 1L; private String name; private String num; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>remote</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/remote-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="phoneNumManager" class="org.spring.test.service.remoting.PhoneNumManagerImpl" /> <bean name="/PhoneNumServiceHessian" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="phoneNumManager" /> <property name="serviceInterface" value="org.spring.test.service.remoting.PhoneNumManager" /> </bean> <bean name="/PhoneNumServiceHttpInvoker" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="phoneNumManager" /> <property name="serviceInterface" value="org.spring.test.service.remoting.PhoneNumManager" /> </bean> <bean name="/PhoneNumServiceBurlap" class="org.springframework.remoting.caucho.BurlapServiceExporter"> <property name="service" ref="phoneNumManager" /> <property name="serviceInterface" value="org.spring.test.service.remoting.PhoneNumManager" /> </bean> </beans>
然后是客户端 cleint spring bean配置文件context_remoting.xml,需要定义remoting的客户端调用bean,我们分别使用HttpInvokerProxyFactoryBean和HessianProxyFactoryBean 这两个FactoryBean来取得远程对象,property需指定serviceUrl和serviceInterface。ps:sweb是context name,remoting是servlet path:
<?xml version="1.0" encoding="GB2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans default-autowire="byName"> <bean id="httpInvokerServices" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8086/sweb/remoting/PhoneNumServiceHttpInvoker</value> </property> <property name="serviceInterface"> <value>org.spring.test.service.remoting.PhoneNumManager</value> </property> </bean> <bean id="hessianServices" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8086/sweb/remoting/PhoneNumServiceHessian</value> </property> <property name="serviceInterface"> <value>org.spring.test.service.remoting.PhoneNumManager</value> </property> </bean> <bean id="burlapServices" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8086/sweb/remoting/PhoneNumServiceBurlap</value> </property> <property name="serviceInterface"> <value>org.spring.test.service.remoting.PhoneNumManager</value> </property> </bean> </beans>
test类:
public class RemotingTest extends TestCase{ ApplicationContext ac = new FileSystemXmlApplicationContext( "src/conf.test/context_remoting.xml") public void testHttpInvoke(){ PhoneNumManager phoneNumManager=(PhoneNumManager)ac.getBean("httpInvokerServices"); phoneNumManager.setName("httpInvoker"); Assert.assertEquals("the name is :HttpInvoker",phoneNumManager.getName()); Assert.assertEquals("HttpInvoker:13456789012",phoneNumManager.getNum()); } public void testHession(){ PhoneNumManager phoneNumManager=(PhoneNumManager)ac.getBean("hessianServices"); phoneNumManager.setName("Hessian"); Assert.assertEquals("the name is :Hessian",phoneNumManager.getName()); Assert.assertEquals("Hessian:13456789012",phoneNumManager.getNum()); } public void testBurlap(){ PhoneNumManager phoneNumManager=(PhoneNumManager)ac.getBean("burlapServices"); phoneNumManager.setName("Burlap"); Assert.assertEquals("the name is :Burlap",phoneNumManager.getName()); Assert.assertEquals("Burlap:13456789012",phoneNumManager.getNum()); } }
到此我们已经可以调用远程服务了。 问题: 其实客户端将对象本地化时可以不用PhoneNumManager这个接口,用其他的接口也可以,只要该接口中的方法名和PhoneNumManager的一致就可以。 public interface OtherInterface { public void setName(String name); public String getName(); public String getNum(); public PhoneBookClient getPhoneBook(); }
PhoneBookClient是个建在客户端和PhoneBook一摸一样的VO,然后将test类里PhoneNumManager接口替换掉: public void testHession(){ OtherInterface phoneNumManager=(OtherInterface)ac.getBean("hessianServices"); phoneNumManager.setName("Hessian"); Assert.assertEquals("the name is :Hessian",phoneNumManager.getName()); Assert.assertEquals("Hessian:13456789012",phoneNumManager.getNum()); }
public interface OtherInterface { public void setName(String name); public String getName(); public String getNum(); public Map getPhoneBook(); }
OK,最后总结一下,几种remoting方案的特点:
环境: tomcat6.0,JDK6,spring-2.5.6.jar,hessian-3.2.0.jar
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|||||||||||||
返回顶楼 | |||||||||||||
浏览 4609 次