Is it possible to automatically clean up resources at the end of scope in Guice?

…衆ロ難τιáo~ 提交于 2019-12-04 22:48:24
Waldheinz

I faced a similar problem myself and finally rolled a Disposable interface which offers nothing but a public void dispose() method. I find this especially valuable for classes that register listeners somewhere and need to unregister them at a defined time. What I already had was my AttributeHolderScope which I blogged about so I won't repeat that part here. The only thing that is missing now is the AbstractAttributeHolder which looks like this:

/**
 * An anstract base class for implementing the {@link AttributeHolder}
 * interface which has an implementation of the attribute related methods.
 *
 * @author Matthias Treydte <waldheinz at gmail.com>
 */
public abstract class AbstractAttributeHolder
        implements AttributeHolder, Disposable {

    private final Object lock = new Object();
    private transient Map<Object, Object> attributes;

    public AbstractAttributeHolder() {
        this.attributes = new HashMap<Object, Object>();
    }

    public void replaceAttributes(Map<Object, Object> newAttr) {
        synchronized (getAttributeLock()){
            this.attributes = newAttr;
        }
    }

    @Override
    public Object getAttributeLock() {
        return this.lock;
    }

    @Override
    public final void putAttribute(Object key, Object value) {
        synchronized (getAttributeLock()) {
            attributes.put(key, value);
        }
    }

    @Override
    public final boolean hasAttribute(Object key) {
        synchronized (getAttributeLock()) {
            return attributes.containsKey(key);
        }
    }

    @Override
    public final Object getAttribute(Object key) {
        synchronized (getAttributeLock()) {
            return attributes.get(key);
        }
    }

    @Override
    public final Set<Object> getAttributes() {
        synchronized (getAttributeLock()) {
            return Collections.unmodifiableSet(
                    new HashSet<Object>(this.attributes.values()));
        }
    }

    @Override
    public void dispose() {
        synchronized (this.getAttributeLock()) {
            for (Object o : this.attributes.values()) {
                if (o instanceof Disposable) {
                    final Disposable d = (Disposable) o;
                    d.dispose();
                }
            }

            this.attributes.clear();
        }
    }
}

This class itself implements Disposable so you can have nested scopes and when you dispose an outer scope, all nested scopes and, more importantly, all injected instances that implement Disposable get cleaned up. And to precisely answer you question: I don't think that this is possible with the Scope implementations provided by Guice itself, but it can be done. Everytime I look at this code I ask myself if this can't be done in a more concise way, but then it works beautifully (at least for me).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!