在工作中遇到了多個客戶端調用同一組Service的情況,所以就趕緊測試了一下Rmi,因為程序用的是SPRing框架,所以此處記錄的僅為Spring中調用Rmi的實例說明:
1. 服務端 B/S端
a. 增加接口:DpersonInfo
package com.hr;import java.util.List;public interface DpersonInfo { List<MpersonInfo> getPersonInfoList(); List<MpersonInfo> getPersonInfoList(String ttype); String getTest(String name);}b. 實現接口:DpersonInfoImpl
package com.hr;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;@Repositorypublic class DpersonInfoImpl implements DpersonInfo { private RowMapper<MpersonInfo> rm=new BeanPropertyRowMapper<>(MpersonInfo.class); @Autowired private JdbcTemplate db; @Override public List<MpersonInfo> getPersonInfoList() { String sql="select * from person_info"; return db.query(sql, rm); //return null; } @Override public List<MpersonInfo> getPersonInfoList(String ttype) { String sql="select * from person_info where ttype='"+ttype+"'"; //return null; return db.query(sql, rm); } @Override public String getTest(String name) { // TODO Auto-generated method stub return "hello "+name+" rmi start..."; }}c. 配置發布接口Bean參數
<bean id="helloWorld" class="com.hr.DpersonInfoImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="helloWorld" /> <property name="serviceName" value="hello" /> <property name="serviceInterface" value="com.hr.DpersonInfo" /> <property name="registryPort" value="8088" /> </bean>2. 客戶端 C/S端
a. 增加接口類:此類和服務端的接口類內容需要一致,即DpersonInfo,名稱可以不一樣,但里面的方法名及參數需要一致。
b. 增加List中普通的Bean對象:此類和服務端中List集合中的對象類一致,即MpersonInfo類。
c. spring環境配置
<?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"> <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiproxyFactoryBean"> <property name="serviceUrl" value="rmi://192.168.96.11:8088/hello" /> <property name="serviceInterface" value="com.hr.DpersonInfo" /> </bean> </beans>d. 調用Rmi
package com.hr;import java.util.List;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Imain { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); DpersonInfo hs = (DpersonInfo) ctx.getBean("helloWorld"); System.out.println(hs.getTest("luke")); List<MpersonInfo> list=hs.getPersonInfoList(); for(MpersonInfo m:list){ System.out.println(m.toString()); } }}以上親測可正常使用....
新聞熱點
疑難解答