001package org.apache.commons.jcs3.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
022import org.apache.commons.jcs3.engine.behavior.ICacheEventQueue;
023import org.apache.commons.jcs3.engine.behavior.ICacheListener;
024import org.apache.commons.jcs3.log.Log;
025import org.apache.commons.jcs3.log.LogManager;
026
027/**
028 * This class hands out event Queues. This allows us to change the implementation more easily. You
029 * can configure the cache to use a custom type.
030 */
031public class CacheEventQueueFactory<K, V>
032{
033    /** The logger. */
034    private static final Log log = LogManager.getLog( CacheEventQueueFactory.class );
035
036    /**
037     * The most commonly used factory method.
038     * <p>
039     * @param listener
040     * @param listenerId
041     * @param cacheName
042     * @param threadPoolName
043     * @param poolType - SINGLE, POOLED
044     * @return ICacheEventQueue
045     */
046    public ICacheEventQueue<K, V> createCacheEventQueue( final ICacheListener<K, V> listener, final long listenerId, final String cacheName,
047                                                   final String threadPoolName, final ICacheEventQueue.QueueType poolType )
048    {
049        return createCacheEventQueue( listener, listenerId, cacheName, 10, 500, threadPoolName, poolType );
050    }
051
052    /**
053     * Fully configured event queue.
054     * <p>
055     * @param listener
056     * @param listenerId
057     * @param cacheName
058     * @param maxFailure
059     * @param waitBeforeRetry
060     * @param threadPoolName null is OK, if not a pooled event queue this is ignored
061     * @param poolType single or pooled
062     * @return ICacheEventQueue
063     */
064    public ICacheEventQueue<K, V> createCacheEventQueue( final ICacheListener<K, V> listener, final long listenerId, final String cacheName,
065                                                   final int maxFailure, final int waitBeforeRetry, final String threadPoolName,
066                                                   final ICacheEventQueue.QueueType poolType )
067    {
068        log.debug( "threadPoolName = [{0}] poolType = {1}", threadPoolName, poolType );
069
070        ICacheEventQueue<K, V> eventQueue = null;
071        if ( poolType == null || ICacheEventQueue.QueueType.SINGLE == poolType )
072        {
073            eventQueue = new CacheEventQueue<>( listener, listenerId, cacheName, maxFailure, waitBeforeRetry );
074        }
075        else if ( ICacheEventQueue.QueueType.POOLED == poolType )
076        {
077            eventQueue = new PooledCacheEventQueue<>( listener, listenerId, cacheName, maxFailure, waitBeforeRetry,
078                                                    threadPoolName );
079        }
080
081        return eventQueue;
082    }
083}