001    package org.apache.jcs.engine;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.Serializable;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.jcs.engine.behavior.ICacheEventQueue;
027    import org.apache.jcs.engine.behavior.ICacheListener;
028    import org.apache.jcs.utils.config.OptionConverter;
029    
030    /**
031     * This class hands out event Queues. This allows us to change the implementation more easily. You
032     * can confugure the cache to use a custom type.
033     * <p>
034     * @author aaronsm
035     */
036    public class CacheEventQueueFactory<K extends Serializable, V extends Serializable>
037    {
038        /** The logger. */
039        private static final Log log = LogFactory.getLog( CacheEventQueueFactory.class );
040    
041        /**
042         * The most commonly used factory method.
043         * <p>
044         * @param listener
045         * @param listenerId
046         * @param cacheName
047         * @param threadPoolName
048         * @param poolType - SINGLE, QUEUED, or classname
049         * @return ICacheEventQueue
050         */
051        public ICacheEventQueue<K, V> createCacheEventQueue( ICacheListener<K, V> listener, long listenerId, String cacheName,
052                                                       String threadPoolName, String poolType )
053        {
054            return createCacheEventQueue( listener, listenerId, cacheName, 10, 500, threadPoolName, poolType );
055        }
056    
057        /**
058         * Fully configured event queue.
059         * <p>
060         * @param listener
061         * @param listenerId
062         * @param cacheName
063         * @param maxFailure
064         * @param waitBeforeRetry
065         * @param threadPoolName null is OK, if not a pooled event queue this is ignored
066         * @param poolType single or pooled
067         * @return ICacheEventQueue
068         */
069        public ICacheEventQueue<K, V> createCacheEventQueue( ICacheListener<K, V> listener, long listenerId, String cacheName,
070                                                       int maxFailure, int waitBeforeRetry, String threadPoolName,
071                                                       String poolType )
072        {
073            if ( log.isDebugEnabled() )
074            {
075                log.debug( "threadPoolName = [" + threadPoolName + "] poolType = " + poolType + " " );
076            }
077    
078            ICacheEventQueue<K, V> eventQueue = null;
079            if ( poolType == null || ICacheEventQueue.SINGLE_QUEUE_TYPE.equalsIgnoreCase( poolType ) )
080            {
081                eventQueue = new CacheEventQueue<K, V>( listener, listenerId, cacheName, maxFailure, waitBeforeRetry );
082            }
083            else if ( ICacheEventQueue.POOLED_QUEUE_TYPE.equalsIgnoreCase( poolType ) )
084            {
085                eventQueue = new PooledCacheEventQueue<K, V>( listener, listenerId, cacheName, maxFailure, waitBeforeRetry,
086                                                        threadPoolName );
087            }
088            else
089            {
090                eventQueue = OptionConverter.instantiateByClassName( poolType, null );
091                if ( eventQueue != null )
092                {
093                    if ( log.isInfoEnabled() )
094                    {
095                        log.info( "Created custom event queue. " + eventQueue );
096                    }
097                    eventQueue.initialize( listener, listenerId, cacheName, maxFailure, waitBeforeRetry, threadPoolName );
098                }
099                else
100                {
101                    log.warn( "Could not instantiate custom event queue [" + poolType
102                        + "].  Will use standard single threaded queue." );
103                    eventQueue = new CacheEventQueue<K, V>( listener, listenerId, cacheName, maxFailure, waitBeforeRetry );
104                }
105            }
106            return eventQueue;
107        }
108    }