(PUBLISHED IN DEVELOPER IQ - September2005)
Spring supports remoting for six different RPC models:
1. Remote Method Invocation (RMI)
2. Hessian
3. Burlap
4. HTTP invoker
5. EJB
6. JAX-RPC
In all the models, services are configured into the application through spring configuration file as spring managed beans. This is accomplished by using a proxy factory bean that enable us to wire remote services into the properties of our beans as if they were local objects.
Spring provides 'RmiProxyFactoryBean' to use the RMI service and 'RmiServiceExporter' to export any spring managed bean as a RMI service.
For wiring a Hessian based service to Spring client, Spring's 'HessianProxyFactory Bean' is used. To export a Hessian Service 'HessianServiceExporter' is used and similarly for wiring a Burlap service 'BurlapProxyFactoryBean' is used and 'BurlapServiceExporter' is used to export a burlap service.
For exporting beans as HTTP invoker services, 'HttpInvokerServiceExporter' is used .To access an HTTP invoker service 'HttpInvokerProxyFactoryBean' can be used.
Spring provides two proxy factory beans to access the Enterprise Java Beans. 'LocalStatelessSessionProxyFactoryBean' is used to access the EJB in the same container(local) and another one is
'SimpleRemoteStatelessSessionProxyFactoryBean' which is used to access the remote EJBs.
For all the above models spring provides service exporter classes that exports Java Beans as remote service. Spring does not provide any EJB Service Exporter and it provides four abstract support classes to make the development of Spring enabled EJB. They are
1. AbstractMessageDrivenBean to develop MDBs that accept sources other than JMS.
2. AbstractJmsMessageDrivenBean to develop MDBs that accept messages from JMS sources.
3. AbstractStatelessSessionBean to develop stateless session bean.
4. AbstractStstefulSessionBean to develop stateful session bean.
'JaxRpcPostProxyFactoryBean' is used to wire a web service into the spring application.
The client makes calls to the proxy to provide the service and the proxy calls the remote service on behalf of the client.
--------------------------------------------
Now we shall see how to wire other RMI services into spring application and also how to export our own service.
Remote Method Invocation (RMI) Model:
RMI was first introduced in JDK 1.1. But developing and accessing RMI services involves various steps and also have lookups which makes the code hard to test. Spring simplifies the RMI by providing a 'proxy factory bean' that enables us to wire the RMI services into spring application as if they were local beans. Spring also provides a remote exporter that converts our 'spring managed beans' into RMI services.
Spring's 'RmiProxyFactoryBean' is a factory bean that creates a proxy to RMI service. It is declared spring configuration file under the <bean> tag as follows,
<bean id="service1"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://${hostname}/service1</value>
</property>
<property name="serviceInterface">
<value>service1</value>
</property>
</bean>
The url of the RMI service is set through the 'serviceUrl' property. The 'serviceInterface' property specifies the interface that the service implements and only through that the client invokes methods on the service.
For using the service the implementation code is wired to the RMI using the following code,
<bean id="serviceimpl" class="serviceimpl">
<property name="service1">
<ref bean="service1"/>
</property>
</bean>
-------------------------------------------
First set the path and classpath as before. Next edit the RMI service.
//f:\springdemo\rmserver.java
import java.rmi.*;
public interface rmserver extends Remote
{
String getresult(String s) throws RemoteException;
}
----------------------------------------------
//f:\springdemo\rmserverimpl.java
import java.rmi.*;
import java.rmi.server.*;
public class rmserverimpl extends UnicastRemoteObject
implements rmserver
{
public static void main(String args[])
{
try
{
rmserverimpl ob = new rmserverimpl();
Naming.rebind("rmserver",ob);
System.out.println("ready");
}
catch(Exception e1)
{System.out.println(""+e1);}
}
public rmserverimpl() throws RemoteException
{
System.out.println("constructor ok");
}
public String getresult(String a) throws RemoteException
{
return "Hai..."+a;
}
}
----------------------------------------------
//f:\springdemo\rmserver.xml
<?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="rmserver"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://localhost/rmserver</value>
</property>
<property name="serviceInterface">
<value>rmserver</value>
</property>
</bean>
<bean id="rmserverimpl" class="rmserverimpl">
<property name="rmserver">
<ref bean="rmserver"/>
</property>
</bean>
</beans>
---------------------------------------------
//f:\springdemo\rmspring.java
import java.rmi.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
public class rmspring
{
public static void main(String args[])
{
try
{
System.out.println("Wait..");
Resource res = new ClassPathResource("rmi.xml");
BeanFactory factory = new XmlBeanFactory(res);
rmserver bean1 = (rmserver) factory.getBean("rmserver");
String r=bean1.getresult(args[0]);
System.out.println(r);
}
catch(Exception e1)
{System.out.println(""+e1);}
}
}
---------------------------------------
To run:
f:\springdemo>javac rmserver.java
f:\springdemo>javac rmserverimpl.java
f:\springdemo>rmic rmserverimpl (To create stub and skeleton)
f:\springdemo>javac rmspring.java
f:\springdemo>start rmiregistry (a blank window will appear)
f:\springdemo>java rmserverimpl
Open another Window and run the client code by giving the argument
f:\springdemo>java rmspring "sam"
We will get the output as:
Wait.. ...... Hai... sam
Here we have removed the 'lookup' code in the client side.
-----------------------------------------------------------------
Spring also supports the server side of RMI. Here the service itself is written with spring and it is exposed as an RMI service. Here the bean is written as a simple JavaBean. Also we need not generate the stub and skeleton using 'rmic' command and manually add it to RMI registry. Instead of these traditional procedure 'RmiServiceExporter' is used to export any Spring managed bean as an RMI service. It wrapps the bean in an adapter class. The adapter class is then bound to RMI registry and the proxies request the service.
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service1">
<ref bean="service1"/>
</property>
<property name="serviceName">
<value>service1</value>
</property>
<property name="serviceInterface">
<value>service1</value>
</property>
</bean>
The 'serviceName property' indicates the name of service and 'serviceInterface' specifies the interface implemented by the service. There is no need of 'serviceUrl' here
First set the path and classpath as before. Next edit the service.
//f:\springdemo\rmservice.java
public interface rmservice
{
String getresult(String s);
} ------------------------------------------
//f:\springdemo\rmserviceimpl.java
public class rmserviceimpl implements rmservice
{
public static void main(String args[])
{
System.out.println("ready");
}
public rmserviceimpl()
{
System.out.println("constructor ok");
}
public String getresult(String a)
{
return "Hai"+a;
}
}
------------------------------------------
//f:\springdemo\rmservice.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/ spring-beans.dtd">
<beans>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<value>rmservice</value>
</property>
<property name="serviceName">
<value>service1</value>
</property>
<property name="serviceInterface">
<value>rmservice</value>
</property>
</bean>
<bean id="rmservice" class="rmserviceimpl">
</bean>
</beans>
------------------------------------------
//f:\springdemo\rmserviceclient.java
import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
class rmserviceclient
{
public static void main(String args[])
{
try
{
System.out.println("Wait..");
Resource res = new ClassPathResource("rmservice.xml");
BeanFactory factory = new XmlBeanFactory(res);
System.out.println("factory created");
rmservice bean1 = (rmservice)factory.getBean("rmservice");
String s = bean1.getresult(args[0]);
System.out.println(s);
}
catch(Exception e1)
{System.out.println(""+e1);}
}
}
---------------------------------------
To run:
f:\springdemo>javac rmservice.java
f:\springdemo>javac rmserviceimpl.java
f:\springdemo>javac rmserviceclient.java
f:\springdemo>java rmsserviceclient
We will get Output as:
Wait..
Aug 12, 2002 10:55:07 PM
org.springframework.beans.factory.
xml.XmlBeanDefinitionReader
loadBeanDefinitions
INFO: Loading XML bean definitions from
class path resource[rmservice.xml]
Aug 12, 2002 10:55:07 PM
org.springframework.beans.factory.
support.AbstractBeanFactory getBean
INFO: Creating shared instance of
singleton bean 'rmservice'
constructor ok
Hai...sam
Here the service interface doesn't extend the 'java.rmi.Remote' method and 'RemoteException' is not thrown by the methods. There is no binding in the implementation code. Also we can direcly run the client. No run to run 'rmserverimpl' first. Also there is no need to run the RMI registry.
--------------------------------------
|
相关推荐
org.apache.cxf.spring.remoting.Jsr181HandlerMapping.jar
org.springframework.remoting.caucho.BurlapClientInterceptor.class org.springframework.remoting.caucho.BurlapProxyFactoryBean.class org.springframework.remoting.caucho.BurlapServiceExporter.class org....
spring-remoting.jar spring-remoting.jar
15.1. An overview of Spring remoting 15.2. Working with RMI 15.2.1. Exporting an RMI service 15.2.2. Wiring an RMI service 15.3. Exposing remote services with Hessian and Burlap 15.3.1. Exposing bean ...
项目工程结构如下图所示,除了包含spring定义远程服务org.springframework.remoting.httpinvoker.*部分节点代码(红框框部分,具体设计代码部分参考相关代码示例说明文章),还包含springmvc等其他示例部分可以忽略...
该章节介绍了Spring Remoting模块,展示了如何利用Spring框架进行远程服务调用。读者将了解到不同的远程调用协议(如RMI、Hessian、Burlap等)与Spring Remoting的集成方式,以及如何构建分布式应用系统。 ### 第17...
使用Spring Remoting - **远程调用**:Spring支持多种远程调用协议,如HTTP invoker、RMI等。 - **服务端与客户端**:介绍了如何实现服务端与客户端的通信。 #### 13. Spring测试 - **单元测试**:Spring提供了对...
《Spring Remoting详解》 Spring框架以其强大的依赖注入和面向切面编程能力,深受开发者喜爱。在分布式系统中,远程调用(Remoting)是必不可少的技术之一,Spring Remoting提供了多种远程调用解决方案,帮助开发者...
Then, you will learn how to use proxy patterns in aspect-oriented programming and remoting. Moving on, you will understand the JDBC template patterns and their use in abstracting database access. ...
标题"jt_dwr_spring.rar_jt"表明这是一个关于"jt"项目的压缩包,其中包含了"DWR (Direct Web Remoting) 与 Spring 框架的整合"相关的资源。"jt"可能代表某个项目或团队的缩写。 描述提到"DWR 和 Spring 相整合从而...
Spring Recipes 3rd Edition Sources ...15. Spring Java Enterprise Services and Remoting Technologies 16. Spring Messaging 17. Spring Integration 18. Spring Testing 19. Spring Caching 20. Grails
11) spring -remoting.jar需spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-context.jar,spring-web.jar,spring-webmvc.jar 12) spring-support.jar需spring-core.jar,spring-...
Direct Web Remoting(DWR)是一个JavaScript库,它允许JavaScript代码直接调用服务器端的Java方法,实现了浏览器与服务器之间的异步通信。在这个项目中,DWR可能被用来实现实时验证,比如在用户注册时,通过DWR调用...
在Spring中输出、访问RMI,之前做的Demo,希望对大家有所帮助!
自己弄的三层框架Spring.net,Remoting 本系统共分3部分: 1, DataAccess 数据访问层, 使用Ado.Net和Sql2005交互..数据访问层基本的Model, 单表基本操作存储过程, Dao代码可以由"动软代码生成工具修改版"生成 2, ...
1.2 Spring的RMI支持:Spring通过`org.springframework.remoting.rmi.RmiServiceExporter`和`RmiProxyFactoryBean`简化了RMI的使用。`RmiServiceExporter`用于发布服务,而`RmiProxyFactoryBean`则用于创建RMI服务的...
刘冬编写Spring.NET学习笔记20——整合Remoting(应用篇)的例子 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_Remoting.html
`spring-remoting.jar`支持多种远程调用协议,如RMI、Hessian、Burlap等,使得Spring应用能够跨越网络边界进行交互。 #### spring-jmx.jar JMX(Java Management Extensions)是一种标准的Java技术,用于管理和...