You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Wildfly连接池与MDB的maxSession,实现运行时可扩展并控连接数?

问题背景

我在Wildfly独立版中配置了名为my-pool的连接池:

<strict-max-pool name="my-pool" max-pool-size="1" instance-acquisition-timeout="5" instance-acquisition-timeout-unit="MINUTES"/>

同时将一个MessageListener类型的MDB关联到该连接池,设置maxSession为1:

@ResourceAdapter("remote-artemis")
@MessageDriven(name = "MyMDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "a_queue"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")
})
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Pool(value = "my-pool")
public class MyMDB implements MessageListener { ... }

现有两种配置方案均存在问题:

  • 若maxSession设为固定小值,调整时需重启应用(即便用系统属性配置仍需重启),扩展性不足
  • 若maxSession设为高值(如默认15),虽可通过Wildfly管理GUI运行时调整连接池大小,但存在弊端:
    • 每个MDB需单独配置连接池
    • 即便连接池设为小值,Wildfly与ActiveMQ之间仍会创建15个消费者/连接/会话

请问如何配置Wildfly连接池与MDB的maxSession,既能在运行时调整参数,又能保证Wildfly与ActiveMQ之间仅建立适量连接?


解决方案

通过解耦MDB的maxSession与Wildfly连接池参数,结合ActiveMQ Artemis连接工厂配置和动态属性控制实现需求:

1. 配置可动态调整的连接工厂与连接池

在Wildfly中配置支持运行时修改的池化连接工厂,关联严格最大池:

<subsystem xmlns="urn:jboss:domain:messaging-activemq:16.0">
    <remote-connector name="remote-artemis" socket-binding="remote-artemis"/>
    <pooled-connection-factory name="remote-artemis-pcf" entries="java:/RemoteConnectionFactory" connectors="remote-artemis" transaction="NONE">
        <strict-max-pool name="artemis-pool" max-pool-size="5" instance-acquisition-timeout="5" instance-acquisition-timeout-unit="MINUTES"/>
    </pooled-connection-factory>
</subsystem>

该连接池的max-pool-size可通过Wildfly管理控制台或CLI在运行时修改,无需重启应用。

2. 调整MDB配置,用动态属性控制maxSession

修改MDB注解,移除@Pool注解(避免双重池化冲突),通过系统属性配置maxSession并关联上述连接工厂:

@ResourceAdapter("remote-artemis")
@MessageDriven(name = "MyMDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "a_queue"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "${wildfly.messaging.max-session:5}"),
        @ActivationConfigProperty(propertyName = "connectionFactoryLookup", propertyValue = "java:/RemoteConnectionFactory")
})
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class MyMDB implements MessageListener { ... }

后续需调整maxSession时,通过Wildfly CLI动态修改系统属性即可,无需重启:

/subsystem=core-management/system-property=wildfly.messaging.max-session:add(value="8")

3. 核心逻辑说明

  • 连接工厂的strict-max-pool直接控制Wildfly与ActiveMQ之间的实际连接数量,运行时修改max-pool-size即可动态调整连接数
  • MDB的maxSession与连接池容量匹配,确保会话数不会超出连接池承载能力,同时通过系统属性实现无重启调整
  • 移除MDB的@Pool注解,避免MDB实例池化与JMS会话池化冲突,让连接工厂池化直接控制会话/连接数量

4. 多MDB共享连接池优化

若多个MDB需共用连接池,只需在所有MDB的activationConfig中指定同一个connectionFactoryLookup值,即可共享连接工厂的连接池,避免每个MDB单独创建连接,减少不必要的开销。


内容的提问来源于stack exchange,提问作者canEE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 07:15:05