Spring与RMI集成实现远程访问
2016-05-17 00:00
471 查看
使用Spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
public interface AccountService {
int queryBalance(String mobileNo);
String shoopingPayment(String mobileNo, byte protocol);
}
服务实现,示例如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
public class MobileAccountServiceImpl implements AccountService {
private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
public int queryBalance(String mobileNo) {
if (mobileNo != null)
return 100;
return 0;
}
public String shoopingPayment(String mobileNo, byte protocol) {
StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
mobileNo).append("/", protocol type is /"").append(protocol)
.append("/".");
LOG.info("Message is: " + sb.toString());
return sb.toString();
}
}
服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示:
[xhtml]
view plain
copy
<?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="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MobileAccountService" />
<property name="service" ref="accountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>
<bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
</beans>
上面配置,指定了暴露的服务的名称,通过serviceName属性注入到RmiServiceExporter中,服务名称为MobileAccountService,客户端通过该服务名称就能够进行调用。
下面启动服务端,发布服务,如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}
客户端调用服务
客户端配置client.xml如下所示:
[xhtml]
view plain
copy
<?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="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
</bean>
</beans>
配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiClient {
private static final Logger LOG = Logger.getLogger(RmiClient.class);
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/shirdrn/spring/remote/rmi/client.xml");
AccountService accountService = (AccountService) ctx
.getBean("mobileAccountService");
String result = accountService.shoopingPayment("13800138000", (byte) 5);
LOG.info(result);
}
}
可见,实现远程访问变得非常容易。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
public interface AccountService {
int queryBalance(String mobileNo);
String shoopingPayment(String mobileNo, byte protocol);
}
服务实现,示例如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
public class MobileAccountServiceImpl implements AccountService {
private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
public int queryBalance(String mobileNo) {
if (mobileNo != null)
return 100;
return 0;
}
public String shoopingPayment(String mobileNo, byte protocol) {
StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
mobileNo).append("/", protocol type is /"").append(protocol)
.append("/".");
LOG.info("Message is: " + sb.toString());
return sb.toString();
}
}
服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示:
[xhtml]
view plain
copy
<?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="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MobileAccountService" />
<property name="service" ref="accountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>
<bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
</beans>
上面配置,指定了暴露的服务的名称,通过serviceName属性注入到RmiServiceExporter中,服务名称为MobileAccountService,客户端通过该服务名称就能够进行调用。
下面启动服务端,发布服务,如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}
客户端调用服务
客户端配置client.xml如下所示:
[xhtml]
view plain
copy
<?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="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
</bean>
</beans>
配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:
[java]
view plain
copy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiClient {
private static final Logger LOG = Logger.getLogger(RmiClient.class);
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/shirdrn/spring/remote/rmi/client.xml");
AccountService accountService = (AccountService) ctx
.getBean("mobileAccountService");
String result = accountService.shoopingPayment("13800138000", (byte) 5);
LOG.info(result);
}
}
可见,实现远程访问变得非常容易。
相关文章推荐
- SpringMVC整合Shiro
- Java 8 新特性 – 终极手册整理
- Spring4新特性:Groovy Bean定义DSL
- java中final和static
- java二维码工具类,中间带LOGO的,很强大
- javamail接收邮件(较全)工具类
- java jsp实现不能通过后退访问缓存,每次都访问最新的
- Spring Task定时任务
- Eclipse控制台中的中文输出乱码问题
- java垃圾回收和收尾
- 基于Java NIO的Socket通信
- java:如何用代码控制H2 Database启动
- java如何生成二维码图片
- Java SpringMVC实现国际化整合案例分析(i18n)
- java中equals和==以及toString
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
- Spring MVC JSON自定义类型转换(续)
- 获取准确的操作系统名称的Java工具类
- 基于Activiti5工作流实战企业协同OA办公系统(spring-data-jpa、uur前台组件)
- 实时 Java 社区论坛:Sym