View Javadoc

1   package org.apache.jcs.engine;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.jcs.engine.behavior.ICacheEventQueue;
25  import org.apache.jcs.engine.behavior.ICacheListener;
26  import org.apache.jcs.utils.config.OptionConverter;
27  
28  /***
29   * This class hands out event Queues. This allows us to change the implementation more easily. You
30   * can confugure the cache to use a custom type.
31   * <p>
32   * @author aaronsm
33   */
34  public class CacheEventQueueFactory
35  {
36      /*** The logger. */
37      private static final Log log = LogFactory.getLog( CacheEventQueueFactory.class );
38  
39      /***
40       * The most commonly used factory method.
41       * <p>
42       * @param listener
43       * @param listenerId
44       * @param cacheName
45       * @param threadPoolName
46       * @param poolType - SINGLE, QUEUED, or classname
47       * @return ICacheEventQueue
48       */
49      public ICacheEventQueue createCacheEventQueue( ICacheListener listener, long listenerId, String cacheName,
50                                                     String threadPoolName, String poolType )
51      {
52          return createCacheEventQueue( listener, listenerId, cacheName, 10, 500, threadPoolName, poolType );
53      }
54  
55      /***
56       * Fully configured event queue.
57       * <p>
58       * @param listener
59       * @param listenerId
60       * @param cacheName
61       * @param maxFailure
62       * @param waitBeforeRetry
63       * @param threadPoolName null is OK, if not a pooled event queue this is ignored
64       * @param poolType single or pooled
65       * @return ICacheEventQueue
66       */
67      public ICacheEventQueue createCacheEventQueue( ICacheListener listener, long listenerId, String cacheName,
68                                                     int maxFailure, int waitBeforeRetry, String threadPoolName,
69                                                     String poolType )
70      {
71          if ( log.isDebugEnabled() )
72          {
73              log.debug( "threadPoolName = [" + threadPoolName + "] poolType = " + poolType + " " );
74          }
75  
76          ICacheEventQueue eventQueue = null;
77          if ( poolType == null || ICacheEventQueue.SINGLE_QUEUE_TYPE.equalsIgnoreCase( poolType ) )
78          {
79              eventQueue = new CacheEventQueue( listener, listenerId, cacheName, maxFailure, waitBeforeRetry );
80          }
81          else if ( ICacheEventQueue.POOLED_QUEUE_TYPE.equalsIgnoreCase( poolType ) )
82          {
83              eventQueue = new PooledCacheEventQueue( listener, listenerId, cacheName, maxFailure, waitBeforeRetry,
84                                                      threadPoolName );
85          }
86          else
87          {
88              eventQueue = (ICacheEventQueue) OptionConverter.instantiateByClassName( poolType, ICacheEventQueue.class,
89                                                                                      null );
90              if ( eventQueue != null )
91              {
92                  if ( log.isInfoEnabled() )
93                  {
94                      log.info( "Created custom event queue. " + eventQueue );
95                  }
96                  eventQueue.initialize( listener, listenerId, cacheName, maxFailure, waitBeforeRetry, threadPoolName );
97              }
98              else
99              {
100                 log.warn( "Could not instantiate custom event queue [" + poolType
101                     + "].  Will use standard single threaded queue." );
102                 eventQueue = new CacheEventQueue( listener, listenerId, cacheName, maxFailure, waitBeforeRetry );
103             }
104         }
105         return eventQueue;
106     }
107 }