probe二次开发(二)

1、修改spring-stats.xml文件
加上
	<bean name="memoryMonitorCollector"
		class="MemoryMonitorCollectorBean">
		<property name="containerWrapper" ref="containerWrapper" />
		<property name="statsCollection" ref="statsCollection" />
		<property name="maxSeries" value="240" />
	</bean>

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

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

和在scheduler里加上
<ref local="memoryMonitorTrigger" />

2、添加类

import java.net.InetAddress;
import java.net.UnknownHostException;

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.SystemInformation;

public class MemoryMonitorCollectorBean extends BaseStatsCollectorBean{
    private ContainerWrapperBean containerWrapper;
    private String viewName;
    private Log logger = LogFactory.getLog(getClass());
    public ContainerWrapperBean getContainerWrapper() {
        return containerWrapper;
    }

    public void setContainerWrapper(ContainerWrapperBean containerWrapper) {
        this.containerWrapper = containerWrapper;
    }

    public String getViewName() {
        return viewName;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }
    public void collect() throws Exception{
        SystemInformation systemInformation = new SystemInformation();
        systemInformation.setAppBase(getContainerWrapper().getTomcatContainer().getAppBase().getAbsolutePath());
        systemInformation.setConfigBase(getContainerWrapper().getTomcatContainer().getConfigBase());
        long rate = (systemInformation.getTotalMemory()-systemInformation.getFreeMemory())* 100 / systemInformation.getMaxMemory();
        if(rate > 80){
        	sendSMS(rate);
        }
        logger.error("The memery userd is "+rate+"%!");
    }
	public void sendSMS(long memUsed) throws UnknownHostException {
		InetAddress inet = InetAddress.getLocalHost();
		String sql = "";
                //执行短信告警语句
		DaoUtils dao = new DaoUtils();
		dao.executeUpdate(sql);
	}
}

你可能感兴趣的:(java,DAO,spring,sql,tomcat)