probe二次开发(六)

定时清理垃圾session
增加java类:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.Context;
import org.apache.catalina.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jstripe.tomcat.probe.beans.ContainerWrapperBean;
import org.jstripe.tomcat.probe.beans.stats.collectors.BaseStatsCollectorBean;
import org.jstripe.tomcat.probe.model.ApplicationSession;
import org.jstripe.tomcat.probe.tools.ApplicationUtils;
@SuppressWarnings("unchecked")
public class ExpireSessionsSchedule extends BaseStatsCollectorBean {
protected final Log logger = LogFactory.getLog(getClass());
private List contextNames = new ArrayList();
public ContainerWrapperBean containerWrapper;
private Map<String,String> conf = new HashMap<String,String>();
public void setContextNames(List contextNames) {
this.contextNames = contextNames;
}
public void setContainerWrapper(ContainerWrapperBean containerWrapper) {
this.containerWrapper = containerWrapper;
}
public ContainerWrapperBean getContainerWrapper() {
return containerWrapper;
}
public void setConf(Map<String, String> conf) {
this.conf = conf;
}
private int idleConf,ageConf,idle,age;
public void collect() throws Exception{
Context context = null;
idleConf = Integer.valueOf(conf.get("idle"));
ageConf = Integer.valueOf(conf.get("age"));
if (contextNames != null) {
for (int i = 0; i < contextNames.size(); i++) {
context = containerWrapper.getTomcatContainer().findContext(contextNames.get(i).toString());
if (context.getManager() != null ) {
Session sessions[] = context.getManager().findSessions();
for (int j = 0; j < sessions.length; j++) {
Session session = sessions[i];
ApplicationSession appSession = ApplicationUtils.getApplicationSession(session, false, false);
if (appSession != null) {
idle = Integer.valueOf((appSession.getIdleTime()/60000)+"");
age = Integer.valueOf((appSession.getAge()/60000)+"");
if(appSession.isSerializable() || idle >= idleConf || age >= ageConf){
logger.info("serializable=="+appSession.isSerializable()+";idle=="+idle+";age=="+age);
if (session != null) {
logger.info("expired session=="+session.getId());
session.expire();
}
}
}
}
}
}
}
}

}

修改配置文件spring-stats.xml
增加配置

<bean name="sessionScheduleCollector"
class="*.*.*.ExpireSessionsSchedule">
<property name="containerWrapper" ref="containerWrapper" />
<property name="contextNames">
<list>
<value>/BIOP</value>
</list>
</property>
<property name="conf">
<map>
<entry key="idle"><value>2</value></entry>
<entry key="age"><value>180</value></entry>
</map>
</property>
</bean>

<bean id="sessionScheduleJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="sessionScheduleCollector" />
<property name="targetMethod" value="collect" />
<property name="concurrent" value="false" />
</bean>

<bean id="sessionScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="sessionScheduleJobDetail" />
<property name="cronExpression">
<value>0 0/2 * * * ?</value>
</property>
</bean>


并在scheduler中加上

<ref local="sessionScheduleTrigger" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值