001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.executor;
016    
017    import com.liferay.portal.kernel.concurrent.RejectedExecutionHandler;
018    import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
019    import com.liferay.portal.kernel.concurrent.ThreadPoolHandler;
020    import com.liferay.portal.kernel.executor.PortalExecutorFactory;
021    import com.liferay.portal.kernel.util.NamedThreadFactory;
022    import com.liferay.portal.util.ClassLoaderUtil;
023    
024    import java.util.concurrent.ThreadFactory;
025    import java.util.concurrent.TimeUnit;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class PortalExecutorFactoryImpl implements PortalExecutorFactory {
031    
032            public void afterPropertiesSet() {
033                    if (_corePoolSize < 0) {
034                            throw new IllegalArgumentException("Core pool size is less than 0");
035                    }
036    
037                    if (_keepAliveTime < 0) {
038                            throw new IllegalArgumentException(
039                                    "Keep alive time is less than 0");
040                    }
041    
042                    if (_maxPoolSize <= 0) {
043                            throw new IllegalArgumentException(
044                                    "Max pool size is less than or equal to 0");
045                    }
046    
047                    if (_maxPoolSize < _corePoolSize) {
048                            throw new IllegalArgumentException(
049                                    "Max pool size is less than core pool size");
050                    }
051    
052                    if (_maxQueueSize <= 0) {
053                            throw new IllegalArgumentException(
054                                    "Max queue size is less than or equal to 0");
055                    }
056    
057                    if (_rejectedExecutionHandler == null) {
058                            throw new IllegalArgumentException(
059                                    "Rejected execution handler is null");
060                    }
061    
062                    if (_threadPoolHandler == null) {
063                            throw new IllegalArgumentException("Thread pool handler is null");
064                    }
065    
066                    if (_timeUnit == null) {
067                            throw new IllegalArgumentException("Time unit is null");
068                    }
069            }
070    
071            @Override
072            public ThreadPoolExecutor createPortalExecutor(String executorName) {
073                    ThreadFactory threadFactory = new NamedThreadFactory(
074                            executorName, Thread.NORM_PRIORITY,
075                            ClassLoaderUtil.getPortalClassLoader());
076    
077                    return new ThreadPoolExecutor(
078                            _corePoolSize, _maxPoolSize, _keepAliveTime, _timeUnit,
079                            _allowCoreThreadTimeout, _maxQueueSize, _rejectedExecutionHandler,
080                            threadFactory, _threadPoolHandler);
081            }
082    
083            public void setAllowCoreThreadTimeout(boolean allowCoreThreadTimeout) {
084                    _allowCoreThreadTimeout = allowCoreThreadTimeout;
085            }
086    
087            public void setCorePoolSize(int corePoolSize) {
088                    _corePoolSize = corePoolSize;
089            }
090    
091            public void setKeepAliveTime(long keepAliveTime) {
092                    _keepAliveTime = keepAliveTime;
093            }
094    
095            public void setMaxPoolSize(int maxPoolSize) {
096                    _maxPoolSize = maxPoolSize;
097            }
098    
099            public void setMaxQueueSize(int maxQueueSize) {
100                    _maxQueueSize = maxQueueSize;
101            }
102    
103            public void setRejectedExecutionHandler(
104                    RejectedExecutionHandler rejectedExecutionHandler) {
105    
106                    _rejectedExecutionHandler = rejectedExecutionHandler;
107            }
108    
109            public void setThreadPoolHandler(ThreadPoolHandler threadPoolHandler) {
110                    _threadPoolHandler = threadPoolHandler;
111            }
112    
113            public void setTimeUnit(TimeUnit timeUnit) {
114                    _timeUnit = timeUnit;
115            }
116    
117            private boolean _allowCoreThreadTimeout;
118            private int _corePoolSize;
119            private long _keepAliveTime;
120            private int _maxPoolSize;
121            private int _maxQueueSize;
122            private RejectedExecutionHandler _rejectedExecutionHandler;
123            private ThreadPoolHandler _threadPoolHandler;
124            private TimeUnit _timeUnit;
125    
126    }