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">
生产上配置如上,逐个解释作用:
- xmlns: 定义一个或者多个命名空间
- brokerName: 设置broker的名字,如果想当作单独的broker,必须是唯一的名字
- dataDirectory: 默认的存储持久化数据的目录
- schedulerSupport:开启延时投递
- persistent:持久化储存消息,和persistenceAdapter一起使用
- useShutdownHook:销毁spring上下文并停止jetty
- splitSystemUsageForProducersConsumers:生产者和消费者线程各自使用各自的内存,默认公用内存
- producerSystemUsagePortion:生产者消费内存比例
- consumerSystemUsagePortion:消费者消费内存比例
- useJmx:打开jmx远程监控
- 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>
- producerFlowControl:生产者流量控制,当broker检测到目标(destination)内存或者缓存或者文件储存超过了限制,消息的流量可以被减缓,生产者将会被阻塞直至资源可用。
- useQueueForQueueMessages:使用队列来保存死信消息
- processNonPersistent:把非持久化消息放入死信队列中
- processExpired:把过期消息放入死信队列中
- 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>
- directory:kahaDB文件位置
- journalMaxFileLength:日志最大文件大小
- enableJournalDiskSyns:确保每一个日志写后都伴随着磁盘同步
- enableIndexWriteAsyns:索引被异步刷新到磁盘
- indexCacheSize:索引页在内存中的缓存数量
- 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>
- uri:masterslave指的是建立主从关系broker
- duplex:消息流动是双向的
- dynamicOnly:持久订阅被激活时才创建对应的网络持久订阅
- networkTTL:默认1,网络中用于消息和订阅消费的broker数量,TTL转发限制次数
- prefetchSize:持有的未确认最大消息数;发送并且消费完一条消息,再发送下一条消息
- decreaseNetworkConsumerPriority:如果true,把网络中的远程消费者优先级降低为-5,如果是false,则和本地一样为0
- 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&wireFormat.maxFrameSize=104857600&wireFormat.maxInactivityDuration=60000&wireFormat.maxInactivityDurationInitialDelay=30000"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
开启端口,tcp协议端口等。设置成nio可有效提升吞吐量。
-
maximumConnections:最大连接数
-
wireFormat.maxFrameSize:
-
wireFormat.maxInactivityDuration:一段时间没消息后会出现"Channel was inactive for too long"异常
-
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中关闭这个要求。