Here is the same code but with the TimerQueue and TimerTask from Simone.
 
Vinay
 
 
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb.plugins;
 
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.util.HashSet;
import javax.ejb.EJBObject;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.RemoveException;
import javax.ejb.EntityBean;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
 
import org.jboss.ejb.Container;
import org.jboss.ejb.EntityContainer;
import org.jboss.ejb.EntityPersistenceManager;
import org.jboss.ejb.EntityEnterpriseContext;
import org.jboss.ejb.EnterpriseContext;
import org.jboss.ejb.InstanceCache;
import org.jboss.ejb.InstancePool;
import org.jboss.ejb.MethodInvocation;
import org.jboss.metadata.ConfigurationMetaData;
import org.jboss.logging.Logger;
import org.jboss.util.TimerQueue;
import org.jboss.util.TimerTask;
 

public class SoftBallInterceptor
extends AbstractInterceptor
{
    /**
     * Commit Option from standardjboss.xml or jboss.xml
     */
    protected int commitOption;
 
    /**
     * The container of this interceptor.
     */
    protected EntityContainer container;
 
    /**
     * Collection of context cache keys
     */
    protected HashSet ctxToInvalidate = new HashSet();
 
    /**
     *  The cache refresh rate
     */
    private static int REFRESH_RATE = 30000;
 

    public void setContainer(Container container)
    {
       this.container = (EntityContainer)container;
    }
 
    public void init()
    throws Exception
    {
       commitOption = container.getBeanMetaData().getContainerConfiguration().getCommitOption();
 
       if(commitOption == ConfigurationMetaData.D_COMMIT_OPTION)
       {
           //Start Timer Task Now!
           new SoftBallScheduler().schedule(new SoftBallSynchronizer(REFRESH_RATE));
        }
    }
 
    public Container getContainer()
    {
       return container;
    }
 
    private void register(EntityEnterpriseContext ctx, Transaction tx)
    {
    }
 
    private void deregister(EntityEnterpriseContext ctx)
    {
    }
 
    // Interceptor implementation --------------------------------------
 
    public Object invokeHome(MethodInvocation mi)
    throws Exception
    {
         return getNext().invokeHome(mi);
 
    }
 
    public Object invoke(MethodInvocation mi)
    throws Exception
    {
       if(commitOption == ConfigurationMetaData.D_COMMIT_OPTION)
       {
           EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext();
 
           //In case the invalidator thread wakes up!
           synchronized(ctxToInvalidate)
           {
             //Check if already present?
             if(!this.ctxToInvalidate.contains(ctx.getCacheKey()))
             {
                 //If not invalidate cache
                 ctx.setValid(false);
                 //and add to the set to make sure it is skipped next time
                 this.ctxToInvalidate.add(ctx.getCacheKey());
             }
           }
       }
 
       return getNext().invoke(mi);
    }
 
    /**
     * The implementation for the TimerQueue that will actually run the
     * timed task for us.
     */
 private static final class SoftBallScheduler
 {
  private static final TimerQueue m_SoftBallScheduler = new TimerQueue();
  static
  {
   m_SoftBallScheduler.start();
  }
  public final void schedule(TimerTask t)
  {
   schedule(t, 0);
  }
  public final void schedule(TimerTask t, long delay)
  {
   m_SoftBallScheduler.schedule(t, delay);
  }
 }
 
    /**
     *
     */
    private class SoftBallSynchronizer
    extends TimerTask
    {
       SoftBallSynchronizer(long refreshRate)
       {
        super(refreshRate);
       }
 
        public void execute()
        {
           synchronized(ctxToInvalidate)
           {
             //Clear the set. Will force invalidation next time on.
             ctxToInvalidate.clear();
           }
        }
    }
}
 
 
Vinay Menon
 
ISe-net Solutions
Carphone Warehouse plc
www.carphonewarehouse.com
 
+44-2088968038 (W)
+44-7801054259 (M)
+44-7808470016 (F)
[EMAIL PROTECTED]

Reply via email to