Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.resource.connectionmanager;
 
 
A ConnectionValidator that performs background validation of managed connections for an InternalManagedConnectionPool.

Author(s):
Weston Price
Version:
$Revision: 71554 $
 
 public class ConnectionValidator
 {
   
   
The log
 
    private static final Logger log = Logger.getLogger(ConnectionValidator.class);
   
   
The pools
 
    private Collection pools = new ArrayList();
   
   
The interval
 
    private long interval = .;

   
The next
 
    private long next = .;//important initialization!
 
   
The validator
 
    private static final ConnectionValidator validator = new ConnectionValidator();
    
    
    private ConnectionValidator(){
       
       AccessController.doPrivileged(new PrivilegedAction()
       {
          public Object run()
          {
             Runnable runnable = new ConnectionValidatorRunnable();
             Thread removerThread = new Thread(runnable"ConnectionValidator");
             removerThread.setDaemon(true);
             removerThread.start();
             return null;
          }
       });
       
    }
    
    public static void registerPool(InternalManagedConnectionPool mcplong interval)
    {
       .internalRegisterPool(mcpinterval);
       
       
    }
    
    public static void unRegisterPool(InternalManagedConnectionPool mcp)
    {
       
    }
    
    
    private void internalRegisterPool(InternalManagedConnectionPool mcplong interval){
       
       synchronized ()
       {
          .add(mcp);
          
          if (interval > 1 && interval/2 < this.
          {
             this. = interval/2;
             long maybeNext = System.currentTimeMillis() + this.;
             if ( > maybeNext && maybeNext > 0) 
            {
                = maybeNext;
               .debug("internalRegisterPool: about to notify thread: old next: " +  + ", new next: " + maybeNext);
               .notify();
            }
         }
      }
      
   }
   
      
      synchronized ()
      {
         .remove(mcp);
         if (.size() == 0) 
         {
            .debug("internalUnregisterPool: setting interval to Long.MAX_VALUE");
             = .;
         }
      
      }
      
   }
   
   private void setupContextClassLoader()
   {
      // Could be null if loaded from system classloader
      final ClassLoader cl = IdleRemover.class.getClassLoader();
      if (cl == null)
         return;
      
      SecurityManager sm = System.getSecurityManager();
      if (sm == null)
         Thread.currentThread().setContextClassLoader(cl);
      
      AccessController.doPrivileged(new PrivilegedAction()
      {
         public Object run()
         {
            Thread.currentThread().setContextClassLoader(cl);
            return null;
         }
      });
   }
   
   public static void waitForBackgroundThread()
   {
      synchronized (.)
      {
         return;
      }
   }
   private class ConnectionValidatorRunnable implements Runnable
   {
      
      public void run()
      {
         setupContextClassLoader();
         
         synchronized ()
         {
            
            while (true)
            {
               try
               {
             
                  .wait();
                  .debug("run: ConnectionValidator notifying pools, interval: " + );
        
                  for(Iterator iter = .iterator(); iter.hasNext();)
                  {
                     
                     InternalManagedConnectionPool mcp = (InternalManagedConnectionPool)iter.next();
                     mcp.validateConnections();
                  }
                   = System.currentTimeMillis() + ;
                  
                  if( < 0)
                  {
                      = .;
                     
                  }
               }
               catch (InterruptedException e)
               {
                  .info("run: ConnectionValidator has been interrupted, returning");
                  return;  
               }
               catch (RuntimeException e)
               {
                  .warn("run: ConnectionValidator ignored unexpected runtime exception"e);
               }
               catch (Exception e)
               {
                  .warn("run: ConnectionValidator ignored unexpected error"e);
               
               }
           
            
            }
            
         }
         
      }
      
   }
New to GrepCode? Check out our FAQ X