View Javadoc

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