ActiveMQ配置文件解析

本文详细解析了ActiveMQ的配置文件,包括credentials.properties、logQuery、Broker配置如destinationPolicy、managementContext、persistenceAdapter等。讨论了如何进行权限控制、持久化、系统资源使用、网络连接及传输端口设定,以及如何处理日志和异常情况,旨在帮助理解ActiveMQ的工作原理和最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActiveMQ配置文件

credentials.properties

		<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:${activemq.conf}/credentials.properties</value>
        </property>
    </bean>

允许我们使用系统属性当作变量在这个配置文件中(credentials.properties)如下:

activemq.username=system
activemq.password=manager
guest.password=password

logQuery

		<!-- Allows accessing the server log -->
    <bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
          lazy-init="false" scope="singleton"
          init-method="start" destroy-method="stop">
    </bean>

允许我们接触到服务器日志,单例模式

Broker 配置

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true" persistent="true" useShutdownHook="false" splitSystemUsageForProducersConsumers="true" producerSystemUsagePortion="50" consumerSystemUsagePortion="50" useJmx="true" schedulePeriodForDestinationPurge="86400000">

生产上配置如上,逐个解释作用:

  1. xmlns: 定义一个或者多个命名空间
  2. brokerName: 设置broker的名字,如果想当作单独的broker,必须是唯一的名字
  3. dataDirectory: 默认的存储持久化数据的目录
  4. schedulerSupport:开启延时投递
  5. persistent:持久化储存消息,和persistenceAdapter一起使用
  6. useShutdownHook:销毁spring上下文并停止jetty
  7. splitSystemUsageForProducersConsumers:生产者和消费者线程各自使用各自的内存,默认公用内存
  8. producerSystemUsagePortion:生产者消费内存比例
  9. consumerSystemUsagePortion:消费者消费内存比例
  10. useJmx:打开jmx远程监控
  11. schedulePeriodForDestinationPurge:定期删除过期的Topic和Queue

destinationPolicy

<destinationPolicy>
		<policyMap>
        <policyEntries>
          	<policyEntry queue=">" producerFlowControl="true" memoryLimit="256mb" queuePrefetch="200" gcInactiveDestinations="true" inactiveTimeoutBeforeGC="86400000" enableAudit="false">
              	<!-- 配置死信队列策略 -->
              	<deadLetterStrategy>
                  	<individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true" processNonPersistent="true" processExpired="true" expiration="60480000"/>
              	</deadLetterStrategy>
              
              	<!--配置replayWhenNoConsumers,能防止消息回流	-->
            		<networkBridgeFilterFactory>
              			<conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/>
            		</networkBridgeFilterFactory>
          	</policyEntry>
          	
          	<!-- 这里的 > 是通配符,默认匹配所有队列 -->
						<policyEntry queue=">" enableAudit="false">
              	<!-- 待处理消息限制策略,-1表示不限制 -->
            		<pendingMessageLimitStrategy>
                  	<constantPendingMessageLimitStrategy limit="-1"/>
              	</pendingMessageLimitStrategy>
              	<!-- 严格按照顺序发送消息 -->
              	<dispatchPolicy>
                  	<strictOrderDispatchPolicy/>
              	</dispatchPolicy>
          	</policyEntry>
        </policyEntries>
		</policyMap>
</destinationPolicy>
  1. producerFlowControl:生产者流量控制,当broker检测到目标(destination)内存或者缓存或者文件储存超过了限制,消息的流量可以被减缓,生产者将会被阻塞直至资源可用。
  2. useQueueForQueueMessages:使用队列来保存死信消息
  3. processNonPersistent:把非持久化消息放入死信队列中
  4. processExpired:把过期消息放入死信队列中
  5. gcInactiveDestinations:清理不活跃的queue和topic

managementContext

<managementContext>
		<managementContext createConnector="true" connectorPort="1099"/>
</managementContext>

