# IGNITE-915: Implemented.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/c3dde572 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/c3dde572 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/c3dde572 Branch: refs/heads/ignite-456 Commit: c3dde5726a84645d6d08b69099c644d250d0dbcb Parents: 2149639 Author: vozerov-gridgain <voze...@gridgain.com> Authored: Mon May 18 11:27:44 2015 +0300 Committer: vozerov-gridgain <voze...@gridgain.com> Committed: Mon May 18 11:27:44 2015 +0300 ---------------------------------------------------------------------- .../internal/interop/InteropIgnition.java | 65 +++++++++++++++++++- .../internal/interop/InteropProcessor.java | 13 +++- 2 files changed, 76 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c3dde572/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropIgnition.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropIgnition.java b/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropIgnition.java index f245122..3ccd361 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropIgnition.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropIgnition.java @@ -25,6 +25,7 @@ import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.lang.*; import org.jetbrains.annotations.*; +import java.lang.ref.*; import java.net.*; import java.security.*; import java.util.*; @@ -52,9 +53,19 @@ public class InteropIgnition { InteropBootstrap bootstrap = bootstrap(factoryId); - return bootstrap.start(cfg, envPtr); + InteropProcessor proc = bootstrap.start(cfg, envPtr); + + trackFinalization(proc); + + return proc; } + /** + * Create configuration. + * + * @param springCfgPath Path to Spring XML. + * @return Configuration. + */ private static IgniteConfiguration configuration(@Nullable String springCfgPath) { try { URL url = springCfgPath == null ? U.resolveIgniteUrl(IgnitionEx.DFLT_CFG) : @@ -95,6 +106,58 @@ public class InteropIgnition { } /** + * Track processor finalization. + * + * @param proc Processor. + */ + private static void trackFinalization(InteropProcessor proc) { + Thread thread = new Thread(new Finalizer(proc)); + + thread.setDaemon(true); + + thread.start(); + } + + /** + * Finalizer runnable. + */ + private static class Finalizer implements Runnable { + /** Queue where we expect notification to appear. */ + private final ReferenceQueue<InteropProcessor> queue; + + /** Phantom reference to processor. */ + private final PhantomReference<InteropProcessor> proc; + + /** Cleanup runnable. */ + private final Runnable cleanup; + + /** + * Constructor. + * + * @param proc Processor. + */ + public Finalizer(InteropProcessor proc) { + queue = new ReferenceQueue<>(); + + this.proc = new PhantomReference<>(proc, queue); + + cleanup = proc.cleanupCallback(); + } + + /** {@inheritDoc} */ + @Override public void run() { + try { + queue.remove(0); + + cleanup.run(); + } + catch (InterruptedException ignore) { + // No-op. + } + } + } + + /** * Private constructor. */ private InteropIgnition() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c3dde572/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropProcessor.java index 6c55296..aa4f877 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/interop/InteropProcessor.java @@ -17,9 +17,20 @@ package org.apache.ignite.internal.interop; +import org.jetbrains.annotations.*; + /** * Interop processor. */ public interface InteropProcessor { - // No-op. + /** + * Get stop runnable to perform cleanup when interop is not longer used. + * <p/> + * <b>NOTE!</b> This runnable is called when current instance of interop processor is eligible for garbage + * collection. Therefore you should <b>never</b> store any references to Ignite internal inside it. Otherwise + * this runnable will never be called. + * + * @return Stop runnable. If {@code null} is returned, then no cleanup is expected. + */ + @Nullable public Runnable cleanupCallback(); }