环绕通知

用一个定时任务检测数据源连通性。将检测结果保存到全局变量。

	<bean name="checkOracleConnTask" class="com.ffcs.wlan.task.CheckOracleConnTask"/>
	<task:scheduler id="myScheduler" pool-size="10" />
	<task:scheduled-tasks scheduler="myScheduler">
		<!--检查oracle连通性-->
			<task:scheduled ref="checkOracleConnTask" method="check" cron="0/20 * * * * ?"/>
	</task:scheduled-tasks>


public class CheckOracleConnTask {

	@Resource
	private JdbcTemplate jdbcTemplate;
	Logger log=Logger.getLogger("checkDBConnLog");
	
	public void check(){
		
		try {
			log.info("begin test application conn with oracle"+"	connStat:"+GlobalData.oracleConn);
			int i=jdbcTemplate.queryForInt("SELECT COUNT(*) FROM DUAL");
			GlobalData.oracleConn=true;
			log.info("application conn with oracle ok");
		} catch (Exception e) {
			log.error("application conn with oracle fail : "+e);
			GlobalData.oracleConn=false;
		}
		
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}

做环绕通知,在插入数据库操作之前,获取数据库连通性全局变量,如果数据库不通,就不做数据库插入操作(redis中取数据,插入oracle)

<!-- 环绕通知通知  日志入库前判断数据库连通性-->
		<bean id="connOracleTestAspect" class="com.ffcs.wlan.aop.ConnOracleTestAspect"/>
		<aop:config>
			<aop:aspect ref="connOracleTestAspect">
				<aop:pointcut id="logIntoOraclePointCut" expression="execution(* com.ffcs.wlan.task.LogIntoOracleTask.insert*(..))" />
				<aop:around method="doCheckConn" pointcut-ref="logIntoOraclePointCut"/>
			</aop:aspect>
		</aop:config> 


package com.ffcs.wlan.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import com.ffcs.wlan.model.GlobalData;

public class ConnOracleTestAspect {
	
	private Logger log=Logger.getLogger(ConnOracleTestAspect.class);
	
	public void doCheckConn(ProceedingJoinPoint joinpoint) throws Throwable{
		
		String methedName=joinpoint.getSignature().getName();
		
		//如果连接oracle成功则执行日志入库
		if(GlobalData.oracleConn){
			log.info(methedName+" task begin");
				joinpoint.proceed();
			log.info(methedName+" task end");
		}else{
			log.info("conn with oracle fail, no gonna execute task "+methedName);
		}
    }
}



你可能感兴趣的:(环绕通知)