温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Spring环境下使用RMI

发布时间:2021-08-24 18:02:49 来源:亿速云 阅读:125 作者:chen 栏目:编程语言

本篇内容介绍了“怎么在Spring环境下使用RMI”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在Spring环境使用RMI时,可以省略以下几点:

1、接口类不需要继承Remote,方法不需要抛出RemoteException异常对象。

2、实现类不需要继承UnicastRemoteObject。

3、RMI服务自动注册。

下面通过具体的例子来说明其用法。

一、导出RMI服务

1、bean的配置

Xml代码

<beans>        <bean id="syncServiceImpl" class="com.cjm.service.rmi.SyncServiceImpl" />                 <bean id="syncServiceProxy" class="com.cjm.service.rmi.SyncServiceRMIExporter">            <property name="service">                <ref bean="syncServiceImpl" />             property>            <property name="serviceName">                <value>hawkeyeServicevalue>             property>            <property name="serviceInterface">                <value>com.cjm.service.rmi.SyncServicevalue>             property>            <property name="servicePort">                <value>1099value>            property>            <property name="registryPort">                <value>1099value>             property>        bean>    beans>

2、类源码

Java代码

public class SyncServiceRMIExporter extends RmiServiceExporter{         public SyncServiceRMIExporter() {             //通过系统属性设置RMI的hostname             System.setProperty("java.rmi.server.hostname", "localhost");          }     }

Java代码

//RMI服务接口类     public interface SyncService{         public boolean updateMonicaSiInfos(String oldInfo, String newInfo);     }

Java代码

public class SyncServiceImpl implements SyncService {         @Override        public boolean updateMonicaSiInfos(String oldInfo, String newInfo) {             oldInfo = StringUtils.trimToEmpty(oldInfo);             newInfo = StringUtils.trimToEmpty(newInfo);                 if (StringUtils.isEmpty(newInfo)) {                 return false;             }                          ......                          logger.warn("成功: oldInfo=" + oldInfo + ", newInfo=" + newInfo);             return true;         }     }

二、调用RMI服务

1、bean的配置

Xml代码

<beans>            <bean id="serviceImpl" class="RMIServiceImpl">            <property name="syncService" ref="hawkeyeService"/>        bean>                 <bean id="hawkeyeService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">                     <property name="serviceUrl">                             <value>rmi://localhost:1099/hawkeyeServicevalue>                      property>                     <property name="serviceInterface">                             <value>com.cjm.service.rmi.SyncServicevalue>                       property>             bean>    beans>

2、类源码

Java代码

public class RMIServiceImpl {         private SyncService syncService;             public SyncService getSyncService() {             return syncService;         }             public void setSyncService(SyncService syncService) {             this.syncService = syncService;         }                  public void doAction(String oldValue, String newValue)throws Exception{             boolean b = syncService.updateMonicaSiInfos(oldValue, newValue);             if(b){                 System.out.println("RMI调用成功");             }else{                 System.out.println("RMI调用失败");             }         }     }

“怎么在Spring环境下使用RMI”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI