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 one at a
029     * time.
030     * </p>
031     *
032     * @author Michael C. Han
033     */
034    public class SerialDestination extends BaseAsyncDestination {
035    
036            public SerialDestination() {
037                    super();
038    
039                    setWorkersCoreSize(_WORKERS_CORE_SIZE);
040                    setWorkersMaxSize(_WORKERS_MAX_SIZE);
041            }
042    
043            /**
044             * @deprecated As of 6.1.0
045             */
046            public SerialDestination(String name) {
047                    super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
048            }
049    
050            @Override
051            protected void dispatch(
052                    final Set<MessageListener> messageListeners, final Message message) {
053    
054                    final Thread currentThread = Thread.currentThread();
055    
056                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
057    
058                    Runnable runnable = new MessageRunnable(message) {
059    
060                            @Override
061                            public void run() {
062                                    try {
063                                            populateThreadLocalsFromMessage(message);
064    
065                                            for (MessageListener messageListener : messageListeners) {
066                                                    try {
067                                                            messageListener.receive(message);
068                                                    }
069                                                    catch (MessageListenerException mle) {
070                                                            _log.error(
071                                                                    "Unable to process message " + message, mle);
072                                                    }
073                                            }
074                                    }
075                                    finally {
076                                            if (Thread.currentThread() != currentThread) {
077                                                    ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
078    
079                                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
080                                            }
081                                    }
082                            }
083    
084                    };
085    
086                    threadPoolExecutor.execute(runnable);
087            }
088    
089            private static final int _WORKERS_CORE_SIZE = 1;
090    
091            private static final int _WORKERS_MAX_SIZE = 1;
092    
093            private static Log _log = LogFactoryUtil.getLog(SerialDestination.class);
094    
095    }