SPRING RMI Remoting调用实例

使用SPRING的POJO方式不用太关心RMI的STUB等代码的编写,可以轻松编写网络通讯代码(RPC方式),下面是个实用的例子;

 

1. 定义POJO类,本例中定义2个Interface加2个Class,基本定义如下:

public interface IExecutor {

 /**
  * remote interface for RMI call.
  */
 public void execute();
 
 /**
  * put string for remote interface.
  *
  */
 public void putSomething(String str);
 
 /**
  * get some results back from the RMI server side.
  *
  * @return
  */
 public String getSomething();

}

 

public interface IReference {
 
  /**
   * simple method for a specified interface Axe.
   * @return
   */
  public String execute();
 
}

 

public class RemoteExecutor implements IExecutor {

 private IReference reference = null;
 
 public RemoteExecutor() {
  System.out.println("Spring bean initializing:Chinese instance...");
 }
 
 public void setReference(IReference reference) {
  System.out.println("Spring using depency injection...");
  this.reference = reference;
 }
 
 @Override
 public void execute() {
  System.out.println(reference.execute());
 }
 
 @Override
 public void putSomething(String sth) {
  System.out.println("Something from cient caller: " + sth);
 }
 
 @Override
 public String getSomething() {
  Calendar calNow = Calendar.getInstance();
  StringBuffer sb = new StringBuffer();
  sb.append("Server Time:");
  sb.append(calNow.get(Calendar.YEAR)).append("-");
  sb.append(calNow.get(Calendar.MONTH)).append("-");
  sb.append(calNow.get(Calendar.DATE)).append(" ");
  sb.append(calNow.get(Calendar.HOUR)).append(":");
  sb.append(calNow.get(Calendar.MINUTE)).append(":");
  sb.append(calNow.get(Calendar.SECOND));
  return sb.toString();
 }

}

 

public class ReferenceImpl implements IReference {
 
 public ReferenceImpl() {
 }
 
 @Override
 public String execute() {
  return "reference bean execution...";
 }

}

 

这里演示的例子存在一个依赖关系,即RemoteExecutor依赖IReference,利用SPRING的依赖注入可以轻松解决Bean类的装载问题;

 

2. 编写配置文件,分别是服务器端配置文件以及客户端配置文件:

注意这2个配置文件需要放到classes根目录下(如果在其他目录需要写相对路径或者load的绝对路径来加载):

服务器端配置文件applicationContext_server.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 3.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
 <bean id="referenceImpl" class="com.sw.spring.remoting.ReferenceImpl" />
 <bean id="executorImpl" class="com.sw.spring.remoting.RemoteExecutor" dependency-check="all">
  <property name="reference">
   <ref local="referenceImpl" />
  </property>
 </bean>
 
 <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="remoteExecutor" />
  <property name="service"><ref bean="executorImpl"/></property>
  <property name="serviceInterface" value="com.sw.spring.remoting.IExecutor" />
  <property name="registryPort" value="1199" />
 </bean>
</beans>

接下来配置客户端配置文件applicationContext_client.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 3.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
 <bean id="executor_1199" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl" value="rmi://192.168.0.191:1199/remoteExecutor" />
        <property name="serviceInterface" value="com.sw.spring.remoting.IExecutor" />
 </bean>
</beans>

 

3. 编写服务器服务装载代码:

public class SpringServiceLoader {

 public static void main(String[] args) {
  try {
   startServerByConfig();
//   startServerByAPI();
   
   System.out.println("RMI listen service loading started.");
  } catch (Exception e) {
   System.out.println(e);
   System.exit(1);
  }
 }
 
 /**
  * start the whole call by using API
  *
  */
 private static void startServerByAPI() {
  RmiServiceExporter exporter = new RmiServiceExporter();
  
  try {
   exporter.setServiceInterface(IExecutor.class);
   exporter.setServiceName("remoteExecutor");
   exporter.setService(new RemoteExecutor());
   exporter.setRegistryPort(1199);
   
   exporter.afterPropertiesSet();
   //exporter.afterPropertiesSet();
  } catch (RemoteException e) {
   e.printStackTrace();
  }
 }

 /**
  * start the whole call by using configuration
  */
 @SuppressWarnings("unused")
 private static void startServerByConfig() {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    "applicationContext_server.xml");
//  final RemoteExecutor beanForChinese = (RemoteExecutor) applicationContext
//    .getBean("executorImpl");

 }

}

 

4. 编写客户端调用代码:

public class SpringClientCaller {

 /**
  * Spring main entry for remote call test.
  *
  * @param args
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException {
  //callByConfig();
  callByAPI();
 }
 
 private static void callByAPI() {
  RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
  factory.setServiceInterface(IExecutor.class);
  factory.setServiceUrl("rmi://192.168.0.191:1199/remoteExecutor");
  factory.afterPropertiesSet();
  
  IExecutor executor = (IExecutor)factory.getObject();
  executor.execute();
  executor.putSomething("Put something");
  String sth = executor.getSomething();
  System.out.println(sth);
 }
 
 private static void callByConfig() throws InterruptedException {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    "applicationContext_client.xml");

  // get the remote call interface
  System.out
    .println("Excutor test------------------------------------------");
  IExecutor personForChinese = (IExecutor) applicationContext
    .getBean("executor_1199");
  personForChinese.execute();

  // put something test for remote call
  System.out
    .println("PutSomething test------------------------------------------");
  personForChinese
    .putSomething("End user putsomething for remote server from client caller.");

  // get something test for remote call
  System.out
    .println("GetSomething test------------------------------------------");
  System.out
    .println("See 'reference bean execution...' in server window.");
  for (int i = 0; i < 5; i++) {
   if (i > 0) {
    System.out.println("--->Afer sleeping 2 seconds");
   }
   String strFromServer = personForChinese.getSomething();
   System.out.println(strFromServer);
   Thread.sleep(2000);
  }
 }

调试时的环境SPRING使用的是3.0.0.RC1,基本类库列表如下:

-----------------------------------------------------------------------------

aopalliance-1.0.jar
org.springframework.aop-3.0.0.RC1.jar
org.springframework.asm-3.0.0.RC1.jar
org.springframework.aspects-3.0.0.RC1.jar
org.springframework.beans-3.0.0.RC1.jar
org.springframework.context-3.0.0.RC1.jar
org.springframework.context.support-3.0.0.RC1.jar
org.springframework.core-3.0.0.RC1.jar
org.springframework.expression-3.0.0.RC1.jar
org.springframework.instrument-3.0.0.RC1.jar
org.springframework.instrument.tomcat-3.0.0.RC1.jar
org.springframework.integration-tests-3.0.0.RC1.jar
org.springframework.jdbc-3.0.0.RC1.jar
org.springframework.jms-3.0.0.RC1.jar
org.springframework.orm-3.0.0.RC1.jar
org.springframework.oxm-3.0.0.RC1.jar
org.springframework.spring-library-3.0.0.RC1.libd
org.springframework.test-3.0.0.RC1.jar
org.springframework.transaction-3.0.0.RC1.jar
org.springframework.web-3.0.0.RC1.jar
org.springframework.web.portlet-3.0.0.RC1.jar
org.springframework.web.servlet-3.0.0.RC1.jar

----------------------------------------------------------------

另附加Log library:

commons-logging.jar
log4j-1.2.9.jar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值