BlazeDS+Spring+activeMQ outofmemory

一、BlazeDS内存溢出修改方案:

BlazeDS在大数据量、频繁推送数据或者频繁刷新页面、且服务器session时间超长的情况下,会导致内存泄漏。由于http协议是无状态的,所以客户端不主动通知blazeds,blazeds是不知道客户端已经断开,一直会等到session超时,且客户端每链接一次服务器,都会产生一个FlexClient,FlexClient对象会维护一个消息队列,所以解决这个问题就应该从两个地方入手,客户端和服务器。
1、flex端代码修改(主动关闭链接)
(1)
//监听页面离开事件
var connectManager:MessageConnectManager = MessageConnectManager.getInstance();
connectManager.regist();
connectManager.addEventListener(MessageConnectManager.DISCONNECT,disConnectHandler);

//页面离开 主动关闭链接
protected function disConnectHandler(e:Event):void
{
if(this.consumer && this.consumer.channelSet){
this.consumer.channelSet.disconnectAll();
}
}
2、修改blazeDS配置文件(网络中断或者用户频繁刷新页面导致内存泄漏)

修改配置文件flex-services-config.xml :
<channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
.....(2)
<flex-client-outbound-queue-processor class="processor.TimeoutOutBoundQueueProcessor">
<properties>
<!--message time out in millisecond-->
<messageTimeOut>30000</messageTimeOut>
<!--client time out in millisecond-->
<clientTimeOut>12000</clientTimeOut>
</properties>
</flex-client-outbound-queue-processor>
</properties>
</channel-definition>


<channel-definition id="my-longpolling-amf" class="mx.messaging.channels.AMFChannel">

<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amflongpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
.....(3)
<flex-client-outbound-queue-processor class="processor.TimeoutOutBoundQueueProcessor">
<properties>
<!--message time out in millisecond-->
<messageTimeOut>30000</messageTimeOut>
<!--client time out in millisecond-->
<clientTimeOut>12000</clientTimeOut>
</properties>
</flex-client-outbound-queue-processor>
</properties>
</channel-definition>

二、activeMQ内存溢出修改方案:
acitveMQ在5.4版本以后,能对慢消费者进行处理,比如blazeds+spring+activemq集成时,如果所有客户端与blazeds断开链接,但activemq还会继续向spring JMSAdapter发生数据,且spring JMSAdapter接受到消息后放消息队列里,且不通知activemq,从而导致内存溢出。
修改activemq配置文件
<broker xmlns="http://activemq.apache.org/schema/core"
brokerName="messagebus" dataDirectory="activemq/data"
destroyApplicationContextOnStop="true" persistent="true">
<destinationPolicy>
<policyMap>
<policyEntries>
........(4)
<policyEntry topic=">" producerFlowControl="false" memoryLimit="1mb" topicPrefetch="100" >
<slowConsumerStrategy>
<abortSlowConsumerStrategy checkPeriod="30000" maxSlowDuration="120000" abortConnection="false" />
</slowConsumerStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
.......(5)
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage limit="20 mb"/>
</memoryUsage>
<storeUsage>
<storeUsage limit="1 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="100 mb"/>
</tempUsage>
</systemUsage>
</systemUsage>
</broker>


配置解释:
(1):客户端监听beforeunload事件,断开链接。工具类见附件“MessageConnectManager.as ”

(2)(3):服务器端保护措施,附件“TimeoutOutBoundQueueProcessor.java ”
messageTimeOut:队列中消息超时时间,单位毫秒,超过指定时间,剔除队列;
clientTimeOut:客户端超时时间,如果超过指定时间,blazeDS没有向客户端flush数据,说明客户端已经断开链接,目前测试blazeds最大flush时间1分钟

(4):activeMQ慢消费者处理策略
topicPrefetch:消费者收到但没有应答的队列长度,超过这个长度,mq就不会向这个消费者推送数据
slowConsumerStrategy:慢消费者策略(如何衡量一个消费者慢? 在指定时间内未响应消息长度达到topicPrefetch设置或者达到到指定慢的次数,目前使用前者)
checkPeriod:检查消费者是否慢的频度
maxSlowDuration:慢持续时间
abortConnection:是否中断链接(注意 不能中断)

(5):activeMQ占用系统资源限制,超过限制,持久化到磁盘。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值