Fork me on GitHub

ActiveMq优化

最近在研究ActiveMq,期间也遇到过很多问题,做了一些总结。

连接数配置

修改activemq.xml文件中maximumConnections参数值,默认最大连接为1000

1
2
3
4
5
6
7
8
9
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<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=100000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

如果超出最大连接限制,会提示以下错误!

1
2
3
4
5
org.apache.activemq.transport.tcp.ExceededMaximumConnectionsException: Exceeded the maximum number of allowed client connections. See the 'maximumConnections' property on the TCP transport configuration URI in the ActiveMQ configuration file (e.g., activemq.xml)
at org.apache.activemq.transport.tcp.TcpTransportServer.doHandleSocket(TcpTransportServer.java:582)[activemq-client-5.15.6.jar:5.15.6]
at org.apache.activemq.transport.tcp.TcpTransportServer.handleSocket(TcpTransportServer.java:571)[activemq-client-5.15.6.jar:5.15.6]
at org.apache.activemq.transport.tcp.TcpTransportServer$1.run(TcpTransportServer.java:497)[activemq-client-5.15.6.jar:5.15.6]
at java.lang.Thread.run(Thread.java:748)[:1.8.0_181]

JVM虚拟机参数配置

当连接比较大,超过1万个长连接以上,很容易出现内存故障,导致MQ不可用,可以通过指定JVM参数解决。

  1. 修改启动脚本activemq
  2. 搜索文件中启动命令位置invoke_start
  3. ACTIVEMQ_OPTS参数中增加JVM参数-Xmx4096m -Xms4096m -Xss512k -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=1024m

修改后的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
invoke_start(){
if ( checkRunning );then
PID="`cat $ACTIVEMQ_PIDFILE`"
echo "INFO: Process with pid '$PID' is already running"
exit 0
fi
ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS $ACTIVEMQ_SUNJMX_START $ACTIVEMQ_SSL_OPTS -Xmx4096m -Xms4096m -Xss512k -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=1024m -Djava.awt.headless=true -Djava.io.tmpdir=\"${ACTIVEMQ_TMP}\""
echo "INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details"
invokeJar "$ACTIVEMQ_PIDFILE" start
exit "$?"
}

大并发会出现以下异常,可以通过以上解决:

1
java.lang.OutOfMemoryError: GC overhead limit exceeded

FallSea wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!