001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.CompanyThreadLocal;
025    import com.liferay.portal.security.auth.PrincipalThreadLocal;
026    
027    import java.util.Set;
028    
029    /**
030     * <p>
031     * Destination that delivers a message to a list of message listeners in
032     * parallel.
033     * </p>
034     *
035     * @author Michael C. Han
036     */
037    public class ParallelDestination extends BaseAsyncDestination {
038    
039            public ParallelDestination() {
040            }
041    
042            /**
043             * @deprecated
044             */
045            public ParallelDestination(String name) {
046                    super(name);
047            }
048    
049            /**
050             * @deprecated
051             */
052            public ParallelDestination(
053                    String name, int workersCoreSize, int workersMaxSize) {
054    
055                    super(name, workersCoreSize, workersMaxSize);
056            }
057    
058            @Override
059            protected void dispatch(
060                    Set<MessageListener> messageListeners, final Message message) {
061    
062                    if (!message.contains("companyId")) {
063                            message.put("companyId", CompanyThreadLocal.getCompanyId());
064                    }
065    
066                    if (!message.contains("principalName")) {
067                            message.put("principalName", PrincipalThreadLocal.getName());
068                    }
069    
070                    if (!message.contains("principalPassword")) {
071                            message.put(
072                                    "principalPassword", PrincipalThreadLocal.getPassword());
073                    }
074    
075                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
076    
077                    for (final MessageListener messageListener : messageListeners) {
078                            Runnable runnable = new MessageRunnable(message) {
079    
080                                    public void run() {
081                                            long companyId = CompanyThreadLocal.getCompanyId();
082                                            String principalName = PrincipalThreadLocal.getName();
083                                            String principalPassword =
084                                                    PrincipalThreadLocal.getPassword();
085    
086                                            try {
087                                                    long messageCompanyId = message.getLong("companyId");
088    
089                                                    if (messageCompanyId > 0) {
090                                                            CompanyThreadLocal.setCompanyId(messageCompanyId);
091                                                    }
092    
093                                                    String messagePrincipalName = message.getString(
094                                                            "principalName");
095    
096                                                    if (Validator.isNotNull(messagePrincipalName)) {
097                                                            PrincipalThreadLocal.setName(messagePrincipalName);
098                                                    }
099    
100                                                    String messagePrincipalPassword = message.getString(
101                                                            "principalPassword");
102    
103                                                    if (Validator.isNotNull(messagePrincipalPassword)) {
104                                                            PrincipalThreadLocal.setPassword(
105                                                                    messagePrincipalPassword);
106                                                    }
107    
108                                                    messageListener.receive(message);
109                                            }
110                                            catch (MessageListenerException mle) {
111                                                    _log.error("Unable to process message " + message, mle);
112                                            }
113                                            finally {
114                                                    CompanyThreadLocal.setCompanyId(companyId);
115                                                    PrincipalThreadLocal.setName(principalName);
116                                                    PrincipalThreadLocal.setPassword(principalPassword);
117                                                    ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
118    
119                                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
120                                            }
121                                    }
122    
123                            };
124    
125                            threadPoolExecutor.execute(runnable);
126                    }
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(ParallelDestination.class);
130    
131    }