tutorials.de Buch-Aktion 02/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1819
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Newcomer2007 Newcomer2007 ist offline Rookie
    Registriert seit
    May 2007
    Beiträge
    5
    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:

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    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(); 
        } 
     
      } 
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    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(); 
        } 
     
      } 
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    public interface RemoteServiceController 
    { 
      public void setString(String sqlStmt); 
     
      public String getString(); 
     
      public void setResult(Object result); 
     
      public Object getResult(); 
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    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
    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <?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
    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <?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
     

  2. #2
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.724
    Blog-Einträge
    29
    Hallo,

    schau mal hier:
    IBusinessService
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    /**
     * 
     */
    package de.tutorials.training.services;
     
    /**
     * @author Thomas.Darimont
     *
     */
    public interface IBusinessService {
        String businessOperation(String args);
    }

    BusinessService
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    /**
     * 
     */
    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
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    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
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    /**
     * 
     */
    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
    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?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
           [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
           [url]http://www.springframework.org/schema/aop[/url]
           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
    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <?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
           [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
           [url]http://www.springframework.org/schema/aop[/url]
           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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    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
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

  3. #3
    Newcomer2007 Newcomer2007 ist offline Rookie
    Registriert seit
    May 2007
    Beiträge
    5
    Hallo Tom,

    danke für das Beispiel
    Jetzt kann ich damit etwas spielen und rumbasteln

    Grüßle
     

Ähnliche Themen

  1. Spring Security NTLM & Spring Framework 3.0
    Von Thomas Darimont im Forum Enterprise Java (JEE, J2EE, Spring & Co.)
    Antworten: 2
    Letzter Beitrag: 26.01.10, 12:50
  2. Unterschiede Spring und Spring RCP
    Von Looky im Forum Java
    Antworten: 8
    Letzter Beitrag: 26.01.09, 08:45
  3. Planung Serversoftware mit Spring als Framework, wie Spring einsetzen?
    Von DarthShader im Forum Enterprise Java (JEE, J2EE, Spring & Co.)
    Antworten: 8
    Letzter Beitrag: 26.09.08, 19:05
  4. DynamicProxy mit Spring.Aop aus Spring.NET
    Von Thomas Darimont im Forum .NET Application und Service Design
    Antworten: 0
    Letzter Beitrag: 27.02.08, 14:00
  5. Spring One Praesentation: Introduction to Data Access with Spring.NET
    Von Thomas Darimont im Forum .NET Datenverwaltung
    Antworten: 2
    Letzter Beitrag: 21.06.06, 13:42