001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.messaging;
016    
017    import com.liferay.portal.kernel.util.ThreadLocalRegistry;
018    
019    import java.util.Set;
020    import java.util.concurrent.ThreadPoolExecutor;
021    
022    /**
023     * <p>
024     * Destination that delivers a message to a list of message listeners one at a
025     * time.
026     * </p>
027     *
028     * @author Michael C. Han
029     */
030    public class SerialDestination extends BaseDestination {
031    
032            public SerialDestination() {
033                    super();
034    
035                    setWorkersCoreSize(_WORKERS_CORE_SIZE);
036                    setWorkersMaxSize(_WORKERS_MAX_SIZE);
037            }
038    
039            /**
040             * @deprecated
041             */
042            public SerialDestination(String name) {
043                    super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
044            }
045    
046            protected void dispatch(
047                    final Set<MessageListener> messageListeners, final Message message) {
048    
049                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
050    
051                    Runnable runnable = new MessageRunnable(message) {
052    
053                            public void run() {
054                                    try {
055                                            for (MessageListener messageListener : messageListeners) {
056                                                    messageListener.receive(message);
057                                            }
058                                    }
059                                    finally {
060                                            ThreadLocalRegistry.resetThreadLocals();
061                                    }
062                            }
063    
064                    };
065    
066                    threadPoolExecutor.execute(runnable);
067            }
068    
069            private static final int _WORKERS_CORE_SIZE = 1;
070    
071            private static final int _WORKERS_MAX_SIZE = 1;
072    
073    }