# "Eager" statt "Lazy" bei Java Remote Object Activation



## -ben- (19. Dezember 2005)

Hi zusammen!

Bei Java ROA werden die Remote-Objects ja "Lazy" instanziert - also erst wenn ein Client eine Remote Methode dieses Objektes aufruft, wird ein Objekt erzeugt.

Nun, wie kann ich eine "eager" Instanzierung des Objektes erzwingen? Ich möchte, dass beim exportieren des Objektes in den RMI-deamon, gerade auch eine Instanz angelegt wird.

Nur wie? Habe im Web nix gefunden, bin mir aber sicher das geht auf irgend ne Weise...

Danke und Gruss
ben


----------



## Thomas Darimont (19. Dezember 2005)

Hallo!

 Weshalb aktivierst du das entsprechende Activatable nicht einfach direkt nachdem du dessen ActivationDesc gebaut hast?

 Gruss Tom


----------



## Thomas Darimont (20. Dezember 2005)

Hallo!

   Wie wärs denn damit:

```
/**
    * 
    */
   package de.tutorials.service;
   
   import java.rmi.Remote;
   import java.rmi.RemoteException;
   
   /**
    * @author Tom
    * 
    */
   public interface SayHelloService extends Remote {
   	final static String SERVICE_NAME = "SayHelloService";
   	void sayHelloTo(String name) throws RemoteException;
   	void init() throws RemoteException;
   }
```
 

```
/**
    * 
    */
   package de.tutorials.service.impl;
   
   import java.rmi.MarshalledObject;
   import java.rmi.RemoteException;
   import java.rmi.activation.Activatable;
   import java.rmi.activation.ActivationID;
   
   import de.tutorials.service.SayHelloService;
   
   /**
    * @author Tom
    * 
    */
   public class SayHelloServiceImpl extends Activatable implements SayHelloService {
   
   	static {
   		System.out.println(Thread.currentThread().getName());
   	}
   
   	public SayHelloServiceImpl(ActivationID id,
 			MarshalledObject marshalledObject) throws RemoteException {
   		super(id, 0);
   		System.out.println("constructing: " + getClass());
   	}
   
   	public void sayHelloTo(String name) throws RemoteException {
   		System.out.println("Hallo: " + name);
   	}
   
   	public void init() throws RemoteException {
   		System.out.println("init Activatable " + getClass());
   	}
   
   }
```
 

```
/**
    * 
    */
   package de.tutorials.service;
   
   import java.io.File;
   import java.rmi.MarshalledObject;
   import java.rmi.Remote;
   import java.rmi.activation.Activatable;
   import java.rmi.activation.ActivationDesc;
   import java.rmi.activation.ActivationGroup;
   import java.rmi.activation.ActivationGroupDesc;
   import java.rmi.activation.ActivationGroupID;
   import java.rmi.registry.LocateRegistry;
   import java.rmi.registry.Registry;
   import java.util.Properties;
   
   /**
    * @author Tom
    * 
    */
   public class Server {
   
   	/**
   	 * @param args
   	 */
   	public static void main(String[] args) throws Exception {
   		System.out.println(new File(".").toURL());
   
   		Registry registry = LocateRegistry.createRegistry(1099);
   
   		Properties props = new Properties();
   		props
   				.put("java.security.policy",
 		 		 "E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/server.policy");
   		ActivationGroupDesc activationGroupDesc = new ActivationGroupDesc(
   				props, null);
   
   		ActivationGroupID activationGroupID = ActivationGroup.getSystem()
 				.registerGroup(activationGroupDesc);
   
   		MarshalledObject marshalledObject = new MarshalledObject(new File(
   				"sayHelloService.state"));
   
   		ActivationDesc activationDesc = new ActivationDesc(
   				activationGroupID,
 				"de.tutorials.service.impl.SayHelloServiceImpl",
   		        "file:/E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/bin/",
   				marshalledObject);
   
   		Remote sayHelloServiceStub = Activatable.register(activationDesc);
   		((SayHelloService) sayHelloServiceStub).init();
   
   		registry.rebind(SayHelloService.SERVICE_NAME, sayHelloServiceStub);
   
   		Thread.sleep(Integer.MAX_VALUE);
   	}
   }
```
 
   Wenn ich den Server starte (nachdem ich rmid gestartet hab) erhalte ich folgende Ausgabe:

```
C:\Programme\Java\jdk1.5.0_04\bin>rmid.exe -J-Djava.security.policy=E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/server.policy -J-Dclass.path=E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/bin
   Tue Dec 20 00:19:07 CET 2005:ExecGroup-0:out:RMI TCP Connection(1)-192.168.75.98
   Tue Dec 20 00:19:07 CET 2005:ExecGroup-0:out:constructing: class de.tutorials.service.impl.SayHelloServiceImpl
   Tue Dec 20 00:19:07 CET 2005:ExecGroup-0:out:init Activatable class de.tutorials.service.impl.SayHelloServiceImpl
```
 
   Starte ich den Client:

