001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.increment;
016    
017    import com.liferay.portal.kernel.concurrent.BatchablePipe;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageRunnable;
022    
023    import java.util.concurrent.RejectedExecutionHandler;
024    import java.util.concurrent.ThreadPoolExecutor;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class BufferedIncrementDiscardPolicy
030            implements RejectedExecutionHandler {
031    
032            @SuppressWarnings("rawtypes")
033            public void rejectedExecution(
034                    Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {
035    
036                    MessageRunnable messageRunnable = (MessageRunnable)runnable;
037    
038                    Message message = messageRunnable.getMessage();
039    
040                    BatchablePipe<String, BufferedIncreasableEntry> batchablePipe =
041                            (BatchablePipe<String, BufferedIncreasableEntry>)
042                                    message.getPayload();
043    
044                    for (int i = 0; i < _discardNumber; i++) {
045                            BufferedIncreasableEntry bufferedIncreasableEntry =
046                                    (BufferedIncreasableEntry)batchablePipe.take();
047    
048                            if (bufferedIncreasableEntry == null) {
049                                    break;
050                            }
051                            else if (_log.isInfoEnabled()) {
052                                    _log.info(
053                                            "Discarding BufferedIncreasableEntry " +
054                                                    bufferedIncreasableEntry);
055                            }
056                    }
057            }
058    
059            public void setDiscardNumber(int discardNumber) {
060                    _discardNumber = discardNumber;
061            }
062    
063            private static Log _log = LogFactoryUtil.getLog(
064                    BufferedIncrementDiscardPolicy.class);
065    
066            private int _discardNumber = 1;
067    
068    }