KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > LRUStatefulContextCachePolicy


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb.plugins;
23
24 import java.util.TimerTask JavaDoc;
25
26 import org.jboss.deployment.DeploymentException;
27 import org.jboss.metadata.MetaData;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Least Recently Used cache policy for StatefulSessionEnterpriseContexts.
32  * @author <a HREF="mailto:simone.bordet@compaq.com">Simone Bordet</a>
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 44565 $
35  */

36 public class LRUStatefulContextCachePolicy extends LRUEnterpriseContextCachePolicy
37 {
38    // Constants -----------------------------------------------------
39

40    // Attributes ----------------------------------------------------
41
/* The age after which a bean is automatically removed */
42    private long m_maxBeanLife;
43    /* The remover timer task */
44    private TimerTask JavaDoc m_remover;
45    /* The period of the remover's runs */
46    private long m_removerPeriod;
47    /**
48     * The typed stateful cache
49     */

50    private StatefulSessionInstanceCache ssiCache;
51
52    // Static --------------------------------------------------------
53

54    // Constructors --------------------------------------------------
55
/**
56     * Creates a LRU cache policy object given the instance cache that use this
57     * policy object.
58     */

59    public LRUStatefulContextCachePolicy(AbstractInstanceCache eic)
60    {
61       super(eic);
62       ssiCache = (StatefulSessionInstanceCache) eic;
63    }
64
65    // Public --------------------------------------------------------
66

67    // Monitorable implementation ------------------------------------
68

69    // Z implementation ----------------------------------------------
70
public void start()
71    {
72       super.start();
73       if (m_maxBeanLife > 0)
74       {
75          m_remover = new RemoverTask(m_removerPeriod);
76          long delay = (long) (Math.random() * m_removerPeriod);
77          tasksTimer.schedule(m_remover, delay, m_removerPeriod);
78       }
79    }
80
81    public void stop()
82    {
83       if (m_remover != null)
84       {
85          m_remover.cancel();
86       }
87       super.stop();
88    }
89
90    /**
91     * Reads from the configuration the parameters for this cache policy, that
92     * are all optionals.
93     */

94    public void importXml(Element JavaDoc element) throws DeploymentException
95    {
96       super.importXml(element);
97
98       String JavaDoc rp = MetaData.getElementContent(MetaData.getOptionalChild(element, "remover-period"));
99       String JavaDoc ml = MetaData.getElementContent(MetaData.getOptionalChild(element, "max-bean-life"));
100       try
101       {
102          if (rp != null)
103          {
104             int p = Integer.parseInt(rp);
105             if (p <= 0)
106             {
107                throw new DeploymentException("Remover period can't be <= 0");
108             }
109             m_removerPeriod = p * 1000;
110          }
111          if (ml != null)
112          {
113             int a = Integer.parseInt(ml);
114             if (a <= 0)
115             {
116                throw new DeploymentException("Max bean life can't be <= 0");
117             }
118             m_maxBeanLife = a * 1000;
119          }
120       }
121       catch (NumberFormatException JavaDoc x)
122       {
123          throw new DeploymentException("Can't parse policy configuration", x);
124       }
125    }
126
127    // Y overrides ---------------------------------------------------
128

129    // Package protected ---------------------------------------------
130

131    // Protected -----------------------------------------------------
132

133    // Private -------------------------------------------------------
134

135    // Inner classes -------------------------------------------------
136
/**
137     * This TimerTask removes beans that have not been called for a while.
138     */

139    protected class RemoverTask extends OveragerTask
140    {
141       protected RemoverTask(long period)
142       {
143          super(period);
144       }
145
146       protected String JavaDoc getTaskLogMessage()
147       {
148          return "Removing from cache bean";
149       }
150
151       protected void kickOut(LRUCacheEntry entry)
152       {
153          remove(entry.m_key);
154       }
155
156       protected long getMaxAge()
157       {
158          return m_maxBeanLife;
159       }
160
161       public void run()
162       {
163          if (ssiCache == null)
164          {
165             cancel();
166             return;
167          }
168
169          synchronized (ssiCache.getCacheLock())
170          {
171             log.debug("Running RemoverTask");
172             // Remove beans from cache and passivate them
173
super.run();
174             log.debug("RemoverTask, PassivatedCount=" + ssiCache.getPassivatedCount());
175          }
176          try
177          {
178             // Throw away any passivated beans that have expired
179
ssiCache.removePassivated(getMaxAge() - super.getMaxAge());
180             log.debug("RemoverTask, done");
181          }
182          catch (Throwable JavaDoc t)
183          {
184             log.debug("Ignored error trying to remove passivated beans from cache", t);
185          }
186       }
187    }
188 }
189
Popular Tags