```
package de.tutorials.service;
   
   import java.rmi.registry.LocateRegistry;
   import java.rmi.registry.Registry;
   
   public class Client {
   
   	/**
   	 * @param args
   	 */
   	public static void main(String[] args) throws Exception {
 	 System.getProperties().put("java.security.policy","E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/server.policy");
   		System.setSecurityManager(new SecurityManager());
   		
   		Registry registry = LocateRegistry.getRegistry();
 		SayHelloService sayHelloServiceStub = (SayHelloService)registry.lookup(SayHelloService.SERVICE_NAME);
   		sayHelloServiceStub.sayHelloTo("Thomas");
   		
   	}
   }
```
   Sieht die Ausgabe (in der rmid Konsole) so aus:

```
C:\Programme\Java\jdk1.5.0_04\bin>rmid.exe -J-Djava.security.policy=E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/server.policy -J-Dclass.path=E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/bin
   Tue Dec 20 00:19:07 CET 2005:ExecGroup-0:out:RMI TCP Connection(1)-192.168.75.98
   Tue Dec 20 00:19:07 CET 2005:ExecGroup-0:out:constructing: class de.tutorials.service.impl.SayHelloServiceImpl
   Tue Dec 20 00:19:07 CET 2005:ExecGroup-0:out:init Activatable class de.tutorials.service.impl.SayHelloServiceImpl
   Tue Dec 20 00:21:57 CET 2005:ExecGroup-0:out:Hallo: Thomas
```
 
  //Edit:
 Wer die RMI Aufrufe nachverfolgen möchte kann auch das logging für die RMI Calls über die Option (-Djava.rmi.server.logCalls=true) aktivieren:

```
C:\Programme\Java\jdk1.5.0_04\bin>rmid.exe -J-Djava.security.policy=E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/server.policy -J-Dclass.path=E:/eclipse/3.1/eclipse/workspace/de.tutorials.server/bin -J-Djava.rmi.server.logCalls=true
  24.12.2005 17:35:52 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(1)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  24.12.2005 17:35:52 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(1)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  24.12.2005 17:36:17 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(2)-192.168.75.98: [192.168.75.98: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
  24.12.2005 17:36:17 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(2)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  24.12.2005 17:36:17 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(2)-192.168.75.98: [192.168.75.98: sun.rmi.server.Activation$ActivationSystemImpl[0:0:0, 4]: public abstract java.rmi.activation.ActivationGroupID java.rmi.activation.ActivationSystem.registerGroup(java.rmi.activation.ActivationGroupDesc) throws java.rmi.activation.ActivationException,java.rmi.RemoteException]
  24.12.2005 17:36:17 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(2)-192.168.75.98: [192.168.75.98: sun.rmi.server.Activation$ActivationSystemImpl[0:0:0, 4]: public abstract java.rmi.activation.ActivationID java.rmi.activation.ActivationSystem.registerObject(java.rmi.activation.ActivationDesc) throws java.rmi.activation.ActivationException,java.rmi.activation.UnknownGroupException,java.rmi.RemoteException]
  24.12.2005 17:36:17 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(2)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  24.12.2005 17:36:17 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(2)-192.168.75.98: [192.168.75.98: sun.rmi.server.Activation$ActivatorImpl[0:0:0, 1]: public abstract java.rmi.MarshalledObject java.rmi.activation.Activator.activate(java.rmi.activation.ActivationID,boolean) throws java.rmi.activation.ActivationException,java.rmi.activation.UnknownObjectException,java.rmi.RemoteException]
  24.12.2005 17:36:18 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(3)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  24.12.2005 17:36:18 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(3)-192.168.75.98: [192.168.75.98: sun.rmi.server.Activation$ActivationSystemImpl[0:0:0, 4]: public abstract java.rmi.activation.ActivationMonitor java.rmi.activation.ActivationSystem.activeGroup(java.rmi.activation.ActivationGroupID,java.rmi.activation.ActivationInstantiator,long) throws java.rmi.activation.UnknownGroupException,java.rmi.activation.ActivationException,java.rmi.RemoteException]
  24.12.2005 17:36:18 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(3)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  24.12.2005 17:36:18 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(4)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  Sat Dec 24 17:36:18 CET 2005:ExecGroup-0:out:RMI TCP Connection(1)-192.168.75.98
  Sat Dec 24 17:36:18 CET 2005:ExecGroup-0:out:constructing: class de.tutorials.service.impl.SayHelloServiceImpl
  Sat Dec 24 17:36:18 CET 2005:ExecGroup-0:out:init Activatable class de.tutorials.service.impl.SayHelloServiceImpl
  24.12.2005 17:36:27 sun.rmi.server.UnicastServerRef logCall
 FEINER: RMI TCP Connection(5)-192.168.75.98: [192.168.75.98: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
  Sat Dec 24 17:36:27 CET 2005:ExecGroup-0:out:Hallo: Thomas
```
 

   Gruss Tom


----------



## Thomas Darimont (15. April 2009)

Hallo,

*push*

Irgendjemand hat wollte eben per ICQ ein Beispiel dazu haben...

Gruß Tom


----------

