View Javadoc

1   /*
2    * $Id: AbstractMonitoredObjectStore.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.util.store;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.lifecycle.Disposable;
15  import org.mule.api.lifecycle.Initialisable;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.store.ObjectStore;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.util.UUID;
20  import org.mule.util.concurrent.DaemonThreadFactory;
21  
22  import java.io.Serializable;
23  
24  import edu.emory.mathcs.backport.java.util.concurrent.ScheduledThreadPoolExecutor;
25  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * TODO
32   */
33  public abstract class AbstractMonitoredObjectStore<T extends Serializable> 
34      implements ObjectStore<T>, Runnable, MuleContextAware, Initialisable, Disposable
35  {
36      protected final Log logger = LogFactory.getLog(this.getClass());
37  
38      protected MuleContext context;
39      protected ScheduledThreadPoolExecutor scheduler;
40  
41      /**
42       * the maximum number of entries that this store keeps around. Specify <em>-1</em> if the store 
43       * is supposed to be "unbounded".
44       */
45      protected int maxEntries = 4000;
46  
47      /**
48       * The time-to-live for each message ID, specified in milliseconds, or <em>-1</em> for entries 
49       * that should never expire. <b>DO NOT</b> combine this with an unbounded store!
50       */
51      protected int entryTTL = -1;
52  
53      /**
54       * The interval for periodic bounded size enforcement and entry expiration, specified in 
55       * milliseconds. Arbitrary positive values between 1 millisecond and several hours or days are 
56       * possible, but should be chosen carefully according to the expected message rate to prevent 
57       * out of memory conditions.
58       */
59      protected int expirationInterval = 1000;
60  
61      /**
62       * A name for this store, can be used for logging and identification purposes.
63       */
64      protected String name = null;
65  
66      public void initialise() throws InitialisationException
67      {
68          if (name == null)
69          {
70              name = UUID.getUUID();
71          }
72  
73          if (expirationInterval <= 0)
74          {
75              throw new IllegalArgumentException(CoreMessages.propertyHasInvalidValue("expirationInterval",
76                      new Integer(expirationInterval)).toString());
77          }
78  
79          if (scheduler == null)
80          {
81              this.scheduler = new ScheduledThreadPoolExecutor(1);
82              scheduler.setThreadFactory(new DaemonThreadFactory(name + "-Monitor", this.getClass().getClassLoader()));
83              scheduler.scheduleWithFixedDelay(this, 0, expirationInterval, TimeUnit.MILLISECONDS);
84          }
85      }
86  
87      public final void run()
88      {
89          expire();
90      }
91  
92      public void dispose()
93      {
94          if (scheduler != null)
95          {
96              scheduler.shutdown();
97          }
98      }
99  
100     public void setEntryTTL(int entryTTL)
101     {
102         this.entryTTL = entryTTL;
103     }
104 
105     public void setExpirationInterval(int expirationInterval)
106     {
107         this.expirationInterval = expirationInterval;
108     }
109 
110     public void setMaxEntries(int maxEntries)
111     {
112         this.maxEntries = maxEntries;
113     }
114 
115     public void setScheduler(ScheduledThreadPoolExecutor scheduler)
116     {
117         this.scheduler = scheduler;
118     }
119 
120     public void setName(String id)
121     {
122         this.name = id;
123     }
124 
125     public void setMuleContext(MuleContext context)
126     {
127         this.context = context;
128     }
129 
130     public int getEntryTTL()
131     {
132         return entryTTL;
133     }
134 
135     public int getExpirationInterval()
136     {
137         return expirationInterval;
138     }
139 
140     public int getMaxEntries()
141     {
142         return maxEntries;
143     }
144 
145     public String getName()
146     {
147         return name;
148     }
149 
150     public ScheduledThreadPoolExecutor getScheduler()
151     {
152         return scheduler;
153     }
154 
155     protected abstract void expire();
156 }