如何使用 Spring 嵌入式 ActiveMQ 代理指定自定义 activemq.xml?

Posted

技术标签:

【中文标题】如何使用 Spring 嵌入式 ActiveMQ 代理指定自定义 activemq.xml?【英文标题】:How to specify a custom activemq.xml with a Spring embedded ActiveMQ broker? 【发布时间】:2021-05-30 04:02:35 【问题描述】:

在 Spring Boot 应用程序中,我使用嵌入式 ActiveMQ 代理。

我可以配置它:

Spring Boot ActiveMQ properties, Broker URI, ActiveMQConnectionFactoryCustomizer,

但某些属性只能通过activemq.xml configuration file 获得。

【问题讨论】:

【参考方案1】:

您可以在您的类路径上定义自定义 ActiveMQ 配置并加载它:

@Profile("embedded-activemq")
@ImportResource("classpath:embedded-activemq.xml")
@Configuration
  public class EmbeddedActiveMQConfig 

ActiveMQ 需要 Spring NamespaceHandler(spring-boot-starter-activemq 中不存在):

<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-spring</artifactId>
</dependency>

然后,您可以使用与您的 ActiveMQ 版本对应的官方activemq.xml 并使其适应 Spring 嵌入式上下文,例如:

<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
    <beans xmlns="http://www.springframework.org/schema/beans"
             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.15.13.xsd
        ">

    <!--
        The <broker> element is used to configure the ActiveMQ broker.
    -->
    <broker xmlns="http://activemq.apache.org/schema/core"
            brokerName="localhost"
            persistent="false"
            useJmx="false"
            enableStatistics="false">

        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" >
                    <!-- The constantPendingMessageLimitStrategy is used to prevent
                         slow topic consumers to block producers and affect other consumers
                         by limiting the number of messages that are retained
                         For more information, see:

                         http://activemq.apache.org/slow-consumer-handling.html

                    -->
                  <pendingMessageLimitStrategy>
                    <constantPendingMessageLimitStrategy limit="1000"/>
                  </pendingMessageLimitStrategy>
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy>

          <!--
            The systemUsage controls the maximum amount of space the broker will
            use before disabling caching and/or slowing down producers. For more information, see:
            http://activemq.apache.org/producer-flow-control.html
          -->
          <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage percentOfJvmHeap="70"/>
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="100 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="1 gb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>

        <!-- destroy the spring context on shutdown -->
        <shutdownHooks>
            <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
        </shutdownHooks>

    </broker>

</beans>

【讨论】:

以上是关于如何使用 Spring 嵌入式 ActiveMQ 代理指定自定义 activemq.xml?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用嵌入式 ActiveMQ Artemis 为 Spring Boot 配置 max-delivery-attempts?

当两个应用程序都使用嵌入式activemq时,如何将Jms消息从一个spring-boot应用程序发送到另一个应用程序

如何在 Spring Boot 中设置 ActiveMQ 端口?

如何在 ActiveMq 嵌入式代理上启用 Web 控制台

使用 spring 集成自动配置 ActiveMQ

使用 Spring Boot 配置 ActiveMQ