View Javadoc

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