]> rtime.felk.cvut.cz Git - orte.git/commitdiff
JORTE: don't use finalizers in Java classes
authorMartin Vajnar <martin.vajnar@gmail.com>
Tue, 16 Jul 2013 09:48:07 +0000 (11:48 +0200)
committerMartin Vajnar <martin.vajnar@gmail.com>
Tue, 16 Jul 2013 09:48:07 +0000 (11:48 +0200)
This replaces finalizers with explicit destroy() functions, because the
order in which objects with no reference in the VM are garbage-collected
is not guaranteed. This caused trouble with finalization of DomainApp
and Publisher (or Subscriber). If the DomainApp was destroyed before
the Publisher (or Subscriber) the Publisher's finalizer would have been
stuck (eventually leading to TimeoutException to be thrown).

orte/java/src/org/ocera/orte/DomainApp.java
orte/java/src/org/ocera/orte/DomainMgr.java
orte/java/src/org/ocera/orte/Publication.java
orte/java/src/org/ocera/orte/Subscription.java
orte/java/src/org/ocera/orte/examples/hello/Publisher.java
orte/java/src/org/ocera/orte/examples/hello/Subscriber.java
orte/java/src/org/ocera/orte/types/DomainProp.java
orte/libjorte/JORTEDomainPropDestroy.c

index 2a0afea36e1c68e4d29ca90640716f07232fd3bc..09efabff9506b14e857aa57ec9acd91087048614 100644 (file)
@@ -68,25 +68,18 @@ public class DomainApp extends Domain
       else System.out.println(":j: application domain created..");
    }
 
-   
-   /**
-    * Destructor. Before it destroy Application Domain, destroy all registered 
-    * data types too.
-    */
-   protected void finalize()
-   {
-     if(!destroyAllRegTypes()) System.out.println(":j!: destroyAllRegTypes fault!");
-     if(!destroy()) System.out.println(":j!: ORTEDomainADestroy fault!");
-   }
-
-    
    /**
     * Destroy the Application Domain.  
     */
    public boolean destroy()
    {
-     if (jORTEDomainAppDestroy(this.handle)) return true;
-     return false;
+     if(!destroyAllRegTypes()) System.out.println(":j!: destroyAllRegTypes fault!");
+     if(!jORTEDomainAppDestroy(this.handle) || !this.props.destroy()) {
+       System.out.println(":j!: ORTEDomainADestroy fault!");
+       return false;
+     }
+     return true;
+       
    }
 
      
index bf4394c78cb249db09565a9d429fb32b6bf7e660..0919db80e09b1267ab5f0c570e8aaf8b7d3dddc8 100644 (file)
@@ -55,22 +55,26 @@ public class DomainMgr extends Domain
                         DomainEvents events,
                         boolean suspend)
     {
+     super();   // set Default Domain Properties
+     if(props == null) {
+         this.props = DomainProp.defaultPropsCreate();
+     }
+     else {
+         this.props = props;
+     }
+     // init Domain Events
+     if(events == null) {
+         this.events.init();
+     }
+     else {
+         this.events = events;
+     }
           handle = jORTEDomainMgrCreate(domain,
                                         props.handle,
                                             events==null ? 0 : events.getHandle(),
                                                                 suspend);
     }
 
-
-    /**
-        * destructor
-        *
-        */
-        protected void finalize()
-        {
-           destroy();
-        }
-
         /*
      public void create()
      {}
@@ -83,7 +87,7 @@ public class DomainMgr extends Domain
          public
          boolean destroy()
          {
-           if(jORTEDomainMgrDestroy(this.handle)) return true;
+           if(jORTEDomainMgrDestroy(this.handle) && this.props.destroy()) return true;
            System.out.println(":j!: ORTEDomainMgrDestroy() fault..");
            return false;
          }
index 16c0dd720c4c19d8fcba2bb786ddbc1587b468a5..5a84c188dab06a4a462e69bf1916223036bb5565 100644 (file)
@@ -34,6 +34,7 @@ public class Publication {
   private org.ocera.orte.types.MessageData msgData;
 
   private boolean b;
+  private Domain appDomain;
 /*
   private int callbackEnvHandle = 0;
 */
