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.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            @Deprecated
047            public SerialDestination(String name) {
048                    super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
049            }
050    
051            @Override
052            protected void dispatch(
053                    final Set<MessageListener> messageListeners, final Message message) {
054    
055                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
056    
057                    Runnable runnable = new MessageRunnable(message) {
058    
059                            @Override
060                            public void run() {
061                                    try {
062                                            populateThreadLocalsFromMessage(message);
063    
064                                            for (MessageListener messageListener : messageListeners) {
065                                                    try {
066                                                            messageListener.receive(message);
067                                                    }
068                                                    catch (MessageListenerException mle) {
069                                                            _log.error(
070                                                                    "Unable to process message " + message, mle);
071                                                    }
072                                            }
073                                    }
074                                    finally {
075                                            ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
076    
077                                            CentralizedThreadLocal.clearShortLivedThreadLocals();
078                                    }
079                            }
080    
081                    };
082    
083                    threadPoolExecutor.execute(runnable);
084            }
085    
086            private static final int _WORKERS_CORE_SIZE = 1;
087    
088            private static final int _WORKERS_MAX_SIZE = 1;
089    
090            private static final Log _log = LogFactoryUtil.getLog(
091                    SerialDestination.class);
092    
093    }