ActiveMQ
所有特性配置在这里: http://activemq.apache.org/features.html
使用文档: http://activemq.apache.org/using-activemq.html
服务端调整参数
1. 启动脚本./bin/activemq的jvm内存设置
# Set jvm memory configuration
ACTIVEMQ_OPTS_MEMORY="-Xms2048M -Xmx4096M"
2. 绑定ip和设置最大链接数
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and framesize to 100MB -->
<transportConnector name="openwire"uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<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协议需要修改”openwire” 项.
3. Conf/activemq.xml配置
内存,硬盘存储空间配置
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsagepercentOfJvmHeap="70" />
<memoryUsagelimit="64 mb"/>
</memoryUsage>
<storeUsage>
<storeUsagelimit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsagelimit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
如果是嵌入式(embedded)方式使用的话,可以new一个SystemUsage来添加到BrokerService中去。这里配置的memoryUsage一定要小于jvm中设置的数量。
5.9.0版本,如果设置的 memoryUsagelimit大于实际可用java heap size值,比如默认启动 xmx=1G,那么实际的java heap size大概是910M,则broker启动时会log.error提示设置内存出错,然后强制使用实际java heap size* 70% = 637M作为memoryUsagelimit。
一般不用设置memoryUsagelimit,自动设置成jvm的比例值就可以了
4. 生成者流程控制
主要是对对列内存控制,意思是当broker探测到queue/topic的内存, 临时文件,文件存储,到达这个queue/topic上限, 消息接收会慢下来,producer将会被住阻塞,并且受到JMSException, 直到有可用空间。Activemq达到最大空间限制后,请不要误解为生产者挂起了,其实是在等待可用空间
● 同步发送消息会默认启用生产者流程控制, 而且会应用到持久化的对列中,除非你启用useAsyncSend
● 如果使用异步发送,如果broker达到空间上线,生产者不会受到任何通知。但如果想要多的得到通知需要在客户端代码做如下配置
ActiveMQConnectionFactory connctionFactory = ...
connctionFactory.setProducerWindowSize(1024000);
The ProducerWindowSize is the maximum number of bytes of data that aproducer will transmit to a broker before waiting for acknowledgment messagesfrom the broker that it has accepted the previously sent messages.
5. Master-slave配置
下面是slave配置,master不需要做任何配置
A master broker doesn't need any special configuration - it's a normalbroker until a slave broker attaches itself.
To identify a broker as a slave - there is just one property to set (see below)as this example shows - so configuration is nice and easy:
<broker masterConnectorURI="tcp://masterhost:62001" shutdownOnMasterFailure="false"> <persistenceAdapter> <journaledJDBC journalLogFiles="5" dataDirectory="${activemq.base}/data/broker2" /> </persistenceAdapter> <transportConnectors> <transportConnector uri="tcp://slavehost:61616"/> </transportConnectors> </broker> |
Client url :failover:
//(
tcp://masterhost:61616,
tcp://slavehost:61616)?randomize=false
The randomize property just disables randomness sothat the transport will always try the master first, then the slave if it can'tconnect to that. Note that the slave does not accept connections until itbecomes the master
randomize 字段如果是false,客户端总是会去建立master链接,直到slave变成master
详细说明: http://activemq.apache.org/pure-master-slave.html
6. 客户端配置
Uri链接:
l 链接配置
如: tcp://localhost:61616?jms.useAsyncSend=true
Connection Options
Thefollowing options should be prefixed with jms. when used on a URI when connecting toa broker.
Option Name | Default Value | Description |
alwaysSessionAsync | true | If this flag is set then a separate thread is used for dispatching messages for each Session in the Connection. However, a separate thread is always used if there is more than one session, or the session isn't in auto acknowledge or dups ok mode |
clientID | null | Sets the JMS clientID to use for the connection |
closeTimeout | 15000ms | Sets the timeout before a close is considered complete. Normally a close() on a connection waits for confirmation from the broker; this allows that operation to timeout to save the client hanging if there is no broker. |
copyMessageOnSend | true | Should a JMS message be copied to a new JMS Message object as part of the send() method in JMS. This is enabled by default to be compliant with the JMS specification. You can disable it if you do not mutate JMS messages after they are sent for a performance boost. |
disableTimeStampsByDefault | false | Sets whether or not timestamps on messages should be disabled or not. If you disable them it adds a small performance boost. |
dispatchAsync | false | Should the broker dispatch messages asynchronously to the consumer. |
nestedMapAndListEnabled | true | Enables/disables whether or not Structured Message Properties and MapMessages are supported so that Message properties and MapMessage entries can contain nested Map and List objects. Available since version 4.1 onwards |
objectMessageSerializationDefered | false | When an object is set on an ObjectMessage, the JMS spec requires the object to be serialized by that set method. Enabling this flag causes the object to not get serialized. The object may subsequently get serialized if the message needs to be sent over a socket or stored to disk. |
optimizeAcknowledge | false | Enables an optimised acknowledgement mode where messages are acknowledged in batches rather than individually. Alternatively, you could use Session.DUPS_OK_ACKNOWLEDGE acknowledgement mode for the consumers which can often be faster. WARNING enabling this issue could cause some issues with auto-acknowledgement on reconnection |
optimizeAcknowledgeTimeOut | 300ms | if > 0, specifies the max time between batch acks when optimizeAcknowledge is enabled. (since 5.6) |
optimizedMessageDispatch | true | If this flag is set then an larger prefetch limit is used - only applicable for durable topic subscribers |
useAsyncSend | false | Forces the use of Async Sends which adds a massive performance boost; but means that the send() method will return immediately whether the message has been sent or not which could lead to message loss. |
useCompression | false | Enables the use of compression of the message bodies |
useRetroactiveConsumer | false | Sets whether or not retroactive consumers are enabled. Retroactive consumers allow non-durable topic subscribers to receive old messages that were published before the non-durable subscriber started. |
warnAboutUnstartedConnectionTimeout | 500 | Enables the timeout in milliseconds from a connection creation to when a warning is generated if the connection is not properly started viaConnection.start() and a message is received by a consumer. It is a very common gotcha to forget to start the connection and then wonder why no messages are delivered so this option makes the default case to create a warning if the user forgets. To disable the warning just set the value to < 0 (say -1). |
auditDepth | 2048 | The size of the message window that will be audited (for duplicates and out of order messages |
auditMaximumProducerNumber | 64 | Maximum number of producers that will be audited |
alwaysSyncSend | false | When true a MessageProducer will always use Sync sends when sending a Message even if it is not required for the Delivery Mode |
Nested Options
You canalso configure nested objects on the connection object using the givenprefixes. See the javadoc for a breakdown of each individual property.
Option Name | Object configured | See Also |
jms.blobTransferPolicy.* | ||
jms.prefetchPolicy.* | ||
jms.redeliveryPolicy.* |
详细配置文档: http://activemq.apache.org/connection-configuration-uri.html
l 失败轮询: failover:(tcp://broker1:61616,tcp://broker2:61616,tcp://broker3:61616)
Transport Options
Option Name | Default Value | Description |
initialReconnectDelay | 10 | How long to wait before the first reconnect attempt (in ms) |
maxReconnectDelay | 30000 | The maximum amount of time we ever wait between reconnect attempts (in ms) |
useExponentialBackOff | true | Should an exponential backoff be used btween reconnect attempts |
reconnectDelayExponent | 2.0 | The exponent used in the exponential backoff attempts |
maxReconnectAttempts | -1 | 0 | From version 5.6 onwards: -1 is default and means retry forever, 0 means don't retry (only try connection once but no retry). |
startupMaxReconnectAttempts | 0 | If not 0, then this is the maximum number of reconnect attempts before an error is sent back to the client on the first attempt by the client to start a connection, once connected the maxReconnectAttempts option takes precedence. |
randomize | true | use a random algorithm to choose the the URI to use for reconnect from the list provided |
backup | false | initialize and hold a second transport connection - to enable fast failover |
timeout | -1 | Enables timeout on send operations (in miliseconds) without interruption of reconnection process |
trackMessages | false | keep a cache of in-flight messages that will flushed to a broker on reconnect |
maxCacheSize | 131072 | size in bytes for the cache, if trackMessages is enabled |
updateURIsSupported | true | Determines whether the client should accept updates to its list of known URIs from the connected broker. Added in ActiveMQ 5.4 |
updateURIsURL | null | A URL (or path to a local file) to a text file containing a comma separated list of URIs to use for reconnect in the case of failure. Added in ActiveMQ 5.4 |
nested.* | null | Extra options to add to the nested URLs. Added in ActiveMQ 5.9 |
warnAfterReconnectAttempts.* | 10 | After every N reconnect attempts log a warning to indicate there is no connection but that we are still trying, set to <= 0 to disable. Added in ActiveMQ 5.10 |
reconnectSupported | true | Determines whether the client should respond to broker ConnectionControl events with a reconnect (see:rebalanceClusterClients) |
失败轮询详细配置: http://activemq.apache.org/failover-transport-reference.html
l 消费者配置
Consumer Options
Option Name | Default Value | Description |
consumer.prefetchSize | variable | The number of message the consumer will prefetch. |
consumer.maximumPendingMessageLimit | 0 | Use to control if messages for non-durable topics are dropped if a slow consumer situation exists. |
consumer.noLocal | false | Same as the noLocal flag on a Topic consumer. Exposed here so that it can be used with a queue. |
consumer.dispatchAsync | true | Should the broker dispatch messages asynchronously to the consumer. |
consumer.retroactive | false | Is this a Retroactive Consumer. |
consumer.selector | null | JMS Selector used with the consumer. |
consumer.exclusive | false | Is this an Exclusive Consumer. |
consumer.priority | 0 | Allows you to configure a Consumer Priority. |
Example
|
7.
8.
9.