在部署时生成客户端 ID

Generate Client ID on deploy

我有一个 WildFly 集群,它应该将所有主题消息共享到不同的节点,并在一个节点离线时保留它们。
对于这种情况,我需要持久订阅者。

@MessageDriven(
    activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/Topic"),
        @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
        @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "anam123e"),
        @ActivationConfigProperty(propertyName = "clientID", propertyValue = "abcd"),
    }
)

我注意到如果我使用相同的 clientID,系统会进行负载平衡。如果我将 clientIDsubscriptionName 更改为唯一值,它就可以工作。

那么何时使用唯一的 clientID 以及何时使用 subscriptionName
我的回答是,每个节点的唯一 clientID 和节点上每个线程的 subscriptionName。

另外我想根据wildfly节点名生成一个clientID类似于:

@ActivationConfigProperty(propertyName = "clientID", propertyValue = "abcd-" + WildFly.getInstance().getNodeName()),

有没有办法实现?

有一个真正简单的解决方案可用:Property Replacement

您需要在 standalone.xml:

中启用它
<subsystem xmlns="urn:jboss:domain:ee:2.0">
    <annotation-property-replacement>true</annotation-property-replacement>
    ...
</subsystem>

新注释可能如下所示:

@MessageDriven(
    activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/Topic"),
        @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
        @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "aname"),
        @ActivationConfigProperty(propertyName = "clientID", propertyValue = "abcd-${jboss.node.name}"),
    }
)