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.kernel.messaging;
016    
017    import com.liferay.portal.kernel.cache.Lifecycle;
018    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
019    import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
023    
024    import java.util.Set;
025    
026    /**
027     * <p>
028     * Destination that delivers a message to a list of message listeners in
029     * parallel.
030     * </p>
031     *
032     * @author Michael C. Han
033     */
034    public class ParallelDestination extends BaseAsyncDestination {
035    
036            public ParallelDestination() {
037            }
038    
039            /**
040             * @deprecated As of 6.1.0
041             */
042            public ParallelDestination(String name) {
043                    super(name);
044            }
045    
046            /**
047             * @deprecated As of 6.1.0
048             */
049            public ParallelDestination(
050                    String name, int workersCoreSize, int workersMaxSize) {
051    
052                    super(name, workersCoreSize, workersMaxSize);
053            }
054    
055            @Override
056            protected void dispatch(
057                    Set<MessageListener> messageListeners, final Message message) {
058    
059                    final Thread currentThread = Thread.currentThread();
060    
061                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
062    
063                    for (final MessageListener messageListener : messageListeners) {
064                            Runnable runnable = new MessageRunnable(message) {
065    
066                                    @Override
067                                    public void run() {
068                                            try {
069                                                    populateThreadLocalsFromMessage(message);
070    
071                                                    messageListener.receive(message);
072                                            }
073                                            catch (MessageListenerException mle) {
074                                                    _log.error("Unable to process message " + message, mle);
075                                            }
076                                            finally {
077                                                    if (Thread.currentThread() != currentThread) {
078                                                            ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
079    
080                                                            CentralizedThreadLocal.
081                                                                    clearShortLivedThreadLocals();
082                                                    }
083                                            }
084                                    }
085    
086                            };
087    
088                            threadPoolExecutor.execute(runnable);
089                    }
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(ParallelDestination.class);
093    
094    }