@@ -63,6 +64,7 @@ public class Publication {
                       PublProp publProp,
                                          MessageData instance)
   {
+   this.appDomain = d;
        this.handle = jORTEPublicationCreate(d.handle,
                                             publProp.getTopic(),
                                             publProp.getTypeName(),
@@ -75,16 +77,6 @@ public class Publication {
   }
 
 
-  /**
-   * destructor
-   *
-   */
-   protected void finalize()
-   {
-     destroy();
-   }
-
-
  /**
   * destroy - Removes a publication.
   * @return False if bad publication handle, True if  succesful.
@@ -92,9 +84,22 @@ public class Publication {
   public
   boolean destroy()
   {
-    if(jORTEPublicationDestroy(this.handle)) return true;
-    System.out.println(":j!: Destroy Publication Fault!");
-    return false;
+       /* TODO vyradit vypis na nasledujici radce */
+       System.out.println(":j: publication destroy called..");
+       // destroy publication
+       if(!jORTEPublicationDestroy(this.handle)) 
+       {
+           System.out.println(":j!: publication destroy fault!");
+           return false;               
+       }
+       // destroy application domain    
+    if(!appDomain.destroy()) 
+    {
+           System.out.println(":j!: publication destroy fault!");
+           return false;                       
+    }
+    System.out.println(":j: publication destroy successfull..");
+    return true;
   }
 
 
index 6233e3a67dfff32df425e50b125d88f379f49aa5..5331cf96c77b4c69b48fb165dbc1b27ebc2bf748 100644 (file)
@@ -69,21 +69,11 @@ public class Subscription {
                                                              subsProp.getMulticastIPAddr());
   }
 
-
-  /**
-   * Destructor - destroy Subscription.
-   */
-   protected void finalize()
-   {
-     destroy();
-   }
-
-
  /**
   * Removes a Subscription.
   * @return False if bad Publication handle, True if  succesful
   */
-  protected
+  public
   boolean destroy()
   {
        /* TODO vyradit vypis na nasledujici radce */
index 4cf94e13dd45abf47d9109a0c7b442c999edfc19..6e3ddc98aa0fa9b1969e278d53f7a23514da2f3a 100644 (file)
@@ -92,13 +92,14 @@ public class Publisher {
     System.out.println(":j: start sending data:");
     System.out.println(":j: * ********************************************* *");
     
-    while(true)
+    for(int i = 0; i < 40; i++)
        {
          pub.send(hellomsg); 
          System.out.println("<<  data to send: " + hellomsg);
          JOrte.sleepMs(1000);
          System.out.println(" ");
        }
+    pub.destroy();
   } 
 
 }
index 32dea86a6e7191d8f18340eeb6f3ffebf66f394a..397c6a3ddda3f34553885a301e791c3168e8ba2c 100644 (file)
@@ -113,11 +113,7 @@ public class Subscriber {
           System.out.println(" ");
        }
 
-       // kill & clean subscriber - call Garbage Collector 
-       sub = null;
-    System.out.println("Calling gc() ...");
-       System.gc();
-
+       sub.destroy();
   }
 
 }
index b735a297d9ef61e3387dcc9c40adba56afc37761..ff46cbe70251b13293ceea5c9f317e977926bb25 100644 (file)
@@ -34,19 +34,20 @@ public class DomainProp {
 
  /* handler to C struct with default domain properties  */
   public long handle = 0;
-  private String mgrs = "";
+  private String mgrs = null;
 
  /**
   *  setProps - sets DomainProp
   */
   public void setProps(String[] mgrs) {
+          this.mgrs = "";
          for (String item : mgrs) {
                  this.mgrs += item + ":";
          }
          this.mgrs = this.mgrs.substring(0, this.mgrs.length()-1);
 
          if(!jORTEDomainPropSet(this.handle,this.mgrs))
-                 System.out.println(":j: Mgrs not set !!!");
+                 System.out.println(":j: DomainProp not set !!!");
   }
 
  /**
@@ -63,14 +64,16 @@ public class DomainProp {
      return prop;
   }
 
-  protected void finalize() {
+  public boolean destroy() {
     System.out.println(":j: DomainProp destroy called..");
 
     if(!jORTEDomainPropDestroy(this.handle,this.mgrs)) {
       System.out.println(":j: DomainProp destroy fault!");
+      return false;
     }
     else {
       System.out.println(":j: DomainProp destroy successful..");
+      return true;
     }
   }
 
index 32514d0a09e353b987e07c3ff2301ac53c7da968..a0a33a0860c1de0e9700a7d7bdb10c09a8eb3023 100644 (file)
         break;
       }
 
-      //release fellow managers string
-      (*env)->ReleaseStringUTFChars(env,mgrs,dp->mgrs);
+      if (mgrs != NULL) {
+        //release fellow managers string
+        (*env)->ReleaseStringUTFChars(env,mgrs,dp->mgrs);
+      }
 
       //free DomainProperties memory
       free(dp);