接着上一篇继续,瞬时的服务器硬件使用数据是应用在页面展示中的。如果要做统计还需要定时执行任务获取多个时间点上的数据,以便进行进一步的分析和总结。这里用到得就是spring的定时任务。
需要注意的点是要带quartz-1.6.0.jar这个包。
定时任务的管理是在XML中进行的,这里独立出一个applicationContext_quartz.xml。
基本的监控要完成三项任务:
1.定时采集数据。
2.根据采集的数据进行分析得到报告。
3.把报告作为EMAIL的内容(或附件)发送给指定的人。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="countInfo"/>
<ref bean="createReport"/>
<ref bean="sendEmail"/>
</list>
</property>
</bean>
<!-- 定时查询系统资源,并写入文件 -->
<bean id="contInfoJobTask" class="com.xxx.monitor.task.CountServerInfoTask"></bean>
<bean id="contInfoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="contInfoJobTask"/>
</property>
<property name="targetMethod">
<value>countInfo</value>
</property>
</bean>
<bean id="countInfo" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="contInfoJob"/>
</property>
<property name="cronExpression">
<value>0 10 0/1 * * ?</value><!-- 0:10,1:10,2:10.... -->
</property>
</bean>
<!-- 定时从文件中统计数字,生成报告 -->
<bean id="reportJobTask" class="com.xxx.monitor.task.CreateReportTask"></bean>
<bean id="reportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="reportJobTask"/>
</property>
<property name="targetMethod">
<value>createReport</value>
</property>
</bean>
<bean id="createReport" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="reportJob"/>
</property>
<property name="cronExpression">
<value>0 5 0 * * ?</value> <!-- 00:05 -->
</property>
</bean>
<!-- 定时发送邮件 -->
<bean id="emailJobTask" class="com.xxx.monitor.task.SendMailTask"></bean>
<bean id="emailJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="emailJobTask"/>
</property>
<property name="targetMethod">
<value>sendMail</value>
</property>
</bean>
<bean id="sendEmail" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="emailJob"/>
</property>
<property name="cronExpression">
<value>0 15 0 * * ?</value> <!-- 0:15 -->
</property>
</bean>
</beans>
本文介绍如何使用Spring框架结合Quartz实现服务器资源的定时监控、数据分析及邮件报告发送。具体包括配置定时任务、采集服务器硬件使用数据、生成分析报告并定时通过邮件发送。
761

被折叠的 条评论
为什么被折叠?



