spring quartz

<bean id="min10Runner" class="com.hzp.common.quarz.Min10Runner"></bean>
	
	<bean id="min10JobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    	<property name="targetObject" ref="min10Runner" />
    	<property name="targetMethod" value="run" />
    	<property name="concurrent" value="false" />
  	</bean>
  	
	<bean id="min10RunBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
    	<property name="jobDetail" ref="min10JobDetail" />
    	<property name="cronExpression" value="0 0/10 * * * ?" />
  	</bean>
 
// 每10分钟运行一次
public class Min10Runner {

	public void run() {

		// 监控Proxool连接池的连接状态 
		MonitorProxoolTask.monitor();
		
	}
}
 
public class MonitorProxoolTask {

	private static final Logger log = LoggerFactory.getLogger(MonitorProxoolTask.class);
	
	// 是否允许发出警告
	public static Boolean isAllowToWarning = true;
	
	// 活动的连接数警告阀值
	private static final Integer ACTIVE_COUNT_WARNING_THRESHOLD = 40;
	
	// 可得到的连接数警告阀值
	private static final Integer AVAILABLE_COUNT_WARNING_THRESHOLD = 10;

	public static void monitor() {

		try {
			
			SnapshotIF snapshot = ProxoolFacade.getSnapshot("normalDBPool", true);

			// 检查活动连接数是否超过阀值,超过了就发出警告
			int curActiveCount = snapshot.getActiveConnectionCount();
			if(isAllowToWarning && curActiveCount >= ACTIVE_COUNT_WARNING_THRESHOLD){
				String text = "【Proxool警告】现在活动的连接数为:" + curActiveCount;
				MDBClientFactory.singleton().sendMessage(text,Constants.ADMINISTRATOR_MOBILE);
				isAllowToWarning = false;
				log.error(text);
			}

			// 获得可得到的连接数
			int availableCount = snapshot.getAvailableConnectionCount();
			if(isAllowToWarning && availableCount <= AVAILABLE_COUNT_WARNING_THRESHOLD){
				String text = "【Proxool警告】现在可使用的连接数为:" + availableCount;
				MDBClientFactory.singleton().sendMessage(text,Constants.ADMINISTRATOR_MOBILE);
				isAllowToWarning = false;
				log.error(text);
			}

		} catch (Exception e) {
			log.error(ExceptionUtils.getExceptionDetails(e));
			e.printStackTrace();
		}
	}
	
}

你可能感兴趣的:(spring)