RMI mit Spring

Newcomer2007

Grünschnabel
Hallo zusammen,

bin absoluter Neuling was dieses Thema betrifft und brauche etwas Hilfe :)
Habe eine kleine Testanwendung geschrieben, welche leider nicht das gewünschte Ergebnis liefert.

Als Ergebnis würde ich folgendes erwarten

Beim einem Client
Antwort fuer Client: A

Beim anderen Client
Antwort fuer Client: B

Leider bekommen beide das Ergebnis:
Antwort fuer Client: A

Hier der Code meines Testprogramms:

Java:
public class TestClient 
{ 
  public static void main(String[] args) 
  { 
     try 
    { 
    
      ApplicationContext appContext = new ClassPathXmlApplicationContext( 
          "meinTest/client/rmi-client.xml"); 
      RemoteServiceController remoteServiceController = (RemoteServiceController) appContext 
          .getBean("remoteServiceController"); 

       while(true) 
       { 
         remoteServiceController.setString("" + args[0]); 
          
         System.out.println(remoteServiceController.getResult()); 
          
         Thread.sleep(10000); 
       } 
      
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
    } 

  } 
}

Java:
public class TestServer 
{ 
  public static void main(String[] args) 
  { 
     try 
    { 
    
      ApplicationContext appContext = new ClassPathXmlApplicationContext( 
          "meinTest/server/rmi-server.xml"); 
      RemoteServiceController remoteServiceController = (RemoteServiceController)   
      appContext.getBean("remoteServiceController"); 
       while(true) 
       { 
         this.remoteServiceController.setResult("Antwort fuer Client " 
            + this.remoteServiceController.getString());          
         Thread.sleep(10000); 
       } 
      
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
    } 

  } 
}

Java:
public interface RemoteServiceController 
{ 
  public void setString(String sqlStmt); 

  public String getString(); 

  public void setResult(Object result); 

  public Object getResult(); 
}

Java:
public class RemoteServiceControllerImpl implements RemoteServiceController 
{ 
   private String str = null; 
   private Object obj = null; 

   public void setString(String str) 
  { 
    this.str = str; 
  } 

  public String getString() 
  { 
    return this.str; 
  } 

  public void setResult(Object obj) 
  { 
    this.obj = obj; 
  } 

  public Object getResult() 
  { 
    return this.obj; 
  } 
}

client.xml
XML:
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
   <bean 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="location" 
         value="meinTest/client/rmi/test/rmi-client.properties" 
         /> 
   </bean> 
   <bean id="remoteServiceController" 
      class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
      <property name="serviceUrl" 
         value="rmi://${server.ip}:1099/RemoteServiceController" /> 
      <property name="serviceInterface" 
         value="meinTest.util.rmi.RemoteServiceController" /> 
   </bean> 
</beans>

server.xml
XML:
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
          "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="remoteServiceController" 
        class="meinTest.util.rmi.RemoteServiceControllerImpl"> 
  </bean> 
  <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> 
    <property name="serviceName"      value="RemoteServiceController" /> 
    <property name="service"          ref="remoteServiceController" /> 
    <property name="serviceInterface" value="meinTest.util.rmi.RemoteServiceController" /> 
  </bean> 
</beans>

Würde mich über eine kleine Hilfestellung sehr freuen
 
Zuletzt bearbeitet von einem Moderator:
Hallo,

schau mal hier:
IBusinessService
Java:
/**
 * 
 */
package de.tutorials.training.services;

/**
 * @author Thomas.Darimont
 *
 */
public interface IBusinessService {
    String businessOperation(String args);
}

BusinessService
Java:
/**
 * 
 */
package de.tutorials.training.services.internal;

import de.tutorials.training.services.IBusinessService;

/**
 * @author Thomas.Darimont
 *
 */
public class BusinessService implements IBusinessService{
    
    public BusinessService(){
        System.out.println("Init:" + getClass());
    }
    
    @Override
    public String businessOperation(String args) {
        System.out.println("Called from Thread: " + Thread.currentThread().getName() + " with args: " + args);
        return args.toUpperCase();
    }
}

Server-Main
Java:
package de.tutorials.training.rmi.server;


