【问题标题】:How to pool stateful bean?如何池化有状态的bean?
【发布时间】:2012-10-06 14:18:16
【问题描述】:

我在独立环境中使用 Spring 3.1。

我有创建编程原型 bean 的场景。

每个 bean 都有自己的状态。(它们是有状态的,具有唯一的 id 等)

创建 bean 后,我将其连接到主题(通过 DLMC 务实)。

发送到主题的每条消息都包含一个特定的 id(主题的消费者之一)

延迟和吞吐量对我来说非常重要。

因此,如果我向特定 bean 发送消息负载,我会在每条消息之间出现可笑的延迟,因为该 bean 非常忙,并且在完成当前工作之前它不会空闲。

所以我认为我每次第一次创建它时都需要创建同一个bean的池以避免这种情况。

有什么想法可以实现吗?也许有一个高级解决方案?

我正在以这种方式务实地创建那些 spring mdb:

java代码:

MyMdb myMdb= (MyMdb) beanFactory.getBean("MyMdb", id);

和xml:

<bean id="fixSessionMDB" class="com.finbird.fixgw.core.mdb.FixSessionMDB"
    scope="prototype" lazy-init="true">
    <constructor-arg ref="0" />
    <constructor-arg ref="0" />
</bean>

【问题讨论】:

    标签: java spring threadpool connection-pooling


    【解决方案1】:

    你并没有提供太多的实现细节,但是看看下面的代码 sn-p:

    <bean id="uniqueConsumerTarget" scope="prototype" lazy-init="true"/>
    
    <bean id="uniqueConsumer" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetSource">
            <bean class="org.springframework.aop.target.CommonsPoolTargetSource">
                <property name="targetClass" value="com.example.UniqueConsumer"/>
                <property name="targetBeanName" value="uniqueConsumerTarget"/>
                <property name="maxSize" value="10"/>
                <property name="maxWait" value="5000"/>
            </bean>
        </property>
    </bean>
    

    这段代码非常聪明。每次你请求"uniqueConsumer" bean 时,Spring 实际上会返回一些动态代理来拦截所有调用。一旦您尝试在该 bean 上执行任何方法,Spring 将从池中获取一个实例(或创建一个新实例)并转发给它。

    "maxSize""maxWait" 参数是自描述的,用于微调池。

    【讨论】:

    • 问题是我正在务实地创建这个原型bean。我已经编辑了我的问题。
    猜你喜欢
    • 2011-04-26
    • 2010-12-16
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多