java:datasource存活自动检测并切换
- DataSourceCheckUtil.java
- public static void checkDataSource(Map<Integer, String> dataSoureMap) throws Exception {
- log .info("[method] checkDataSource(Map<Integer, String> dataSoureMap, int index) index = "
- + DataSourceId.dataSourceId); int size = dataSoureMap.size();
- if (DataSourceId.dataSourceId >= size) { throw new OverFlowExcetion("数据边界溢出");
- } else { if (DataSourceId.dataSourceId < size - 1) {
- DataSourceId.dataSourceId = DataSourceId.dataSourceId + 1; } else {
- DataSourceId.dataSourceId = 0; }
- } }
- DataSourceCheckTask.java private ServletContext servletContext;
- private boolean checkDataSourceAlive(String dataSourceName) { WebApplicationContext wac = WebApplicationContextUtils
- .getWebApplicationContext(servletContext); boolean result = true;
- Connection cc = null; try {
- DataSource ds = (DataSource) wac.getBean(dataSourceName); cc = ds.getConnection();
- if (cc.isClosed() || cc == null) { result = false;
- } } catch (SQLException e) {
- result = false; log.error(e);
- } finally { try {
- cc.close(); } catch (SQLException e) {
- log.error(e); }
- }
- return result; }
- @SuppressWarnings("static-access") public void findAliveDataSource() {
- log.info("[method] findAliveDataSource()"); int total = 0;
- Map<Integer, String> dataSourceMap = DataSourceMap.getInstance().dataSoureMap; int size = dataSourceMap.size();
- while (total < size) {
- total++; String dataSourceName = dataSourceMap
- .get(DataSourceId.dataSourceId); if (checkDataSourceAlive(dataSourceName)) {
- log.info("当前存活的data source id = " + DataSourceId.dataSourceId); break;
- } else { try {
- DataSourceCheckUtil.checkDataSource(dataSourceMap); } catch (Exception e) {
- log.error(e); }
- } }
- }
- public void setServletContext(ServletContext arg0) {
- this.servletContext = arg0; }
- DynamicDataSource public class DynamicDataSource extends AbstractRoutingDataSource {
- @Override
- protected Object determineCurrentLookupKey() { return DataSourceId.dataSourceId;
- }
- }
- spring config file
- <bean id="dataSource0"
- class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"
- value="${jdbc.driverClassName}"> </property>
- <property name="url" value="${jdbc.url.36}"></property> <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property> </bean>
- <bean id="dataSource1"
- class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"
- value="${jdbc.driverClassName}"> </property>
- <property name="url" value="${jdbc.url.46}"></property> <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property> </bean>
- <bean id="dataSource"
- class="cn.sh.online.movie.task.autoSwitchDataSource.DynamicDataSource"> <property name="targetDataSources">
- <map key-type="java.lang.Integer"> <entry key="0" value-ref="dataSource0" />
- <entry key="1" value-ref="dataSource1" /> </map>
- </property> <property name="defaultTargetDataSource" ref="dataSource0" />
- </bean>
- <!-- 检查并查找有效的Data source,************* begin --> <bean id="checkDataSource"
- class="cn.xxxxx.task.autoSwitchDataSource.DataSourceCheckTask" /> <bean id="checkDataSourceJob"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject">
- <ref bean="checkDataSource" /> </property>
- <property name="targetMethod"> <value>findAliveDataSource</value>
- </property> </bean>
- <bean id="checkDataSourceCron" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail"> <ref bean="checkDataSourceJob" />
- </property> <property name="cronExpression">
- <value>0 */1 * * * ?</value> </property>
- </bean> <!-- 检查并查找有效的Data source,************* end -->
- <bean
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers">
- <list> <ref local="checkDataSourceCron" />
- </list> </property>
- </bean>
顶(4)
踩(0)
- 最新评论