如何使用spring嵌入的activemq代理指定自定义activemq.xml?

sulc1iza  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(293)

在spring引导应用程序中,我使用嵌入式activemq代理。
我可以用以下配置:
spring boot activemq属性,
代理uri,
activemqconnectionfactorycustomizer,
但是有些属性只能通过activemq.xml配置文件获得。
如何使用spring嵌入的activemq代理指定自定义activemq.xml?

hrirmatl

hrirmatl1#

您可以在类路径上定义自定义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>

相关问题