在工作中遇到了多个客户端调用同一组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()); } }}以上亲测可正常使用....
新闻热点
疑难解答