import java.util.concurrent.TimeUnit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author Thomas.Darimont
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/server-context.xml");
        applicationContext.getBean("businessService");
        System.out.println("Server ready...");
        
        try {
            TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Client-Main
Java:
/**
 * 
 */
package de.tutorials.training.rmi.client;

import java.rmi.dgc.VMID;
import java.util.concurrent.TimeUnit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import de.tutorials.training.services.IBusinessService;

/**
 * @author Thomas.Darimont
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/client-context.xml");
        System.out.println("Client: " + new VMID() + " is ready");
        IBusinessService businessService = (IBusinessService)applicationContext.getBean("businessService");
        for(int i = 0; i< 5;i++){
        System.out.println(businessService.businessOperation("abc" + System.currentTimeMillis()));
        try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}


Server-context.xml
XML:
<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
       
    <bean name="businessService" class="de.tutorials.training.services.internal.BusinessService"/>

    <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
     <property name="serviceName" value="BusinessService"/>
     <property name="service" ref="businessService"/>
     <property name="serviceInterface" value="de.tutorials.training.services.IBusinessService"/>
     <property name="registryPort" value="10099"/>
    </bean>

</beans>

Client-context.xml
XML:
<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
       
    <bean name="businessService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
     <property name="serviceUrl" value="rmi://localhost:10099/BusinessService"/>
     <property name="serviceInterface" value="de.tutorials.training.services.IBusinessService"/>
    </bean>
    
</beans>

Ausgabe: Server
Code:
21.05.2007 15:10:08 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@c40c80: display name [org.springframework.context.support.FileSystemXmlApplicationContext@c40c80]; startup date [Mon May 21 15:10:08 CEST 2007]; root of context hierarchy
21.05.2007 15:10:08 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Dokumente und Einstellungen\Thomas.Darimont\workspace\de.tutorials.training.spring\conf\server-context.xml]
21.05.2007 15:10:08 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@c40c80]: org.springframework.beans.factory.support.DefaultListableBeanFactory@ae506e
21.05.2007 15:10:08 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ae506e: defining beans [businessService,org.springframework.remoting.rmi.RmiServiceExporter]; root of factory hierarchy
Init:class de.tutorials.training.services.internal.BusinessService
21.05.2007 15:10:08 org.springframework.remoting.rmi.RmiServiceExporter getRegistry
INFO: Looking for RMI registry at port '10099'
21.05.2007 15:10:10 org.springframework.remoting.rmi.RmiServiceExporter getRegistry
INFO: Could not detect RMI registry - creating new one
21.05.2007 15:10:10 org.springframework.remoting.rmi.RmiServiceExporter prepare
INFO: Binding service 'BusinessService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.5.64:10099](local),objID:[0:0:0, 0]]]]
Server ready...
Called from Thread: RMI TCP Connection(2)-192.168.5.64 with args: abc1179753011351
Called from Thread: RMI TCP Connection(4)-192.168.5.64 with args: abc1179753012695
Called from Thread: RMI TCP Connection(2)-192.168.5.64 with args: abc1179753016366
Called from Thread: RMI TCP Connection(4)-192.168.5.64 with args: abc1179753017710
Called from Thread: RMI TCP Connection(2)-192.168.5.64 with args: abc1179753021381
Called from Thread: RMI TCP Connection(4)-192.168.5.64 with args: abc1179753022709
Called from Thread: RMI TCP Connection(2)-192.168.5.64 with args: abc1179753026381
Called from Thread: RMI TCP Connection(4)-192.168.5.64 with args: abc1179753027709
Called from Thread: RMI TCP Connection(2)-192.168.5.64 with args: abc1179753031380
Called from Thread: RMI TCP Connection(4)-192.168.5.64 with args: abc1179753032708


Ausgabe: Client 1
Code:
21.05.2007 15:10:12 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@dbe178: display name [org.springframework.context.support.FileSystemXmlApplicationContext@dbe178]; startup date [Mon May 21 15:10:12 CEST 2007]; root of context hierarchy
21.05.2007 15:10:12 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Dokumente und Einstellungen\Thomas.Darimont\workspace\de.tutorials.training.spring.client\conf\client-context.xml]
21.05.2007 15:10:12 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@dbe178]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3901c6
21.05.2007 15:10:12 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3901c6: defining beans [businessService]; root of factory hierarchy
Client: 35ab801d281f932e:-67d240fd:112aec25d2b:-7ffe is ready
ABC1179753012695
ABC1179753017710
ABC1179753022709
ABC1179753027709
ABC1179753032708

Ausgabe: Client 2
Code:
21.05.2007 15:10:10 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@dbe178: display name [org.springframework.context.support.FileSystemXmlApplicationContext@dbe178]; startup date [Mon May 21 15:10:10 CEST 2007]; root of context hierarchy
21.05.2007 15:10:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Dokumente und Einstellungen\Thomas.Darimont\workspace\de.tutorials.training.spring.client\conf\client-context.xml]
21.05.2007 15:10:11 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@dbe178]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3901c6
21.05.2007 15:10:11 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3901c6: defining beans [businessService]; root of factory hierarchy
Client: 35ab801d281f932e:-a592f75:112aec257ec:-7ffe is ready
ABC1179753011351
ABC1179753016366
ABC1179753021381
ABC1179753026381
ABC1179753031380

Gruß Tom
 
Zuletzt bearbeitet von einem Moderator:

Neue Beiträge

Zurück