001    /**
002     * Copyright (c) 2000-present 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.thread.local.Lifecycle;
018    import com.liferay.portal.kernel.cache.thread.local.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                    setWorkersCoreSize(_WORKERS_CORE_SIZE);
038                    setWorkersMaxSize(_WORKERS_MAX_SIZE);
039            }
040    
041            @Override
042            protected void dispatch(
043                    final Set<MessageListener> messageListeners, final Message message) {
044    
045                    final Thread currentThread = Thread.currentThread();
046    
047                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
048    
049                    Runnable runnable = new MessageRunnable(message) {
050    
051                            @Override
052                            public void run() {
053                                    try {
054                                            populateThreadLocalsFromMessage(message);
055    
056                                            for (MessageListener messageListener : messageListeners) {
057                                                    try {
058                                                            messageListener.receive(message);
059                                                    }
060                                                    catch (MessageListenerException mle) {
061                                                            _log.error(
062                                                                    "Unable to process message " + message, mle);
063                                                    }
064                                            }
065                                    }
066                                    finally {
067                                            if (Thread.currentThread() != currentThread) {
068                                                    ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
069    
070                                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
071                                            }
072                                    }
073                            }
074    
075                    };
076    
077                    threadPoolExecutor.execute(runnable);
078            }
079    
080            private static final int _WORKERS_CORE_SIZE = 1;
081    
082            private static final int _WORKERS_MAX_SIZE = 1;
083    
084            private static final Log _log = LogFactoryUtil.getLog(
085                    SerialDestination.class);
086    
087    }