在代理的 activemq 网络中禁用 jmx(spring、xbean)

发布于 2024-08-26 14:24:05 字数 5358 浏览 4 评论 0原文

由于我在这个问题上挣扎了很多,所以我发布了我的解决方案。 在代理的 activemq 网络中禁用 jmx 可消除有关 jmx 连接器注册的竞争条件。在同一台机器上启动多个activemq服务器时:

无法启动 jmx 连接器:无法绑定到 URL [rmi://localhost:1099/jmxrmi]:javax.naming.NameAlreadyBoundException:jmxrmi [根异常是 java.rmi.AlreadyBoundException:jmxrmi]

另一个问题是,即使你不会引起竞争条件,这个异常仍然可能发生。即使在等待代理正确初始化的同时启动一个又一个代理也是如此。如果一个进程由 root 作为第一个实例运行,另一个进程作为普通用户运行,则用户进程会以某种方式尝试注册自己的 jmx 连接器,尽管已经有一个。

或者当成功注册 jmx 连接器的代理出现故障时会发生另一个异常:

无法启动jmx连接器:无法绑定到URL [rmi://localhost:1099/jmxrmi]:javax.naming.ServiceUnavailableException [根异常是java.rmi.ConnectException:连接拒绝主机:localhost;嵌套异常是:java.net.ConnectException:连接被拒绝]

这些异常会导致代理网络停止工作,或根本不工作。 禁用 jmx 的技巧是,也必须在连接工厂中禁用 jmx。 文档 http://activemq.apache.org/jmx.html 并没有说这是明确需要。于是我纠结了2天,才找到解决方案:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.3.1.xsd">

<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <constructor-arg ref="connectionFactory" />
</bean>

<!-- Caching, sodass das jms template überhaupt nutzbar ist in sachen performance -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
    <property name="exceptionListener" ref="jmsExceptionListener" />
    <property name="sessionCacheSize" value="1" />
</bean>

<!--
    Jeder Client verbindet sich mit seinem eigenen broker, broker sind untereinander vernetzt. Nur wenn hier
    nochmals jmx deaktiviert wird, bleibt es auch deaktiviert...
-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="vm://broker:default?useJmx=false" />

<!--
    Broker suchen sich einen eigenen Port und sind gegenseitig verbunden, ergeben dadurch ein Grid. Dies zwar etwas
    langsamer, aber dafür ausfallsicherer. Siehe http://activemq.apache.org/networks-of-brokers.html
-->
<amq:broker useJmx="false" persistent="false">
    <!-- Wird benötigt um JMX endgültig zu deaktivieren -->
    <amq:managementContext>
        <amq:managementContext connectorHost="localhost" createConnector="false" />
    </amq:managementContext>
    <!-- Nun die normale Konfiguration für Network of Brokers -->
    <amq:networkConnectors>
        <amq:networkConnector networkTTL="1" duplex="true" dynamicOnly="true" uri="multicast://default" />
    </amq:networkConnectors>
    <amq:persistenceAdapter>
        <amq:memoryPersistenceAdapter />
    </amq:persistenceAdapter>
    <amq:transportConnectors>
        <amq:transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default" />
    </amq:transportConnectors>
</amq:broker>

</beans>

有了这个,就不需要为jvm指定-Dcom.sun.management.jmxremote=false了。这对我来说也不起作用,因为connectionfactory启动了jmx连接器。

编辑:

托尼的回答让我重新考虑配置,我发现了一个也可以工作的简化版本。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.3.2.xsd">

<!-- Caching, sodass das jms template überhaupt nutzbar ist in sachen performance -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
    <property name="exceptionListener" ref="jmsExceptionListener" />
    <property name="sessionCacheSize" value="1" />
</bean>

<!--
    Jeder Client verbindet sich mit seinem eigenen broker, broker sind untereinander vernetzt. Nur wenn hier nochmals jmx
    deaktiviert wird, bleibt es auch deaktiviert...
-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="vm://default?broker.persistent=false" />

<!--
    Broker suchen sich einen eigenen Port und sind gegenseitig verbunden, ergeben dadurch ein Grid. Dies zwar etwas
    langsamer, aber dafür ausfallsicherer. Siehe http://activemq.apache.org/networks-of-brokers.html
-->
<amq:broker useJmx="false" persistent="false">
    <amq:networkConnectors>
        <amq:networkConnector networkTTL="1" conduitSubscriptions="true" duplex="true" dynamicOnly="true"
            uri="multicast://default" />
    </amq:networkConnectors>
    <amq:persistenceAdapter>
        <amq:memoryPersistenceAdapter />
    </amq:persistenceAdapter>
    <amq:transportConnectors>
        <amq:transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default" />
    </amq:transportConnectors>
</amq:broker>

Since I've struggled a lot with this problem, I am posting my solution.
Disabling jmx in an activemq network of brokers removes race conditions about the registration of the jmx connector. When starting multiple activemq servers on the same machine:

Failed to start jmx connector: Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]

Another problem with this is, that even if you don't cause a race condition, this exception can still occur. Even when starting one broker after another while waiting for them to initialize properly in between. If one process is run by root as the first instance and the other as a normal user, somehow the user process tries to register its own jmx connector, though there already is one.

Or another exception which happens when the broker that successfully registered the jmx connector goes down:

Failed to start jmx connector: Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused]

