AsyncDataTreeChangeListenerBase @PreDestroy close() for easier DI 58/45258/3
authorMichael Vorburger <vorburger@redhat.com>
Tue, 6 Sep 2016 17:04:07 +0000 (19:04 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Tue, 6 Sep 2016 17:21:21 +0000 (17:21 +0000)
and new @PostConstruct protected void init() { }

This goes together with the
https://git.opendaylight.org/gerrit/#/c/44554/
and https://git.opendaylight.org/gerrit/#/c/45248/ work, and when merged
will allow to further simplify and standardize e.g.
https://git.opendaylight.org/gerrit/#/c/45249/ one more step, as shown
in https://git.opendaylight.org/gerrit/#/c/45261/

Change-Id: I536f8a31f81229b1c3baabb55d95cf3db827692f
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
commons/config-parent/pom.xml
mdsalutil/mdsalutil-api/pom.xml
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/AsyncClusteredDataChangeListenerBase.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/AsyncClusteredDataTreeChangeListenerBase.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/AsyncDataChangeListenerBase.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/AsyncDataTreeChangeListenerBase.java

index 42a7f4ab69422f5754faf33dc4ec14b2efc2c5cf..43d854917085e7591a8b746ab58e7a448d9e25e0 100644 (file)
@@ -31,6 +31,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     <model.bgp.version>2013.07.15.10-SNAPSHOT</model.bgp.version>
     <openflowplugin.version>0.4.0-SNAPSHOT</openflowplugin.version>
     <genius.ovsdb.version>1.4.0-SNAPSHOT</genius.ovsdb.version>
+    <genius.infrautils.version>1.1.0-SNAPSHOT</genius.infrautils.version>
     <liblldp.version>0.12.0-SNAPSHOT</liblldp.version>
     <karaf.shell.console.version>3.0.3</karaf.shell.console.version>
   </properties>
index 37650745c454ad091de36b6d93c7d4768487c01a..e3c952892f2df6165d5d8e59596f96eb7d2b41eb 100644 (file)
@@ -20,6 +20,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
   <artifactId>mdsalutil-api</artifactId>
   <version>0.2.0-SNAPSHOT</version>
   <packaging>bundle</packaging>
+
 <dependencies>
   <dependency>
     <groupId>org.opendaylight.openflowplugin.model</groupId>
@@ -56,6 +57,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     <artifactId>utils.config</artifactId>
     <version>${genius.ovsdb.version}</version>
   </dependency>
+  <dependency>
+      <groupId>org.opendaylight.infrautils</groupId>
+      <artifactId>inject</artifactId>
+      <version>${genius.infrautils.version}</version>
+  </dependency>
   <dependency>
     <groupId>com.google.guava</groupId>
     <artifactId>guava-testlib</artifactId>
index 3739740e9fa039a79a4fabc47754b921da8acb79..e5e4f86cd344487f048c75ab7d5a8230d330beed 100644 (file)
@@ -16,6 +16,7 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.PreDestroy;
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
@@ -128,6 +129,7 @@ public abstract class AsyncClusteredDataChangeListenerBase<T extends DataObject,
     }
 
     @Override
+    @PreDestroy
     public void close() throws Exception {
         if (listenerRegistration != null) {
             try {
index afd0aff93ab40f1f0a0c0444760421efe4fbf437..b77763d79fe0c8fddc25fdfaf246ba7ce2918811 100644 (file)
@@ -22,10 +22,10 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
-import java.util.concurrent.Callable;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.PreDestroy;
 
 public abstract class AsyncClusteredDataTreeChangeListenerBase<T extends DataObject, K extends ClusteredDataTreeChangeListener> implements ClusteredDataTreeChangeListener<T>, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(AsyncClusteredDataTreeChangeListenerBase.class);
@@ -67,12 +67,7 @@ public abstract class AsyncClusteredDataTreeChangeListenerBase<T extends DataObj
         final DataTreeIdentifier<T> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
         try {
             TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
-            listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<K>>() {
-                @Override
-                public ListenerRegistration<K> call() throws Exception {
-                    return db.registerDataTreeChangeListener(treeId, getDataTreeChangeListener());
-                }
-            });
+            listenerRegistration = looper.loopUntilNoException(() -> db.registerDataTreeChangeListener(treeId, getDataTreeChangeListener()));
         } catch (final Exception e) {
             LOG.warn("{}: Data Tree Change listener registration failed.", eventClazz.getName());
             LOG.debug("{}: Data Tree Change listener registration failed: {}", eventClazz.getName(), e);
@@ -81,6 +76,7 @@ public abstract class AsyncClusteredDataTreeChangeListenerBase<T extends DataObj
     }
 
     @Override
+    @PreDestroy
     public void close() throws Exception {
         if (listenerRegistration != null) {
             try {
index 6d94f15f42bdb755302864efde6f0bf5d3296be5..6d46e30db08c6bc5768cd646f421ca8f4c58c96c 100644 (file)
@@ -16,6 +16,7 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.PreDestroy;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
@@ -128,6 +129,7 @@ public abstract class AsyncDataChangeListenerBase<T extends DataObject, K extend
     }
 
     @Override
+    @PreDestroy
     public void close() throws Exception {
         if (listenerRegistration != null) {
             try {
index 3a509bde58ec171b73e71bb630bff45664c3e31d..16686d8e9b9feab3e3ba8d9a363dda5b348b8b7a 100644 (file)
@@ -10,10 +10,11 @@ package org.opendaylight.genius.datastoreutils;
 
 import com.google.common.base.Preconditions;
 import java.util.Collection;
-import java.util.concurrent.Callable;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
@@ -26,7 +27,9 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K extends DataTreeChangeListener> implements DataTreeChangeListener<T>, AutoCloseable {
+public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K extends DataTreeChangeListener>
+        implements DataTreeChangeListener<T>, AutoCloseable {
+
     private static final Logger LOG = LoggerFactory.getLogger(AsyncDataTreeChangeListenerBase.class);
 
     private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE = 1;
@@ -66,12 +69,7 @@ public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K ex
         final DataTreeIdentifier<T> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
         try {
             TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
-            listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<K>>() {
-                @Override
-                public ListenerRegistration<K> call() throws Exception {
-                    return db.registerDataTreeChangeListener(treeId, getDataTreeChangeListener());
-                }
-            });
+            listenerRegistration = looper.loopUntilNoException(() -> db.registerDataTreeChangeListener(treeId, getDataTreeChangeListener()));
         } catch (final Exception e) {
             LOG.warn("{}: Data Tree Change listener registration failed.", eventClazz.getName());
             LOG.debug("{}: Data Tree Change listener registration failed: {}", eventClazz.getName(), e);
@@ -79,7 +77,15 @@ public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K ex
         }
     }
 
+    /**
+     * Subclasses should override this and place initialization logic, notably calls to registerListener(), here.
+     */
+    @PostConstruct
+    protected void init() {
+    }
+
     @Override
+    @PreDestroy
     public void close() throws Exception {
         if (listenerRegistration != null) {
             try {
@@ -104,8 +110,6 @@ public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K ex
             this.changes = changes;
         }
 
-
-
         @Override
         public void run() {
             for (DataTreeModification<T> change : changes) {