快捷搜索:   nginx

java:datasource存活自动检测并切换

 

  1. DataSourceCheckUtil.java   
  2.     public static void checkDataSource(Map<Integer, String> dataSoureMap)              throws Exception {  
  3.         log                  .info("[method]  checkDataSource(Map<Integer, String> dataSoureMap, int index) index = " 
  4.                         + DataSourceId.dataSourceId);          int size = dataSoureMap.size();  
  5.         if (DataSourceId.dataSourceId >= size) {              throw new OverFlowExcetion("数据边界溢出");  
  6.         } else {              if (DataSourceId.dataSourceId < size - 1) {  
  7.                 DataSourceId.dataSourceId = DataSourceId.dataSourceId + 1;              } else {  
  8.                 DataSourceId.dataSourceId = 0;              }  
  9.         }      }  
  10.   
  11. DataSourceCheckTask.java  private ServletContext servletContext;  
  12.       
  13.     private boolean checkDataSourceAlive(String dataSourceName) {          WebApplicationContext wac = WebApplicationContextUtils  
  14.                 .getWebApplicationContext(servletContext);          boolean result = true;  
  15.         Connection cc = null;          try {  
  16.             DataSource ds = (DataSource) wac.getBean(dataSourceName);              cc = ds.getConnection();  
  17.             if (cc.isClosed() || cc == null) {                  result = false;  
  18.             }          } catch (SQLException e) {  
  19.             result = false;              log.error(e);  
  20.         } finally {              try {  
  21.                 cc.close();              } catch (SQLException e) {  
  22.                 log.error(e);              }  
  23.         }   
  24.         return result;      }  
  25.       
  26.     @SuppressWarnings("static-access")      public void findAliveDataSource() {  
  27.         log.info("[method] findAliveDataSource()");          int total = 0;  
  28.         Map<Integer, String> dataSourceMap = DataSourceMap.getInstance().dataSoureMap;          int size = dataSourceMap.size();  
  29.          while (total < size) {  
  30.             total++;              String dataSourceName = dataSourceMap  
  31.                     .get(DataSourceId.dataSourceId);              if (checkDataSourceAlive(dataSourceName)) {  
  32.                 log.info("当前存活的data source id = " + DataSourceId.dataSourceId);                  break;  
  33.             } else {                  try {  
  34.                     DataSourceCheckUtil.checkDataSource(dataSourceMap);                  } catch (Exception e) {  
  35.                     log.error(e);                  }  
  36.             }          }  
  37.      }  
  38.      public void setServletContext(ServletContext arg0) {  
  39.         this.servletContext = arg0;      }  
  40.   
  41. DynamicDataSource  public class DynamicDataSource extends AbstractRoutingDataSource {  
  42.      @Override 
  43.     protected Object determineCurrentLookupKey() {          return DataSourceId.dataSourceId;  
  44.     }   
  45. }   
  46.  spring config file  
  47.  <bean id="dataSource0" 
  48.         class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" 
  49.             value="${jdbc.driverClassName}">          </property>  
  50.         <property name="url" value="${jdbc.url.36}"></property>          <property name="username" value="${jdbc.username}"></property>  
  51.         <property name="password" value="${jdbc.password}"></property>      </bean>  
  52.      <bean id="dataSource1" 
  53.         class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" 
  54.             value="${jdbc.driverClassName}">          </property>  
  55.         <property name="url" value="${jdbc.url.46}"></property>          <property name="username" value="${jdbc.username}"></property>  
  56.         <property name="password" value="${jdbc.password}"></property>      </bean>  
  57.      <bean id="dataSource" 
  58.         class="cn.sh.online.movie.task.autoSwitchDataSource.DynamicDataSource">          <property name="targetDataSources">  
  59.             <map key-type="java.lang.Integer">                  <entry key="0" value-ref="dataSource0" />  
  60.                 <entry key="1" value-ref="dataSource1" />              </map>  
  61.         </property>          <property name="defaultTargetDataSource" ref="dataSource0" />  
  62.     </bean>   
  63.   
  64. <!-- 检查并查找有效的Data source,************* begin -->      <bean id="checkDataSource" 
  65.         class="cn.xxxxx.task.autoSwitchDataSource.DataSourceCheckTask" />      <bean id="checkDataSourceJob" 
  66.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">          <property name="targetObject">  
  67.             <ref bean="checkDataSource" />          </property>  
  68.         <property name="targetMethod">              <value>findAliveDataSource</value>  
  69.         </property>      </bean>  
  70.     <bean id="checkDataSourceCron"         class="org.springframework.scheduling.quartz.CronTriggerBean">  
  71.         <property name="jobDetail">              <ref bean="checkDataSourceJob" />  
  72.         </property>          <property name="cronExpression">  
  73.             <value>0 */1 * * * ?</value>          </property>  
  74.     </bean>      <!-- 检查并查找有效的Data source,************* end -->  
  75.      <bean   
  76.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">          <property name="triggers">  
  77.             <list>                  <ref local="checkDataSourceCron" />  
  78.             </list>          </property>  
  79.      </bean> 

 

顶(4)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论