Spring整合RMI
2015-06-26 15:17
411 查看
Spring整合RMI的原理
客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。
通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。
服务端暴露远程服务
RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。
服务端程序:
1 IHelloWorld.java POJO的接口
[java]
view plaincopy
public interface IHelloWorld {
public String helloWorld();
public String sayHelloToSomeBody(String someBodyName);
}
2 HelloWorld.java POJO的实现
[java]
view plaincopy
public class HelloWorld implements IHelloWorld {
@Override
public String helloWorld() {
return "Hello World!";
}
@Override
public String sayHelloToSomeBody(String someBodyName) {
return "Hello World!" + someBodyName;
}
}
3 spring配置文件rmi_server_context.xml
[xhtml]
view plaincopy
<?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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="helloWorld" class="springapp.rmi.rmi.HelloWorld" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="helloWorld" />
<!-- 定义服务名 -->
<property name="serviceName" value="hello" />
<property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" />
<property name="registryPort" value="8088" />
</bean>
</beans>
4 服务端启动RMI的代码HelloHost.java
[java]
view plaincopy
public class HelloHost {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"rmi_server_context.xml");
System.out.println("RMI服务伴随Spring的启动而启动了.....");
}
}
客户端
1 配置文件rmi_client_context.xml
[java]
view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://10.87.40.141:8088/hello" />
<property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" />
</bean>
</beans>
2 客户端代码 HelloClient.java
[java]
view plaincopy
public class HelloClient {
public static void main(String[] args) throws RemoteException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"rmi_client_context.xml");
IHelloWorld hs = (IHelloWorld) ctx.getBean("helloWorld");
System.out.println(hs.helloWorld());
System.out.println(hs.sayHelloToSomeBody("Lavasoft"));
}
}
原文地址:http://blog.csdn.net/arkblue/article/details/6237380
客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。
通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。
服务端暴露远程服务
RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。
服务端程序:
1 IHelloWorld.java POJO的接口
[java]
view plaincopy
public interface IHelloWorld {
public String helloWorld();
public String sayHelloToSomeBody(String someBodyName);
}
2 HelloWorld.java POJO的实现
[java]
view plaincopy
public class HelloWorld implements IHelloWorld {
@Override
public String helloWorld() {
return "Hello World!";
}
@Override
public String sayHelloToSomeBody(String someBodyName) {
return "Hello World!" + someBodyName;
}
}
3 spring配置文件rmi_server_context.xml
[xhtml]
view plaincopy
<?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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="helloWorld" class="springapp.rmi.rmi.HelloWorld" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="helloWorld" />
<!-- 定义服务名 -->
<property name="serviceName" value="hello" />
<property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" />
<property name="registryPort" value="8088" />
</bean>
</beans>
4 服务端启动RMI的代码HelloHost.java
[java]
view plaincopy
public class HelloHost {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"rmi_server_context.xml");
System.out.println("RMI服务伴随Spring的启动而启动了.....");
}
}
客户端
1 配置文件rmi_client_context.xml
[java]
view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://10.87.40.141:8088/hello" />
<property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" />
</bean>
</beans>
2 客户端代码 HelloClient.java
[java]
view plaincopy
public class HelloClient {
public static void main(String[] args) throws RemoteException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"rmi_client_context.xml");
IHelloWorld hs = (IHelloWorld) ctx.getBean("helloWorld");
System.out.println(hs.helloWorld());
System.out.println(hs.sayHelloToSomeBody("Lavasoft"));
}
}
原文地址:http://blog.csdn.net/arkblue/article/details/6237380
相关文章推荐
- 错误笔记:Caused by: java.lang.StackOverflowError
- java类的加载以及初始化顺序
- Spring MVC使用拦截器实现权限控制
- QA:java.lang.RuntimeException:java.io.FileNotFoundException:Resource nexus-maven-repository-index.properties does not exist.
- R语言RJava安装步骤
- Spring学习(3)---Spring设值注入和构造注入
- Eclipse中出现无法找到Maven包
- Java学习-016-CSV 文件读取实例源代码
- 在eclipse中搭建SSH框架
- VisualRules 调用Java类
- java学习之旅22--综合练习_九九乘法表
- Java内部类详解
- Hibernate4 与 spring3 集成之后 “No Session found for current thread”
- 如何创建Java中的线程池
- java 多线程
- java四种内部类详解
- 在eclipse中将android项目生成apk并且给apk签名
- Java多线程的用法详解
- Java学习-015-CSV 文件写入实例源代码
- Spring的注解积累