【问题标题】:How to dispose/clean MessageListener bean programmatically如何以编程方式处理/清理 MessageListener bean
【发布时间】:2012-07-15 11:01:07
【问题描述】:

我正在使用 Spring3.1(独立环境)

我通过实现通过模板连接到主题的 MessageListener 创建了 MDB。

这个 bean 作用域是单例的。

有时我想处理掉那个 Listener Bean。当我说 dispose 时,我的意思是我希望 ioc 释放该资源并从容器中清除该 bean。(这最终将使该 bean 停止侦听消息并释放未使用的内存。)

  1. 我需要使用 getBean(..) 方法通过它的 id 检索这个 bean 来执行它的处置。我听说使用 getBean(..) 会导致内存泄漏。否则我该怎么做?

  2. 我应该为此使用单例范围还是原型范围?

【问题讨论】:

    标签: java spring spring-integration imdb


    【解决方案1】:

    我不确定您所说的 getBean() 和内存泄漏是什么意思,但是...

    如果您想完全删除它,而不仅仅是停止它,您可以在它自己的“子”应用程序上下文中声明它。将主上下文设为父上下文,以便它可以引用主上下文中的 bean(如果需要)。

    /**
     * Create a new ClassPathXmlApplicationContext with the given parent,
     * loading the definitions from the given XML files and automatically
     * refreshing the context.
     * @param configLocations array of resource locations
     * @param parent the parent context
     * @throws BeansException if context creation failed
     */
    public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
        this(configLocations, true, parent);
    }
    

    当你想删除它时;调用 context.destroy()。

    【讨论】:

    • 那么如果我需要 50 个 mdb 是否需要创建 50 个上下文?没有意义。
    • 我就是喜欢那样做;保持清洁;使用子上下文允许您使用属性占位符配置 bean 并设置上下文的环境(在 3.1 中)。如果你不想那样做,你可以使用 AbstractBeanFactory factory = (AbstractBeanFactory) ctx.getBeanFactory(); factory.destroySingleton("dmlc");
    • 如果我使用这种方式(AbstractBeanFactory 方式),性能/内存泄漏会怎样
    • 你能举出你的第一个解决方案的简单例子吗?谢谢。
    • 我不知道有任何性能/内存泄漏问题。我会看看我是否可以找到一些时间来构建一个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2014-03-15
    • 1970-01-01
    相关资源
    最近更新 更多