Those exceptions cause the network of brokers to stop working, or to not work at all.
The trick to disable jmx was, that jmx had to be disabled in the connectionfactory aswell.
The documentation http://activemq.apache.org/jmx.html does not say that this is needed explicitly. So I had to struggle for 2 days until I found the solution:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.3.1.xsd">

<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <constructor-arg ref="connectionFactory" />
</bean>

<!-- Caching, sodass das jms template überhaupt nutzbar ist in sachen performance -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
    <property name="exceptionListener" ref="jmsExceptionListener" />
    <property name="sessionCacheSize" value="1" />
</bean>

<!--
    Jeder Client verbindet sich mit seinem eigenen broker, broker sind untereinander vernetzt. Nur wenn hier
    nochmals jmx deaktiviert wird, bleibt es auch deaktiviert...
-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="vm://broker:default?useJmx=false" />

<!--
    Broker suchen sich einen eigenen Port und sind gegenseitig verbunden, ergeben dadurch ein Grid. Dies zwar etwas
    langsamer, aber dafür ausfallsicherer. Siehe http://activemq.apache.org/networks-of-brokers.html
-->
<amq:broker useJmx="false" persistent="false">
    <!-- Wird benötigt um JMX endgültig zu deaktivieren -->
    <amq:managementContext>
        <amq:managementContext connectorHost="localhost" createConnector="false" />
    </amq:managementContext>
    <!-- Nun die normale Konfiguration für Network of Brokers -->
    <amq:networkConnectors>
        <amq:networkConnector networkTTL="1" duplex="true" dynamicOnly="true" uri="multicast://default" />
    </amq:networkConnectors>
    <amq:persistenceAdapter>
        <amq:memoryPersistenceAdapter />
    </amq:persistenceAdapter>
    <amq:transportConnectors>
        <amq:transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default" />
    </amq:transportConnectors>
</amq:broker>

</beans>

With this, there is no need to specify -Dcom.sun.management.jmxremote=false for the jvm. Which somehow also didn't work for me, because the connectionfactory started the jmx connector.

Edit:

Tonys answer brought me to rethinking the configuration and I found a simplified version which works aswell.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.3.2.xsd">

<!-- Caching, sodass das jms template überhaupt nutzbar ist in sachen performance -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
    <property name="exceptionListener" ref="jmsExceptionListener" />
    <property name="sessionCacheSize" value="1" />
</bean>

<!--
    Jeder Client verbindet sich mit seinem eigenen broker, broker sind untereinander vernetzt. Nur wenn hier nochmals jmx
    deaktiviert wird, bleibt es auch deaktiviert...
-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="vm://default?broker.persistent=false" />

<!--
    Broker suchen sich einen eigenen Port und sind gegenseitig verbunden, ergeben dadurch ein Grid. Dies zwar etwas
    langsamer, aber dafür ausfallsicherer. Siehe http://activemq.apache.org/networks-of-brokers.html
-->
<amq:broker useJmx="false" persistent="false">
    <amq:networkConnectors>
        <amq:networkConnector networkTTL="1" conduitSubscriptions="true" duplex="true" dynamicOnly="true"
            uri="multicast://default" />
    </amq:networkConnectors>
    <amq:persistenceAdapter>
        <amq:memoryPersistenceAdapter />
    </amq:persistenceAdapter>
    <amq:transportConnectors>
        <amq:transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default" />
    </amq:transportConnectors>
</amq:broker>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2024-09-02 14:24:06

可以将额外的参数传递给代理 URL,例如

vm://localhost?broker.persistent=false&broker.useJmx=false

Broker.useJmx=false 即可。

It's possible to pass additional parameter to broker URL, like

vm://localhost?broker.persistent=false&broker.useJmx=false

broker.useJmx=false will do the trick.

谁的年少不轻狂 2024-09-02 14:24:06

vm:// URI 连接到一个代理,该代理的brokerName 属性与URI 中使用的属性相匹配,否则它将启动一个具有该名称的嵌入式代理。因此,您可以轻松设置 vm://foo,其中

有时可能会出现竞争情况,即工厂在代理之前启动,然后启动嵌入式实例(请参阅 htp://activemq.apache.org/vm-transport-reference.html)。您可以通过使用 ConnectionFactory 的 Spring bean 配置中的 dependent-on 属性来解决这个问题。

The vm:// URI connects to a broker whose brokerName attribute matches the one used in the URI, otherwise it starts an embedded one with than name. So you could just as easily set up vm://foo where <amq:broker brokerName="foo"/>.

There may sometimes be a race condition where the factory starts before the broker and in turn starts an embedded instance (see htp://activemq.apache.org/vm-transport-reference.html). You can get around this by using the depends-on attribute in the ConnectionFactory's Spring bean configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文