[英]Spring AOP Exception Handling
我正在嘗試檢查某些數據庫主機是否還存在。 ConfigReader類解析xml文件並獲取一些數據庫主機的列表。 如果某些主機不可用,我想查看這樣的日志:
running check for host:dbhost1
Some Exception is ...
running check for host:dbhost2
host is alive
等等。
但是,如果主機不存在,我會看到此異常。 應用程序退出,而不是記錄並引發異常。
#### running check for host:dbhost1
Exception in thread "main" java.sql.SQLException: Cannot create PoolableConnectionFactory (I/O-Fehler: The Network Adapter could not establish the connection)
at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2291)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2038)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
at com.csinfra.jdbmon.app.CheckDatabase.runCheckGroup(CheckDatabase.java:108)
這些是類:
@Component
public class ScheduleCheck {
public ConfigReader configReader;
public CheckDatabase checkDatabase;
public void runHostCheck() throws Exception {
configReader.readConfig();
checkDatabase.setConfigReader(configReader);
for(Host host:configReader.getHostMap().values()){
System.out.println("#### running check for host:"+host.getId());
checkDatabase.runCheckGroup(host, getCheckFromCheckGroup(host));
}
}
}
@Component
public class CheckDatabase {
public void runCheckGroup(Host host) throws Exception{
createConnections(host.getJdbcurl(), host.getJdbcuser(), host.getJdbcpassword(), host.getDriverclassname());
Connection connection_single = jdbcPool.getConnection();
if(connection_single!=null){
System.out.println("host is alive");
}
}
public void createConnections(String url, String username, String password, String driverClassname) throws Exception{
jdbcPool = new BasicDataSource();
jdbcPool.setDriverClassName(driverClassname);
jdbcPool.setUsername(username);
jdbcPool.setUrl(url);
jdbcPool.setPassword(password);
jdbcPool.setInitialSize(2);
jdbcPool.setCacheState(false);
jdbcPool.setMaxTotal(2);
jdbcPool.setMaxWaitMillis(6000);
jdbcPool.setMaxIdle(2);
jdbcPool.setDefaultReadOnly(true);
}
}
@Configurable
@Aspect
@Component
public class AopMethodLogger {
private static final Logger LOGGER = RootLogger.getLogger(AopMethodLogger.class);
@AfterThrowing(pointcut = "execution(* com.csinfra.app..*.*(..))", throwing = "exception")
public void logAfterThrowing(JoinPoint joinPoint, Throwable exception) throws Throwable {
LOGGER.debug("Some Exception is "+exception.getLocalizedMessage());
throw exception;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan base-package="com.csinfra.app />
<bean id="aopMethod" class="com.csinfra.app.log.AopMethodLogger" />
<bean id="scheduleCheck" class="com.csinfra.app.ScheduleCheck">
<property name="configReader" ref="configReader" />
<property name="checkDatabase" ref="checkDatabase" />
</bean>
<bean id="checkDatabase" class="com.csinfra.app.CheckDatabase">
</bean>
</beans>
CheckDatabase.runCheckGroup引發異常,並且未對其進行處理,這就是應用程序退出的原因。 用try / catch包圍對runCheckGroup的調用並記錄異常:
try{
checkDatabase.runCheckGroup(host, getCheckFromCheckGroup(host));
} catch(Exception e){
System.out.println("Exception occured :"+e);
}
與數據庫的連接數是有限的,因此僅檢查數據庫是否存在而創建一個新連接似乎相當昂貴。 最好讓spring注入所有可用的數據源:
@Autowire
List<DataSource>dataSources;
這還具有無需額外配置即可自動檢查所有已配置數據源的優勢。
而且要遍歷這些,每個人都要做類似的事情
new JdbcTemplate(dataSource).queryForInt("SELECT 1")
檢查它的生命。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.