Quartz和Spring整合

1、环境:Spring3.1+Quartz1.8(Spring3.*和Quartz2.*整合报错,改用Quartz1.8版本)

2、查询剩余表空间Job:

package com.sxit.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * @功能:查询表空间Job
 * @作者: smile
 * @时间:2013-4-8 下午4:53:22
 * @版本:1.0
 */
public class TableSpaceSmsAlarmJob extends QuartzJobBean{
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
	
		TableSpaceSmsAlarm alarm = new TableSpaceSmsAlarm();
		alarm.queryTableSpace();
	}
}

 3、处理类TableSpaceSmsAlarm :

package com.sxit.job;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.sxit.util.ConnectionUtil;
import com.sxit.util.SendPwdService;

/**
 * @功能:表空间短信报警
 * @作者: smile
 * @时间:2013-4-8 下午3:51:35
 * @版本:1.0
 */
public class TableSpaceSmsAlarm {

	private static final Logger logger = Logger.getLogger(TableSpaceSmsAlarm.class);
	
	/** 表空间警告阀值 */
	private static int tableSpaceSize;
	/** 手机号 */
	private static String mobileStr;
	
	public TableSpaceSmsAlarm(){
		init();
	}
	
	/**
	 * @功能:初始化参数
	 */
	public void init(){
		Properties pro = new Properties();
		InputStream is = null;
		try {
			String separator = System.getProperty("file.separator");
			URL url = Thread.currentThread().getContextClassLoader().getResource("");
			File f = new File(url.getPath() + separator + "config.properties");
			is = new FileInputStream(f);
			pro.clear();
			pro.load(is);
			tableSpaceSize = Integer.parseInt(pro.get("tableSpaceSize").toString());
			mobileStr = pro.getProperty("mobileStr").toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				} finally{
					is = null;
				}
		}
	}

	
	/**
	 * @功能:查询空闲表空间
	 */
	public void queryTableSpace(){
		
		Connection con = ConnectionUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		int freeSpace = 0;
		
		String sql = "select round(sum(bytes)/1024/1024) freespace "
						+" from dba_free_space "
						+" where  tablespace_name='LNXXT'";
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next()){
				freeSpace = rs.getInt("freespace");
				//小于当前阀值  发送短信
				if(freeSpace <= tableSpaceSize){
					SendPwdService send = new SendPwdService();
					if(mobileStr!=null && mobileStr.length()>0){
						String[] mobile = mobileStr.split(",");
						for(String phone : mobile){
							send.sendSMS(con, phone, freeSpace);
						}
					}else{
						logger.info("配置文件出错,手机号码为空!");
						//系统退出
						System.exit(0);
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			ConnectionUtil.freeAll(con, ps, rs);
		}
	}
}

 4、配置文件config.properties:

#表空间大小警告阀值
tableSpaceSize=10240

#手机号 以逗号分隔
mobileStr=158********,159********,150********

 5、Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<!-- 定义调度工作任务 -->
	<bean id="alarmBean" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.sxit.job.TableSpaceSmsAlarmJob" />
	</bean>
	
	<bean id="alarmTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="alarmBean" />
		<!-- 每天7点到23这个时间段 每隔一小时执行job一次 -->
		<property name="cronExpression" value="0 0/59 7-23 * * ?" />
	</bean>

	<!-- 
	<bean id="alarmTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="alarmBean" />
		<property name="repeatInterval" value="10000" />
	</bean> 
	-->

	<!-- 启动调度 -->
	<bean id="scheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
		<property name="triggers">
			<list>
				<ref bean="alarmTrigger" />
			</list>
		</property>
	</bean>

</beans>

 6、web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 定义Spring配置文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 对Spring容器进行实例化 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

猜你喜欢

转载自luan.iteye.com/blog/1844099