managementContext是用来配置AMQ如何暴露给JMX,默认使用MBean

persistenceAdapter

<persistenceAdapter>
		<kahaDB directory="${activemq.data}/kahadb" journalMaxFileLength="32mb" enableJournalDiskSyns="true" enableIndexWriteAsyns="true" indexCacheSize="5000" indexWriteBatchSize="2000"/>
</persistenceAdapter>
  1. directory:kahaDB文件位置
  2. journalMaxFileLength:日志最大文件大小
  3. enableJournalDiskSyns:确保每一个日志写后都伴随着磁盘同步
  4. enableIndexWriteAsyns:索引被异步刷新到磁盘
  5. indexCacheSize:索引页在内存中的缓存数量
  6. indexWriteBatchSize:一批索引写的数量

plugins

<plugins>
		<loggingBrokerPlugin logProducerEvents="true" logConsumerEvents="true"/>
  	<simpleAuthenticationPlugin>
    		<users>
          	<authenticationUser username="admin" password="admin" groups="users, admins"/>
      	</users>
  	</simpleAuthenticationPlugin>
</plugins>

设置安全验证信息,配置连接ActiveMQ的账号密码

loggingBrokerPlugin 设置打印和消费者和生产者有关的日志,详见:http://activemq.apache.org/logging-interceptor

systemUsage

<systemUsage>
		<systemUsage>
				<memoryUsage>
						<memoryUsage percentOfJvmHeap="80" /></memoryUsage>
						<storeUsage>
								<storeUsage limit="10 gb"/>
						</storeUsage>
						<tempUsage>
								<tempUsage limit="8 gb"/>
						</tempUsage>
		</systemUsage>
</systemUsage>

systemUsage 控制最大broker使用空间,当broker开始关闭缓存或者减速消费者。

networkConnectors

<networkConnectors>
		<networkConnector name="amqbroker2" uri="masterslave:(tcp:host,tcp:host2)" duplex="true" dynamicOnly="true" networkTTL="2" prefetchSize="1" decreaseNetworkConsumerPriority="true" conduitSubscription="false"/>
</networkConnectors>
  1. uri:masterslave指的是建立主从关系broker
  2. duplex:消息流动是双向的
  3. dynamicOnly:持久订阅被激活时才创建对应的网络持久订阅
  4. networkTTL:默认1,网络中用于消息和订阅消费的broker数量,TTL转发限制次数
  5. prefetchSize:持有的未确认最大消息数;发送并且消费完一条消息,再发送下一条消息
  6. decreaseNetworkConsumerPriority:如果true,把网络中的远程消费者优先级降低为-5,如果是false,则和本地一样为0
  7. conduitSubscription:是否区分对待两个broker上的消费者,消息一半给broker1,另一半给broker2;而不是根据broker上的消费者数量按比例分配

transportConnectors

				<transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=10000&amp;wireFormat.maxFrameSize=104857600&amp;wireFormat.maxInactivityDuration=60000&amp;wireFormat.maxInactivityDurationInitialDelay=30000"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

开启端口,tcp协议端口等。设置成nio可有效提升吞吐量。

  1. maximumConnections:最大连接数

  2. wireFormat.maxFrameSize:

  3. wireFormat.maxInactivityDuration:一段时间没消息后会出现"Channel was inactive for too long"异常

  4. wireFormat.maxInactivityDurationInitialDelay:

    javax.jms.JMSException: Wire format negociation timeout: peer did not send his wire format.
    

    出现如下异常等时候,配置 wireFormat.maxInactivityDurationInitialDelay=60000 即可

shutdownHooks

				<shutdownHooks>
            <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
        </shutdownHooks>

销毁spring上下文,并且停止jetty

jetty.xml

<import resource="jetty.xml"/>

导入外部资源 jetty.xml,允许使用activeMQ控制台,REST 和 Ajax API以及demo。web consoles默认要求登陆,可以在jetty.xml中关闭这个要求。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值