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" />

你可能感兴趣的:(java,apache,spring,tomcat,xml)